mirror of
https://github.com/DataTables/DataTables.git
synced 2025-02-18 16:54:14 +01:00
New: API: fnGetData now also allows TD/TH nodes to be passed in and DataTables will figure out the data for the cell, returning that - easier than calculating the column index yourself. So you can now get the whole table's data, a row's data or an individual cell's data from the fnGetData function (you could get the cell data before, but you would need to use fnGetPosition).
New: API: Underscore function updated to use fnGetData rather than its internal function calls. Although slightly more expensive in computation terms, it extends the capacibabites of the underscore function greatly, allowing TD nodes to be the result of the selector as well as TR nodes. So now you can get a column of data with something like: $('#example').dataTable()._('td:nth-child(4)') . Cool :-) Dev: New internal function called _fnNodeToColumnIndex, adapted from fnGetPosition for reuse.
This commit is contained in:
parent
acfee98b7c
commit
3e366a5fe9
64
media/js/jquery.dataTables.js
vendored
64
media/js/jquery.dataTables.js
vendored
@ -21,7 +21,7 @@
|
||||
*/
|
||||
|
||||
/*jslint evil: true, undef: true, browser: true */
|
||||
/*globals $, jQuery,_fnExternApiFunc,_fnInitialise,_fnInitComplete,_fnLanguageCompat,_fnAddColumn,_fnColumnOptions,_fnAddData,_fnCreateTr,_fnGatherData,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnServerParams,_fnAddOptionsHtml,_fnFeatureHtmlTable,_fnScrollDraw,_fnAdjustColumnSizing,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnBuildSearchArray,_fnBuildSearchRow,_fnFilterCreateSearch,_fnDataToSearch,_fnSort,_fnSortAttachListener,_fnSortingClasses,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnFeatureHtmlLength,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnNodeToDataIndex,_fnVisbleColumns,_fnCalculateEnd,_fnConvertToWidth,_fnCalculateColumnWidths,_fnScrollingWidthAdjust,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnDetectType,_fnSettingsFromNode,_fnGetDataMaster,_fnGetTrNodes,_fnGetTdNodes,_fnEscapeRegex,_fnDeleteIndex,_fnReOrderIndex,_fnColumnOrdering,_fnLog,_fnClearTable,_fnSaveState,_fnLoadState,_fnCreateCookie,_fnReadCookie,_fnDetectHeader,_fnGetUniqueThs,_fnScrollBarWidth,_fnApplyToChildren,_fnMap,_fnGetRowData,_fnGetCellData,_fnSetCellData,_fnGetObjectDataFn,_fnSetObjectDataFn,_fnApplyColumnDefs,_fnBindAction,_fnCallbackReg,_fnCallbackFire,_fnJsonString,_fnRender*/
|
||||
/*globals $, jQuery,_fnExternApiFunc,_fnInitialise,_fnInitComplete,_fnLanguageCompat,_fnAddColumn,_fnColumnOptions,_fnAddData,_fnCreateTr,_fnGatherData,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnServerParams,_fnAddOptionsHtml,_fnFeatureHtmlTable,_fnScrollDraw,_fnAdjustColumnSizing,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnBuildSearchArray,_fnBuildSearchRow,_fnFilterCreateSearch,_fnDataToSearch,_fnSort,_fnSortAttachListener,_fnSortingClasses,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnFeatureHtmlLength,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnNodeToDataIndex,_fnVisbleColumns,_fnCalculateEnd,_fnConvertToWidth,_fnCalculateColumnWidths,_fnScrollingWidthAdjust,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnDetectType,_fnSettingsFromNode,_fnGetDataMaster,_fnGetTrNodes,_fnGetTdNodes,_fnEscapeRegex,_fnDeleteIndex,_fnReOrderIndex,_fnColumnOrdering,_fnLog,_fnClearTable,_fnSaveState,_fnLoadState,_fnCreateCookie,_fnReadCookie,_fnDetectHeader,_fnGetUniqueThs,_fnScrollBarWidth,_fnApplyToChildren,_fnMap,_fnGetRowData,_fnGetCellData,_fnSetCellData,_fnGetObjectDataFn,_fnSetObjectDataFn,_fnApplyColumnDefs,_fnBindAction,_fnCallbackReg,_fnCallbackFire,_fnJsonString,_fnRender,_fnNodeToColumnIndex*/
|
||||
|
||||
(/** @lends <global> */function($, window, document, undefined) {
|
||||
/**
|
||||
@ -677,6 +677,29 @@
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Take a TD element and convert it into a column data index (not the visible index)
|
||||
* @param {object} oSettings dataTables settings object
|
||||
* @param {int} iRow The row number the TD/TH can be found in
|
||||
* @param {node} n The TD/TH element to find
|
||||
* @returns {int} index if the node is found, -1 if not
|
||||
* @memberof DataTable#oApi
|
||||
*/
|
||||
function _fnNodeToColumnIndex( oSettings, iRow, n )
|
||||
{
|
||||
var anCells = _fnGetTdNodes( oSettings, iRow );
|
||||
|
||||
for ( var i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
|
||||
{
|
||||
if ( anCells[i] === n )
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an array of data for a given row from the internal data cache
|
||||
* @param {object} oSettings dataTables settings object
|
||||
@ -4745,6 +4768,7 @@
|
||||
* ("current") or not ("all"). If 'current' is given, then order is assumed to be
|
||||
* 'current' and filter is 'applied', regardless of what they might be given as.
|
||||
* @returns {object} jQuery object, filtered by the given selector.
|
||||
* @dtopt API
|
||||
*
|
||||
* @example
|
||||
* $(document).ready(function() {
|
||||
@ -4859,6 +4883,7 @@
|
||||
* @returns {array} Data for the matched TR elements. If any elements, as a result of the
|
||||
* selector, were not TR elements in the DataTable, they will have a null entry in the
|
||||
* array.
|
||||
* @dtopt API
|
||||
*
|
||||
* @example
|
||||
* $(document).ready(function() {
|
||||
@ -4892,8 +4917,7 @@
|
||||
|
||||
for ( i=0, iLen=aTrs.length ; i<iLen ; i++ )
|
||||
{
|
||||
iIndex = _fnNodeToDataIndex( oSettings, aTrs[i] );
|
||||
aOut.push( iIndex!==null ? oSettings.aoData[iIndex]._aData : null );
|
||||
aOut.push( this.fnGetData(aTrs[i]) );
|
||||
}
|
||||
|
||||
return aOut;
|
||||
@ -5430,8 +5454,20 @@
|
||||
|
||||
if ( mRow !== undefined )
|
||||
{
|
||||
var iRow = (typeof mRow === 'object') ?
|
||||
_fnNodeToDataIndex(oSettings, mRow) : mRow;
|
||||
var iRow = mRow;
|
||||
if ( typeof mRow === 'object' )
|
||||
{
|
||||
var sNode = mRow.nodeName.toLowerCase();
|
||||
if (sNode === "tr" )
|
||||
{
|
||||
iRow = _fnNodeToDataIndex(oSettings, mRow);
|
||||
}
|
||||
else if ( sNode === "td" )
|
||||
{
|
||||
iRow = _fnNodeToDataIndex(oSettings, mRow.parentNode);
|
||||
iCol = _fnNodeToColumnIndex( oSettings, iRow, mRow );
|
||||
}
|
||||
}
|
||||
|
||||
if ( iCol !== undefined )
|
||||
{
|
||||
@ -5508,16 +5544,9 @@
|
||||
}
|
||||
else if ( sNodeName == "TD" || sNodeName == "TH" )
|
||||
{
|
||||
var iDataIndex = _fnNodeToDataIndex(oSettings, nNode.parentNode);
|
||||
var anCells = _fnGetTdNodes( oSettings, iDataIndex );
|
||||
|
||||
for ( var i=0 ; i<oSettings.aoColumns.length ; i++ )
|
||||
{
|
||||
if ( anCells[i] == nNode )
|
||||
{
|
||||
return [ iDataIndex, _fnColumnIndexToVisible(oSettings, i ), i ];
|
||||
}
|
||||
}
|
||||
var iDataIndex = _fnNodeToDataIndex( oSettings, nNode.parentNode );
|
||||
var iColumnIndex = _fnNodeToColumnIndex( oSettings, iDataIndex, nNode );
|
||||
return [ iDataIndex, _fnColumnIndexToVisible(oSettings, iColumnIndex ), iColumnIndex ];
|
||||
}
|
||||
return null;
|
||||
};
|
||||
@ -6065,7 +6094,8 @@
|
||||
"_fnCallbackReg": _fnCallbackReg,
|
||||
"_fnCallbackFire": _fnCallbackFire,
|
||||
"_fnJsonString": _fnJsonString,
|
||||
"_fnRender": _fnRender
|
||||
"_fnRender": _fnRender,
|
||||
"_fnNodeToColumnIndex": _fnNodeToColumnIndex
|
||||
};
|
||||
|
||||
$.extend( DataTable.ext.oApi, this.oApi );
|
||||
@ -7446,6 +7476,7 @@
|
||||
* example with a custom Ajax call.
|
||||
* @type array
|
||||
* @default null
|
||||
* @dtopt Option
|
||||
*
|
||||
* @example
|
||||
* // Using a 2D array data source
|
||||
@ -9282,6 +9313,7 @@
|
||||
* do a multi-column sort over the two columns.
|
||||
* @type array
|
||||
* @default null <i>Takes the value of the column index automatically</i>
|
||||
* @dtopt Columns
|
||||
*
|
||||
* @example
|
||||
* // Using aoColumnDefs
|
||||
|
@ -21,7 +21,7 @@
|
||||
*/
|
||||
|
||||
/*jslint evil: true, undef: true, browser: true */
|
||||
/*globals $, jQuery,_fnExternApiFunc,_fnInitialise,_fnInitComplete,_fnLanguageCompat,_fnAddColumn,_fnColumnOptions,_fnAddData,_fnCreateTr,_fnGatherData,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnServerParams,_fnAddOptionsHtml,_fnFeatureHtmlTable,_fnScrollDraw,_fnAdjustColumnSizing,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnBuildSearchArray,_fnBuildSearchRow,_fnFilterCreateSearch,_fnDataToSearch,_fnSort,_fnSortAttachListener,_fnSortingClasses,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnFeatureHtmlLength,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnNodeToDataIndex,_fnVisbleColumns,_fnCalculateEnd,_fnConvertToWidth,_fnCalculateColumnWidths,_fnScrollingWidthAdjust,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnDetectType,_fnSettingsFromNode,_fnGetDataMaster,_fnGetTrNodes,_fnGetTdNodes,_fnEscapeRegex,_fnDeleteIndex,_fnReOrderIndex,_fnColumnOrdering,_fnLog,_fnClearTable,_fnSaveState,_fnLoadState,_fnCreateCookie,_fnReadCookie,_fnDetectHeader,_fnGetUniqueThs,_fnScrollBarWidth,_fnApplyToChildren,_fnMap,_fnGetRowData,_fnGetCellData,_fnSetCellData,_fnGetObjectDataFn,_fnSetObjectDataFn,_fnApplyColumnDefs,_fnBindAction,_fnCallbackReg,_fnCallbackFire,_fnJsonString,_fnRender*/
|
||||
/*globals $, jQuery,_fnExternApiFunc,_fnInitialise,_fnInitComplete,_fnLanguageCompat,_fnAddColumn,_fnColumnOptions,_fnAddData,_fnCreateTr,_fnGatherData,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnServerParams,_fnAddOptionsHtml,_fnFeatureHtmlTable,_fnScrollDraw,_fnAdjustColumnSizing,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnBuildSearchArray,_fnBuildSearchRow,_fnFilterCreateSearch,_fnDataToSearch,_fnSort,_fnSortAttachListener,_fnSortingClasses,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnFeatureHtmlLength,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnNodeToDataIndex,_fnVisbleColumns,_fnCalculateEnd,_fnConvertToWidth,_fnCalculateColumnWidths,_fnScrollingWidthAdjust,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnDetectType,_fnSettingsFromNode,_fnGetDataMaster,_fnGetTrNodes,_fnGetTdNodes,_fnEscapeRegex,_fnDeleteIndex,_fnReOrderIndex,_fnColumnOrdering,_fnLog,_fnClearTable,_fnSaveState,_fnLoadState,_fnCreateCookie,_fnReadCookie,_fnDetectHeader,_fnGetUniqueThs,_fnScrollBarWidth,_fnApplyToChildren,_fnMap,_fnGetRowData,_fnGetCellData,_fnSetCellData,_fnGetObjectDataFn,_fnSetObjectDataFn,_fnApplyColumnDefs,_fnBindAction,_fnCallbackReg,_fnCallbackFire,_fnJsonString,_fnRender,_fnNodeToColumnIndex*/
|
||||
|
||||
(/** @lends <global> */function($, window, document, undefined) {
|
||||
/**
|
||||
|
@ -111,7 +111,8 @@ this.oApi = {
|
||||
"_fnCallbackReg": _fnCallbackReg,
|
||||
"_fnCallbackFire": _fnCallbackFire,
|
||||
"_fnJsonString": _fnJsonString,
|
||||
"_fnRender": _fnRender
|
||||
"_fnRender": _fnRender,
|
||||
"_fnNodeToColumnIndex": _fnNodeToColumnIndex
|
||||
};
|
||||
|
||||
$.extend( DataTable.ext.oApi, this.oApi );
|
||||
|
@ -14,6 +14,7 @@
|
||||
* ("current") or not ("all"). If 'current' is given, then order is assumed to be
|
||||
* 'current' and filter is 'applied', regardless of what they might be given as.
|
||||
* @returns {object} jQuery object, filtered by the given selector.
|
||||
* @dtopt API
|
||||
*
|
||||
* @example
|
||||
* $(document).ready(function() {
|
||||
@ -128,6 +129,7 @@ this.$ = function ( sSelector, oOpts )
|
||||
* @returns {array} Data for the matched TR elements. If any elements, as a result of the
|
||||
* selector, were not TR elements in the DataTable, they will have a null entry in the
|
||||
* array.
|
||||
* @dtopt API
|
||||
*
|
||||
* @example
|
||||
* $(document).ready(function() {
|
||||
@ -161,8 +163,7 @@ this._ = function ( sSelector, oOpts )
|
||||
|
||||
for ( i=0, iLen=aTrs.length ; i<iLen ; i++ )
|
||||
{
|
||||
iIndex = _fnNodeToDataIndex( oSettings, aTrs[i] );
|
||||
aOut.push( iIndex!==null ? oSettings.aoData[iIndex]._aData : null );
|
||||
aOut.push( this.fnGetData(aTrs[i]) );
|
||||
}
|
||||
|
||||
return aOut;
|
||||
@ -666,31 +667,38 @@ this.fnFilter = function( sInput, iColumn, bRegex, bSmart, bShowGlobal, bCaseIns
|
||||
|
||||
|
||||
/**
|
||||
* Return an array with the data which is used to make up the table
|
||||
* or string if both row and column are given
|
||||
* @param {mixed} [mRow] The TR row element to get the data for, or the aoData
|
||||
* internal index (mapped to the TR element)
|
||||
* @param {int} [iCol] Optional column index that you want the data of
|
||||
* @returns {array|string} If mRow is undefined, then the data for all rows is
|
||||
* Get the data for the whole table, an individual row or an individual cell based on the
|
||||
* provided parameters.
|
||||
* @param {int|node} [mRow] A TR row node, TD/TH cell node or an integer. If given as
|
||||
* a TR node then the data source for the whole row will be returned. If given as a
|
||||
* TD/TH cell node then iCol will be automatically calculated and the data for the
|
||||
* cell returned. If given as an integer, then this is treated as the aoData internal
|
||||
* data index for the row (see fnGetPosition) and the data for that row used.
|
||||
* @param {int} [iCol] Optional column index that you want the data of.
|
||||
* @returns {array|object|string} If mRow is undefined, then the data for all rows is
|
||||
* returned. If mRow is defined, just data for that row, and is iCol is
|
||||
* defined, only data for the designated cell is returned.
|
||||
*
|
||||
* @example
|
||||
* // Row data
|
||||
* $(document).ready(function() {
|
||||
* $('#example tbody td').click( function () {
|
||||
* // Get the position of the current data from the node
|
||||
* var aPos = oTable.fnGetPosition( this );
|
||||
*
|
||||
* // Get the data array for this row
|
||||
* var aData = oTable.fnGetData( this.parentNode );
|
||||
*
|
||||
* // Update the data array and return the value
|
||||
* aData[ aPos[1] ] = 'clicked';
|
||||
* this.innerHTML = 'clicked';
|
||||
* } );
|
||||
*
|
||||
* // Init DataTables
|
||||
* oTable = $('#example').dataTable();
|
||||
*
|
||||
* oTable.$('tr').click( function () {
|
||||
* var data = oTable.fnGetData( this );
|
||||
* // ... do something with the array / object of data for the row
|
||||
* } );
|
||||
* } );
|
||||
*
|
||||
* @example
|
||||
* // Individual cell data
|
||||
* $(document).ready(function() {
|
||||
* oTable = $('#example').dataTable();
|
||||
*
|
||||
* oTable.$('td').click( function () {
|
||||
* var sData = oTable.fnGetData( this );
|
||||
* alert( 'The cell clicked on had the value of '+sData );
|
||||
* } );
|
||||
* } );
|
||||
*/
|
||||
this.fnGetData = function( mRow, iCol )
|
||||
@ -699,8 +707,20 @@ this.fnGetData = function( mRow, iCol )
|
||||
|
||||
if ( mRow !== undefined )
|
||||
{
|
||||
var iRow = (typeof mRow === 'object') ?
|
||||
_fnNodeToDataIndex(oSettings, mRow) : mRow;
|
||||
var iRow = mRow;
|
||||
if ( typeof mRow === 'object' )
|
||||
{
|
||||
var sNode = mRow.nodeName.toLowerCase();
|
||||
if (sNode === "tr" )
|
||||
{
|
||||
iRow = _fnNodeToDataIndex(oSettings, mRow);
|
||||
}
|
||||
else if ( sNode === "td" )
|
||||
{
|
||||
iRow = _fnNodeToDataIndex(oSettings, mRow.parentNode);
|
||||
iCol = _fnNodeToColumnIndex( oSettings, iRow, mRow );
|
||||
}
|
||||
}
|
||||
|
||||
if ( iCol !== undefined )
|
||||
{
|
||||
@ -777,16 +797,9 @@ this.fnGetPosition = function( nNode )
|
||||
}
|
||||
else if ( sNodeName == "TD" || sNodeName == "TH" )
|
||||
{
|
||||
var iDataIndex = _fnNodeToDataIndex(oSettings, nNode.parentNode);
|
||||
var anCells = _fnGetTdNodes( oSettings, iDataIndex );
|
||||
|
||||
for ( var i=0 ; i<oSettings.aoColumns.length ; i++ )
|
||||
{
|
||||
if ( anCells[i] == nNode )
|
||||
{
|
||||
return [ iDataIndex, _fnColumnIndexToVisible(oSettings, i ), i ];
|
||||
}
|
||||
}
|
||||
var iDataIndex = _fnNodeToDataIndex( oSettings, nNode.parentNode );
|
||||
var iColumnIndex = _fnNodeToColumnIndex( oSettings, iDataIndex, nNode );
|
||||
return [ iDataIndex, _fnColumnIndexToVisible(oSettings, iColumnIndex ), iColumnIndex ];
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
@ -252,6 +252,29 @@ function _fnNodeToDataIndex( oSettings, n )
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Take a TD element and convert it into a column data index (not the visible index)
|
||||
* @param {object} oSettings dataTables settings object
|
||||
* @param {int} iRow The row number the TD/TH can be found in
|
||||
* @param {node} n The TD/TH element to find
|
||||
* @returns {int} index if the node is found, -1 if not
|
||||
* @memberof DataTable#oApi
|
||||
*/
|
||||
function _fnNodeToColumnIndex( oSettings, iRow, n )
|
||||
{
|
||||
var anCells = _fnGetTdNodes( oSettings, iRow );
|
||||
|
||||
for ( var i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
|
||||
{
|
||||
if ( anCells[i] === n )
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an array of data for a given row from the internal data cache
|
||||
* @param {object} oSettings dataTables settings object
|
||||
|
@ -11,6 +11,7 @@ DataTable.defaults.columns = {
|
||||
* do a multi-column sort over the two columns.
|
||||
* @type array
|
||||
* @default null <i>Takes the value of the column index automatically</i>
|
||||
* @dtopt Columns
|
||||
*
|
||||
* @example
|
||||
* // Using aoColumnDefs
|
||||
|
@ -13,6 +13,7 @@ DataTable.defaults = {
|
||||
* example with a custom Ajax call.
|
||||
* @type array
|
||||
* @default null
|
||||
* @dtopt Option
|
||||
*
|
||||
* @example
|
||||
* // Using a 2D array data source
|
||||
|
Loading…
x
Reference in New Issue
Block a user