| 8 |
lars |
1 |
/**
|
|
|
2 |
* Add a new row to the table and display it on the screen by jumping the
|
|
|
3 |
* pagination to the required location. This function also returns an object
|
|
|
4 |
* with the added `dt-tag TR` element and it's index in `aoData` such that you
|
|
|
5 |
* could provide an effect (fade for example) to show which row has been added.
|
|
|
6 |
*
|
|
|
7 |
* This function is a drop in replacement for `fnAddData` with one important
|
|
|
8 |
* exception, it will only take a 1D array or an object, and not a 2D array
|
|
|
9 |
* (i.e. it will not add multiple rows like `fnAddData`).
|
|
|
10 |
*
|
|
|
11 |
* @name fnAddDataAndDisplay
|
|
|
12 |
* @summary Add data and shift the paging to display it immediately
|
|
|
13 |
* @author [Allan Jardine](http://sprymedia.co.uk)
|
|
|
14 |
*
|
|
|
15 |
* @param {data} aData Data to add to the table
|
|
|
16 |
* @returns {object} Object with `nTr` and `iPos` parameters, where the former
|
|
|
17 |
* is the added `dt-tag tr` element and the latter is the row's index.
|
|
|
18 |
*
|
|
|
19 |
* @example
|
|
|
20 |
* $(document).ready(function() {
|
|
|
21 |
* var table = $('#example').dataTable();
|
|
|
22 |
* table.fnAddDataAndDisplay( [ 1, 2, 3, 4, 5, ... ] );
|
|
|
23 |
* } );
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
jQuery.fn.dataTableExt.oApi.fnAddDataAndDisplay = function ( oSettings, aData )
|
|
|
27 |
{
|
|
|
28 |
/* Add the data */
|
|
|
29 |
var iAdded = this.oApi._fnAddData( oSettings, aData );
|
|
|
30 |
var nAdded = oSettings.aoData[ iAdded ].nTr;
|
|
|
31 |
|
|
|
32 |
/* Need to re-filter and re-sort the table to get positioning correct, not perfect
|
|
|
33 |
* as this will actually redraw the table on screen, but the update should be so fast (and
|
|
|
34 |
* possibly not alter what is already on display) that the user will not notice
|
|
|
35 |
*/
|
|
|
36 |
this.oApi._fnReDraw( oSettings );
|
|
|
37 |
|
|
|
38 |
/* Find it's position in the table */
|
|
|
39 |
var iPos = -1;
|
|
|
40 |
for( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ )
|
|
|
41 |
{
|
|
|
42 |
if( oSettings.aoData[ oSettings.aiDisplay[i] ].nTr == nAdded )
|
|
|
43 |
{
|
|
|
44 |
iPos = i;
|
|
|
45 |
break;
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/* Get starting point, taking account of paging */
|
|
|
50 |
if( iPos >= 0 )
|
|
|
51 |
{
|
|
|
52 |
oSettings._iDisplayStart = ( Math.floor(i / oSettings._iDisplayLength) ) * oSettings._iDisplayLength;
|
|
|
53 |
if ( this.oApi._fnCalculateEnd ) {
|
|
|
54 |
this.oApi._fnCalculateEnd( oSettings );
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
this.oApi._fnDraw( oSettings );
|
|
|
59 |
return {
|
|
|
60 |
"nTr": nAdded,
|
|
|
61 |
"iPos": iAdded
|
|
|
62 |
};
|
|
|
63 |
};
|