Mount HTML table using JSON AJAX

Asked

Viewed 375 times

0

I have a registration page and just below the form I have a table that shows the registered records. This HTML table is mounted with the data registered using JSON via jQuery. Even at this point it works perfectly, but what I need is for each registered record to be updated in this HTML table below the form only the record that was registered at that time and keeping the others that were already. But when I do this it shows the current record that I have registered, but it duplicates all other registered records. How can I resolve this?

       jsonTreatments();

    $("form[name='form_register_treatment']").on('submit', function( event ) { 
        event.preventDefault();
        $.ajax({
        url: "http://...",
        type: "POST",
        dataType: "json",
        data: $( this ).serialize()
        }).done(function( register ){
            if( register.verified === 'registered' ){             
                swal( "Sucesso!", register.message , "success" );
                jsonTreatments();
            }else{
                swal( "Erro!", register.message , "error" );
            }
        });
        return false;
    }); 

    function jsonTreatments(){
        $.getJSON( "http://...", 
            {id : 7 }, function( treatment ){        
              var itens = "";
              $.each(treatment, function( key, val){
                  itens += "<tr>";
                  itens += "<td>" + val.treatment_date_register + "</td>";
                  itens += "<td>" + val.treatment_procedure + " <br> <small class'text-primary'>"+ val.treatment_covenant + "</small></td>";
                  itens += "<td>" + val.treatment_teeth + "</td>";
                  itens += "<td>" + val.treatment_faces + "</td>";
                  itens += '<td> <button class="btn btn-default btn-circle btn-sm"><i class="fa fa-check"></i></button> <button class="btn btn-danger btn-circle btn-sm"><i class="fa fa-trash"></i></button> </td>';
                  itens += "</tr>";
              }) 
              $('#treatments_patient').append( itens );
        });
    }


                    <table class="table color-table info-table">
                    <thead>
                        <tr>
                            <th>Data</th>
                            <th>Procedimento</th>
                            <th>Dente(s)</th>
                            <th>Faces</th>
                            <th>Ações</th>
                        </tr>
                    </thead>
                    <tbody id="treatments_patient">

                    </tbody>
                </table>
  • In jsonTreatments function only the same or all ID 7 is coming?

  • User only with ID 7

1 answer

2


You call the function jsonTreatments() load and mount the table. Then in the first Ajax you call the function again making a .append() with the same data coming from the second Ajax, with this feeding the table with the values that were already there.

The way to solve is to empty the table before the append. You solve this with the method .empty() before the .append():

$('#treatments_patient').empty().append( itens );
  • Thanks Sam, it worked!

  • in the jsonTreatments function, you have the last TD where you have actions to check and delete, but I’m not getting the jquery does not find the Trs of the table are not visible in the DOM, due to the iteration made by the jquery itself, how can I solve this?

  • See if this is it: https://answall.com/a/274000/8063

  • 1

    I was using the class instead of Document and this example you gave me worked perfectly. Thanks again @Sam

Browser other questions tagged

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