0
Guys, I need to manipulate an element of the kind checkbox
according to the values in the bank, if in 'contact.favorite' is populated with 'on', the checkbox inside the modal should be marked "checked
".
How can I do this with javascript?
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Favorito</th>
</tr>
</thead>
<tbody>
<?php
foreach( crud::select(select::contatos($id)) as $sql )
{
echo '<tr>';
echo '<th scope="row">#</th>';
echo "<td>".$sql['favorito']."</td>";
echo '<td><button type="button" class="btn" data-toggle="modal" data-target="#modalContatos" data-favorito="'.$sql['favorito'].'" value="">Exibir</button>';
echo '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<div class="bd-example">
<div class="modal fade" id="modalContatos" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="exampleModalLabel">Modal Favorito</h4>
</div>
<div class="modal-body">
<form name="frmModal" method="post" action="../_controllers/update_contatos.php">
<div class="form-group row">
<label class="col-sm-2">Favorito: </label>
<div class="col-sm-4">
<div class="form-check">
<label class="form-check-label">Favorito</label>
<input class="form-check-input" type="checkbox" name="favorito" id="favorito">
</div>
</div>
</div>
<!--[ Form Index End ]-->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
<button type="submit" class="btn btn-primary">Gravar</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
$('#modalContatos').on('show.bs.modal', function (event) {
var favorito = button.data('favorito')
var modal = $(this)
modal.find('#favorito').val(favorito)
}</script>
You could not do an if checking contacts.favorite ='on' you arrow the checkbox:
$('#seuCheckbox').attr( 'checked', true )
? And it’s a little confusing the question, I suggest an issue =]– Brunno
Brunno, thanks for the suggestion. I will study a little about the attr() function and try to do, it is already a direction for me, vlw.
– Jean Freitas
post your html and javascript to help you better =]
– Brunno
Are you already receiving the data in javascript? Otherwise, first you need to read the database information on the server, whether using java or other language, and send this data to the screen. (as for example using spring framework Modelandview) implementing the handleRequest function of the controller class. Thus, in javascript you will have access to favorite contacts. and you can mark the checkbox as checked as Brunno taught you. You missed passing more information so we can help. Have the custom to detail the question better, if possible by posting code and what you already have t
– Fabio Carvalho
I posted the code excerpt, the modal is a form where you will edit the table contacts data. I know it’s a lot of effort but I have the project on github if you want to look link
– Jean Freitas