Sam Hanson
Sam Hanson

Reputation: 1357

jquery datatable sorting not working for run time values

I used jquery data table for sorting. It works fine under normal condition. If i changed the value in run time, the sorting is not working.

This is my table data

        <table width="94%" cellpadding="2" cellspacing="1" class="tablehead" id="pls-batting">
    <thead>     
    <tr class="tab_head" align="right" id="pls-play" >
    <th width="44" align="center" ># No </th>
    </tr>
    </thead>
    <tbody>
    <tr  id="116706">
    <td align="left" id='1' >test</td>
    </tr>
    <tr id="116707">
    <td align="left" id='2'>bbb</td>
    </tr>
    <tr id="116708">
    <td align="left" id='3' >xxx</td>
    </tr>
    </tbody>
    </table>

Jquery method used for sorting is :

 $(document).ready(function() {

   $('#pls-batting').dataTable( {

            } );
  } );

By clicking the '# No' head the corresponding column displayed in asc and desc order respectively. These TD values will be changed onload by using

        document.getElementById(3).innerHTML = 'something';

So as a result the 3rd column value is 'something'. So sorting will be done by this values. But it is not working.

It takes the old values. Please help me. Thanks

Upvotes: 0

Views: 1037

Answers (1)

Jovan MSFT
Jovan MSFT

Reputation: 14600

In DataTables, you should not update content of HTML cell. DT uses internal JavaScript array structure for searching/sorting and this cell is only a display value.

To update some cell you will need to use DT fnUpdate function see http://datatables.net/api#fnUpdate. Example of updating cell in the table is:

var oTable = $('#example').dataTable(); oTable.fnUpdate( 'New content', 10, 3 );

Note that cells are referenced by row/cell positions. If you don't know row/cell position then you can use http://datatables.net/api#fnGetPosition function to find position of TR with id 3 and use this info to update cell data using the fnGetData function (you can find example on the http://datatables.net/api#fnGetPosition)

Jovan

Upvotes: 1

Related Questions