Providing the UI to allow end users to reorder a table is only half of the story - likely you will wish to have the changes caused by the end user to effect a
database or some other data store. This can be done by listening for the row-reorder
event.
This examples shows how the row-reorder
event can
be listened for and an action taken when it is triggered. In this case we simply output data about the change to the page, but a more sophisticated use case might
involve using Ajax to inform a server-side about the change.
An example of Editor using RowReorder making use of Editor's multi-row editing ability, is available on the Editor web-site.
Seq. | Name | Position | Office | Start date | Salary |
---|---|---|---|---|---|
Seq. | Name | Position | Office | Start date | Salary |
1 | Shou Itou | Regional Marketing | Tokyo | 2011/08/14 | $163,000 |
2 | Tiger Nixon | System Architect | Edinburgh | 2011/04/25 | $320,800 |
3 | Bruno Nash | Software Engineer | London | 2011/05/03 | $163,500 |
4 | Olivia Liang | Support Engineer | Singapore | 2011/02/03 | $234,500 |
5 | Gavin Joyce | Developer | Edinburgh | 2010/12/22 | $92,575 |
6 | Ashton Cox | Junior Technical Author | San Francisco | 2009/01/12 | $86,000 |
7 | Fiona Green | Chief Operating Officer (COO) | San Francisco | 2010/03/11 | $850,000 |
8 | Martena Mccray | Post-Sales support | Edinburgh | 2011/03/09 | $324,050 |
9 | Hermione Butler | Regional Director | London | 2011/03/21 | $356,250 |
10 | Finn Camacho | Support Engineer | San Francisco | 2009/07/07 | $87,500 |
The Javascript shown below is used to initialise the table shown in this example:
$(document).ready(function() {
var table = $('#example').DataTable( {
rowReorder: true
} );
table.on( 'row-reorder', function ( e, diff, edit ) {
var result = 'Reorder started on row: '+edit.triggerRow.data()[1]+'<br>';
for ( var i=0, ien=diff.length ; i<ien ; i++ ) {
var rowData = table.row( diff[i].node ).data();
result += rowData[1]+' updated to be in position '+
diff[i].newData+' (was '+diff[i].oldData+')<br>';
}
$('#result').html( 'Event result:<br>'+result );
} );
} );
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:
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:
#result {
border: 1px solid #888;
background: #f7f7f7;
padding: 1em;
margin-bottom: 1em;
}
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.