Skip to content

Commit

Permalink
add datatables-plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersongoncalves committed Dec 6, 2018
1 parent b819df1 commit 496e051
Show file tree
Hide file tree
Showing 259 changed files with 22,320 additions and 0 deletions.
32 changes: 32 additions & 0 deletions webroot/components/datatables-plugins/api/average().js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* It can sometimes be useful to get the average of data in an API result set,
* be it from a column, or a collection of cells. This method provides exactly
* that ability.
*
* @name average()
* @summary Average the values in a data set.
* @author [Allan Jardine](http://sprymedia.co.uk)
* @requires DataTables 1.10+
*
* @returns {Number} Calculated average
*
* @example
* // Average a column
* var table = $('#example').DataTable();
* table.column( 3 ).data().average();
*
* @example
* // Average two cells
* var table = $('#example').DataTable();
* table.cells( 0, [3,4] ).data().average();
*/

jQuery.fn.dataTable.Api.register( 'average()', function () {
var data = this.flatten();
var sum = data.reduce( function ( a, b ) {
return (a*1) + (b*1); // cast values in-case they are strings
}, 0 );

return sum / data.length;
} );

21 changes: 21 additions & 0 deletions webroot/components/datatables-plugins/api/column().title().js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* This plug-in will read the text from the header cell of a column, returning
* that value.
*
* @name column().title()
* @summary Get the title of a column
* @author Alejandro Navarro
* @requires DataTables 1.10+
*
* @returns {String} Column title
*
* @example
* // Read the title text of column index 3
* var table = $('#example').DataTable();
* table.column( 3 ).title();
*/

$.fn.dataTable.Api.register( 'column().title()', function () {
var colheader = this.header();
return $(colheader).text().trim();
} );
46 changes: 46 additions & 0 deletions webroot/components/datatables-plugins/api/columns().order().js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* The DataTables core library provides the ability to set the ordering via the
* `dt-api column().order()` method, but there is no plural equivalent. While
* multi-column ordering can be set using `dt-api order()` that method requires
* that column indexes be used.
*
* This plug-in provides the plural `columns().order()` method so you can set
* multi-column ordering, while retaining the benefits of the `dt-api columns()`
* selector options.
*
* @name columns().order()
* @summary Apply multi-column ordering through the columns() API method.
* @author [Allan Jardine](http://sprymedia.co.uk)
* @requires DataTables 1.10+
* @param {string|array} dir The order to apply to the columns selected. This
* can be a string (`asc` or `desc`) which will be applied to all columns,
* or an array (again `asc` or `desc` as the elements in the array) which is
* the same length as the number of columns selected, and will be applied to
* the columns in sequence.
*
* @returns {DataTables.Api} DataTables API instance
*
* @example
* // Apply multi-column sorting with a common direction
* table.columns( [ 1, 2 ] ).order( 'desc' ).draw();
*
* @example
* // Multi-column sorting with individual direction for the columns
* table.columns( [ 1, 2 ] ).order( [ 'desc', 'asc' ] ).draw();
*
* @example
* // Multi-column sorting based on a name selector
* table.columns( [ 'sign_up_date:name', 'user_name:name' ] ).order( 'desc' ).draw();
*/

$.fn.dataTable.Api.register( 'columns().order()', function ( dir ) {
return this.iterator( 'columns', function ( settings, columns ) {
var a = [];

for ( var i=0, ien=columns.length ; i<ien ; i++ ) {
a.push( [ columns[i], $.isArray(dir) ? dir[i] : dir ] );
}

new $.fn.dataTable.Api( settings ).order( a );
} );
} );
63 changes: 63 additions & 0 deletions webroot/components/datatables-plugins/api/fnAddDataAndDisplay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Add a new row to the table and display it on the screen by jumping the
* pagination to the required location. This function also returns an object
* with the added `dt-tag TR` element and it's index in `aoData` such that you
* could provide an effect (fade for example) to show which row has been added.
*
* This function is a drop in replacement for `fnAddData` with one important
* exception, it will only take a 1D array or an object, and not a 2D array
* (i.e. it will not add multiple rows like `fnAddData`).
*
* @name fnAddDataAndDisplay
* @summary Add data and shift the paging to display it immediately
* @author [Allan Jardine](http://sprymedia.co.uk)
*
* @param {data} aData Data to add to the table
* @returns {object} Object with `nTr` and `iPos` parameters, where the former
* is the added `dt-tag tr` element and the latter is the row's index.
*
* @example
* $(document).ready(function() {
* var table = $('#example').dataTable();
* table.fnAddDataAndDisplay( [ 1, 2, 3, 4, 5, ... ] );
* } );
*/

jQuery.fn.dataTableExt.oApi.fnAddDataAndDisplay = function ( oSettings, aData )
{
/* Add the data */
var iAdded = this.oApi._fnAddData( oSettings, aData );
var nAdded = oSettings.aoData[ iAdded ].nTr;

/* Need to re-filter and re-sort the table to get positioning correct, not perfect
* as this will actually redraw the table on screen, but the update should be so fast (and
* possibly not alter what is already on display) that the user will not notice
*/
this.oApi._fnReDraw( oSettings );

/* Find it's position in the table */
var iPos = -1;
for( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ )
{
if( oSettings.aoData[ oSettings.aiDisplay[i] ].nTr == nAdded )
{
iPos = i;
break;
}
}

/* Get starting point, taking account of paging */
if( iPos >= 0 )
{
oSettings._iDisplayStart = ( Math.floor(i / oSettings._iDisplayLength) ) * oSettings._iDisplayLength;
if ( this.oApi._fnCalculateEnd ) {
this.oApi._fnCalculateEnd( oSettings );
}
}

this.oApi._fnDraw( oSettings );
return {
"nTr": nAdded,
"iPos": iAdded
};
};
74 changes: 74 additions & 0 deletions webroot/components/datatables-plugins/api/fnAddTr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* This method will add an existing `dt-tag tr` element to a DataTable. This can
* be useful for maintaining custom classes and other attributes which have
* been explicitly assigned to the row.
*
* DataTables 1.10+ has `dt-api row.add()` and `dt-api rows.add()` which have
* this ability built in, and extend it to be able to use jQuery objects as well
* as plain `dt-tag tr` elements. As such this method is marked deprecated, but
* is available for use with legacy version of DataTables. Please use the
* new API if you are used DataTables 1.10 or newer.
*
* @name fnAddTr
* @summary Add a `dt-tag tr` element to the table
* @author [Allan Jardine](http://sprymedia.co.uk)
* @deprecated
*
* @param {node} nTr `dt-tag tr` element to add to the table
* @param {boolean} [bRedraw=false] Indicate if the table should do a redraw or not.
*
* @example
* var table = $('#example').dataTable();
* table.fnAddTr( $('<tr>'+
* '<td>1</td>'+
* '<td>2</td>'+
* '<td>3</td>'+
* '</tr>')[0]
* );
*/

jQuery.fn.dataTableExt.oApi.fnAddTr = function ( oSettings, nTr, bRedraw ) {
if ( typeof bRedraw == 'undefined' )
{
bRedraw = true;
}

var nTds = nTr.getElementsByTagName('td');
if ( nTds.length != oSettings.aoColumns.length )
{
alert( 'Warning: not adding new TR - columns and TD elements must match' );
return;
}

var aData = [];
var aInvisible = [];
var i;
for ( i=0 ; i<nTds.length ; i++ )
{
aData.push( nTds[i].innerHTML );
if (!oSettings.aoColumns[i].bVisible)
{
aInvisible.push( i );
}
}

/* Add the data and then replace DataTable's generated TR with ours */
var iIndex = this.oApi._fnAddData( oSettings, aData );
nTr._DT_RowIndex = iIndex;
oSettings.aoData[ iIndex ].nTr = nTr;

oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();

// Hidding invisible columns
for ( i = (aInvisible.length - 1) ; i >= 0 ; i-- )
{
oSettings.aoData[iIndex]._anHidden[ i ] = nTds[aInvisible[i]];
nTr.removeChild( nTds[aInvisible[i]] );
}

// Redraw
if ( bRedraw )
{
this.oApi._fnReDraw( oSettings );
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* When DataTables removes columns from the display (`bVisible` or
* `fnSetColumnVis`) it removes these elements from the DOM, effecting the index
* value for the column positions. This function converts the data column index
* (i.e. all columns regardless of visibility) into a visible column index.
*
* DataTables 1.10+ has this ability built-in through the
* `dt-api column.index()` method. As such this method is marked deprecated, but
* is available for use with legacy version of DataTables.
*
* @name fnColumnIndexToVisible
* @summary Convert a column data index to a visible index.
* @author [Allan Jardine](http://sprymedia.co.uk)
* @deprecated
*
* @param {integer} iMatch Column data index to convert to visible index
* @returns {integer} Visible column index
*
* @example
* var table = $('#example').dataTable( {
* aoColumnDefs: [
* { bVisible: false, aTargets: [1] }
* ]
* } );
*
* // This will show 1
* alert( 'Column 2 visible index: '+table.fnColumnIndexToVisible(2) );
*/

jQuery.fn.dataTableExt.oApi.fnColumnIndexToVisible = function ( oSettings, iMatch )
{
return oSettings.oApi._fnColumnIndexToVisible( oSettings, iMatch );
};
25 changes: 25 additions & 0 deletions webroot/components/datatables-plugins/api/fnDataUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Update the internal data for a `dt-tag tr` element based on what is used in the
* DOM. You will likely want to call fnDraw() after this function.
*
* DataTables 1.10+ has this ability built-in through the
* `dt-api row().invalidate()` method. As such this method is marked deprecated,
* but is available for use with legacy version of DataTables. Please use the
* new API if you are used DataTables 1.10 or newer.
*
* @name fnDataUpdate
* @summary Update DataTables cached data from the DOM
* @author Lior Gerson
* @deprecated
*
* @param {node} nTr `dt-tag tr` element to get the data from
* @param {integer} iRowIndex Row's position in the table (`fnGetPosition`).
*/

jQuery.fn.dataTableExt.oApi.fnDataUpdate = function ( oSettings, nRowObject, iRowIndex )
{
jQuery(nRowObject).find("TD").each( function(i) {
var iColIndex = oSettings.oApi._fnVisibleToColumnIndex( oSettings, i );
oSettings.oApi._fnSetCellData( oSettings, iRowIndex, iColIndex, jQuery(this).html() );
} );
};
46 changes: 46 additions & 0 deletions webroot/components/datatables-plugins/api/fnDisplayRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* This plug-in will take a `dt-tag tr` element and alter the table's paging
* to make that `dt-tag tr` element (i.e. that row) visible.
*
* @name fnDisplayRow
* @summary Shift the table's paging to display a given `dt-tag tr` element
* @author [Allan Jardine](http://sprymedia.co.uk)
*
* @param {node} nRow Row to display
*
* @example
* // Display the 21st row in the table
* var table = $('#example').dataTable();
* table.fnDisplayRow( table.fnGetNodes()[20] );
*/

jQuery.fn.dataTableExt.oApi.fnDisplayRow = function ( oSettings, nRow )
{
// Account for the "display" all case - row is already displayed
if ( oSettings._iDisplayLength == -1 )
{
return;
}

// Find the node in the table
var iPos = -1;
for( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ )
{
if( oSettings.aoData[ oSettings.aiDisplay[i] ].nTr == nRow )
{
iPos = i;
break;
}
}

// Alter the start point of the paging display
if( iPos >= 0 )
{
oSettings._iDisplayStart = ( Math.floor(i / oSettings._iDisplayLength) ) * oSettings._iDisplayLength;
if ( this.oApi._fnCalculateEnd ) {
this.oApi._fnCalculateEnd( oSettings );
}
}

this.oApi._fnDraw( oSettings );
};
32 changes: 32 additions & 0 deletions webroot/components/datatables-plugins/api/fnDisplayStart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Set the point at which DataTables will start it's display of data in the
* table.
*
* @name fnDisplayStart
* @summary Change the table's paging display start.
* @author [Allan Jardine](http://sprymedia.co.uk)
* @deprecated
*
* @param {integer} iStart Display start index.
* @param {boolean} [bRedraw=false] Indicate if the table should do a redraw or not.
*
* @example
* var table = $('#example').dataTable();
* table.fnDisplayStart( 21 );
*/

jQuery.fn.dataTableExt.oApi.fnDisplayStart = function ( oSettings, iStart, bRedraw )
{
if ( typeof bRedraw == 'undefined' ) {
bRedraw = true;
}

oSettings._iDisplayStart = iStart;
if ( oSettings.oApi._fnCalculateEnd ) {
oSettings.oApi._fnCalculateEnd( oSettings );
}

if ( bRedraw ) {
oSettings.oApi._fnDraw( oSettings );
}
};
Loading

0 comments on commit 496e051

Please sign in to comment.