How to manipulate a checkbox in a modal Bootstrap from BD

Asked

Viewed 632 times

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">&times;</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>
  • 1

    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 =]

  • 1

    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.

  • post your html and javascript to help you better =]

  • 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

  • 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

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.