0

For the first time when i insert data into table it get inserted into datatable tbody. next time when i change data tbody get appened with datatable tbody. I want to remove tbody of datatable for my code to work.

 <table frame="hsides" id="table">
        <thead>
            <tr>
                <th>Rules</th>
            </tr>
     </thead>
 </table>


$.ajax({url: '../controllers/manage_rule_controller.php',

    type: 'POST',
    data: { "operation": "get_rule"},
    dataType:"json",
        },

         success: function (data) {
         keys = Object.keys(data);
         var table = document.getElementById("table");
         $("#table").find("tr:not(:first)").remove();
         var tBody = document.createElement("tbody");
         table.appendChild(tBody);

        for(i=0;i<keys.length;i++)
        {
            var row = tBody.insertRow(i);
            var cell1 = row.insertCell(0);
            cell1.innerHTML = "<div class='col-md-12 col-sm-12 col-xs-12 testdiv' id='fullrule"+keys[i]+"'><div style='border-bottom:1px solid #f2f2f2' class='col-md-10 col-sm-10 col-xs-8'  id='rule"+keys[i]+"' onclick='updaterule(this)'>"+data[keys[i]]+" </div></div>"

        } var dataTable = $('#table').DataTable();  
    }, 
  });
2
  • Try adding tBody.html("");before the for loop Commented Jul 19, 2016 at 10:43
  • No it is not working Commented Jul 19, 2016 at 10:55

2 Answers 2

1

Before calling your ajax and redrawing your datatable,

var dataTable = $('#table').DataTable();
dataTable.rows().remove().draw();
Sign up to request clarification or add additional context in comments.

Comments

0

its because of you are using table.appendChild(tBody), this will append not over write the newly generated html each time. So try to use html() or clear old html before appending new one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.