Datatables .row() is undefined

Shin Dapaty picture Shin Dapaty · Jul 3, 2014 · Viewed 38.2k times · Source

i want to delete rows by simply clicking related Button.. data tables is works, i can use some basic function like sort and search in datatable, but when i click the button it just simply said undefined error :

for your information, im using datatable 1.10 and jquery 1.10.2 enter image description here

Code :

<table cellpadding="0" cellspacing="0" border="0" class="row-border" id="table">
<thead>
    <th>Video ID</th>
    <th>Filename</th>
    <th>Action</th>
</thead>
<tbody>
    <td>1</td>
    <td>ABCD</td>
    <td><input type='button' name='deleteBtn' value='Delete' />
</tbody>
<tfoot>
    <tr>
        <th>Video ID</th>
        <th>Filename</th>
        <th>Action</th>
    </tr>
</tfoot>
</table>

<script src="jquery.min.js"></script>
<script src="jquery.dataTables.min.js"></script>
<script type="text/javascript">
var table = $('#table').dataTable( {} );

$('#table tbody').on('click',"input[type='button']",function() {
    table
        .row( $(this).parents('tr') )**
        .remove()
        .draw();
});
</script>
</body>
</html>

Answer

davidkonrad picture davidkonrad · Jul 3, 2014

It does not work, because there is a huge difference between the dataTable() constructor and the DataTable() constructor introduced in 1.10.x (see docs) :

The difference between the two is that the first will return a jQuery object, while the second returns a DataTables API instance.

Simply change

var table = $('#table').dataTable( {} );

to

var table = $('#table').DataTable( {} );

if you want to work on the new dataTables API through the table variable.
See your code working -> http://jsfiddle.net/Sd6UQ/

NB : Remember to use <tr>..</tr> and close <td>'s properly. dataTables can be very sensitive to malformed markup.