When you want to have user selectable rows in DataTables, it is relatively trivial when using DOM based data - but when using server-side processing, DataTables doesn't retain DOM row elements over pages / filtering etc. As such, you will need to keep a track of which rows a user as selected and mark them as selected on each draw.
This is shown in this demo, which uses a unique ID assigned to the TR element (this is done automatically through the use of the DT_RowId
special
property returned as part of the object given by the server for each row) to track which rows are selected and reselect them is appropriate on a draw.
If you are looking for a more complete and easier to use row selection option, the Select extension provides an API that is fully integrated with DataTables for selecting rows and acting upon that selection.
First name | Last name | Position | Office | Start date | Salary |
---|---|---|---|---|---|
First name | Last name | Position | Office | Start date | Salary |
The Javascript shown below is used to initialise the table shown in this example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | $(document).ready( function () { var selected = []; $( "#example" ).DataTable({ "processing" : true , "serverSide" : true , "ajax" : "scripts/ids-arrays.php" , "rowCallback" : function ( row, data ) { if ( $.inArray(data.DT_RowId, selected) !== -1 ) { $(row).addClass( 'selected' ); } } }); $( '#example tbody' ).on( 'click' , 'tr' , function () { var id = this .id; var index = $.inArray(id, selected); if ( index === -1 ) { selected.push( id ); } else { selected.splice( index, 1 ); } $( this ).toggleClass( 'selected' ); } ); } ); |
In addition to the above code, the following Javascript library files are loaded for use in this example:
The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Position</th>
<th>Office</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Position</th>
<th>Office</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js plain">$(document).ready(</code><code class="js keyword">function</code><code class="js plain">() {</code></div><div class="line number2 index1 alt1"><code class="js spaces"> </code><code class="js keyword">var</code> <code class="js plain">selected = [];</code></div><div class="line number3 index2 alt2"> </div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js plain">$(</code><code class="js string">"#example"</code><code class="js plain">).DataTable({</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js string">"processing"</code><code class="js plain">: </code><code class="js keyword">true</code><code class="js plain">,</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js string">"serverSide"</code><code class="js plain">: </code><code class="js keyword">true</code><code class="js plain">,</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js string">"ajax"</code><code class="js plain">: </code><code class="js string">"scripts/ids-arrays.php"</code><code class="js plain">,</code></div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js string">"rowCallback"</code><code class="js plain">: </code><code class="js keyword">function</code><code class="js plain">( row, data ) {</code></div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">( $.inArray(data.DT_RowId, selected) !== -1 ) {</code></div><div class="line number10 index9 alt1"><code class="js spaces"> </code><code class="js plain">$(row).addClass(</code><code class="js string">'selected'</code><code class="js plain">);</code></div><div class="line number11 index10 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js plain">});</code></div><div class="line number14 index13 alt1"> </div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js plain">$(</code><code class="js string">'#example tbody'</code><code class="js plain">).on(</code><code class="js string">'click'</code><code class="js plain">, </code><code class="js string">'tr'</code><code class="js plain">, </code><code class="js keyword">function</code> <code class="js plain">() {</code></div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js keyword">var</code> <code class="js plain">id = </code><code class="js keyword">this</code><code class="js plain">.id;</code></div><div class="line number17 index16 alt2"><code class="js spaces"> </code><code class="js keyword">var</code> <code class="js plain">index = $.inArray(id, selected);</code></div><div class="line number18 index17 alt1"> </div><div class="line number19 index18 alt2"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">( index === -1 ) {</code></div><div class="line number20 index19 alt1"><code class="js spaces"> </code><code class="js plain">selected.push( id );</code></div><div class="line number21 index20 alt2"><code class="js spaces"> </code><code class="js plain">} </code><code class="js keyword">else</code> <code class="js plain">{</code></div><div class="line number22 index21 alt1"><code class="js spaces"> </code><code class="js plain">selected.splice( index, 1 );</code></div><div class="line number23 index22 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number24 index23 alt1"> </div><div class="line number25 index24 alt2"><code class="js spaces"> </code><code class="js plain">$(</code><code class="js keyword">this</code><code class="js plain">).toggleClass(</code><code class="js string">'selected'</code><code class="js plain">);</code></div><div class="line number26 index25 alt1"><code class="js spaces"> </code><code class="js plain">} );</code></div><div class="line number27 index26 alt2"><code class="js plain">} );</code></div></div></td></tr></tbody></table><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2" style="display: none;">1</div></td><td class="code"><div class="container" style="display: none;"><div class="line number1 index0 alt2" style="display: none;"> </div></div></td></tr></tbody></table>
This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The additional CSS used is shown below:
1 |
The following CSS library files are loaded for use in this example to provide the styling of the table:
This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is loaded.
The script used to perform the server-side processing for this table is shown below. Please note that this is just an example script using PHP. Server-side processing scripts can be written in any language, using the protocol described in the DataTables documentation.
Please refer to the DataTables documentation for full information about its API properties and methods.
Additionally, there are a wide range of extensions and plug-ins which extend the capabilities of DataTables.
DataTables designed and created by SpryMedia Ltd © 2007-2018
DataTables is licensed under the MIT license.