Change the state of a checkbox component in the bank using AJAX

1
Very good night to everyone!

Well, I have a table in which there is a checkbox component. This component has the task of enabling and disabling a target on my website. The figure below shows a table row with that element:

Hereisthehtmlcode,itdoesthecheckonlyifitiscomingtrueorfalsefromthedatabasetorenderonthescreen:

<c:forEachitems="${destination}" var="d">
   <tr>
      <td>${d.dtName}</td>
      <td>${d.categories.ctName}</td>
      <td>
         <div class="make-switch" data-on="primary" data-off="info">
            <c:choose>
               <c:when test="${d.dtAppearWebsite}">
                  <input type="checkbox" checked>
               </c:when>
               <c:otherwise>
                  <input type="checkbox">
               </c:otherwise>
            </c:choose>
         </div>
      </td>
   </tr>
</c:forEach>

Well, my question is how could I change the state of my component via AJAX and reflect on the database. The biggest question is how do I get that component and make the change asynchronously.

Has anyone ever done anything like this and can you give me some strength?

Thanks for everyone's attention

    
asked by anonymous 13.08.2014 / 02:56

1 answer

2

To make the change in the bank, you need to have an action that does this. Let's consider that there are 2 actions, the ativarDestino and the desativarDestino . When the checkbox is selected we will call activate, otherwise we call the other.

$(document).ready(function() {

    // usando id para identificar o checkbox
    $('#myCheckbox').click(function() {
        var action = $(this).is(':checked') ? 'ativarDestino' : 'desativarDestino';

        // uma vez determinada a action, é só usar o get do jQuery
        $.get(action, function(data) {
            // faz alguma coisa com o retorno, manda msg de sucesso, algo assim
        })
        .fail(function(error) {
            // se der problema cai aqui. Você pode exibir o erro e desfazer a checagem do checkbox
        });
    });
});

You can see the jQuery.get documentation here: link

    
13.08.2014 / 20:50