Update ColReorder Datatables plugin

This commit is contained in:
Martin Konecny 2013-02-14 17:48:32 -05:00
parent b30f67b24b
commit 4f3ea3f406

View file

@ -1,8 +1,8 @@
/* /*
* File: ColReorder.js * File: ColReorder.js
* Version: 1.0.5 * Version: 1.0.8
* CVS: $Id$ * CVS: $Id$
* Description: Controls for column visiblity in DataTables * Description: Allow columns to be reordered in a DataTable
* Author: Allan Jardine (www.sprymedia.co.uk) * Author: Allan Jardine (www.sprymedia.co.uk)
* Created: Wed Sep 15 18:23:29 BST 2010 * Created: Wed Sep 15 18:23:29 BST 2010
* Modified: $Date$ by $Author$ * Modified: $Date$ by $Author$
@ -174,10 +174,10 @@ $.fn.dataTableExt.oApi.fnColReorder = function ( oSettings, iFrom, iTo )
for ( i=0, iLen=iCols ; i<iLen ; i++ ) for ( i=0, iLen=iCols ; i<iLen ; i++ )
{ {
oCol = oSettings.aoColumns[i]; oCol = oSettings.aoColumns[i];
if ( typeof oCol.mDataProp == 'number' ) { if ( typeof oCol.mData == 'number' ) {
oCol.mDataProp = aiInvertMapping[ oCol.mDataProp ]; oCol.mData = aiInvertMapping[ oCol.mData ];
oCol.fnGetData = oSettings.oApi._fnGetObjectDataFn( oCol.mDataProp ); oCol.fnGetData = oSettings.oApi._fnGetObjectDataFn( oCol.mData );
oCol.fnSetData = oSettings.oApi._fnSetObjectDataFn( oCol.mDataProp ); oCol.fnSetData = oSettings.oApi._fnSetObjectDataFn( oCol.mData );
} }
} }
@ -274,13 +274,12 @@ $.fn.dataTableExt.oApi.fnColReorder = function ( oSettings, iFrom, iTo )
} }
/* /* Fire an event so other plug-ins can update */
* Any extra operations for the other plug-ins $(oSettings.oInstance).trigger( 'column-reorder', [ oSettings, {
*/ "iFrom": iFrom,
if ( typeof ColVis != 'undefined' ) "iTo": iTo,
{ "aiInvertMapping": aiInvertMapping
ColVis.fnRebuild( oSettings.oInstance ); } ] );
}
if ( typeof oSettings.oInstance._oPluginFixedHeader != 'undefined' ) if ( typeof oSettings.oInstance._oPluginFixedHeader != 'undefined' )
{ {
@ -295,10 +294,10 @@ $.fn.dataTableExt.oApi.fnColReorder = function ( oSettings, iFrom, iTo )
* ColReorder provides column visiblity control for DataTables * ColReorder provides column visiblity control for DataTables
* @class ColReorder * @class ColReorder
* @constructor * @constructor
* @param {object} DataTables object * @param {object} DataTables settings object
* @param {object} ColReorder options * @param {object} ColReorder options
*/ */
ColReorder = function( oTable, oOpts ) ColReorder = function( oDTSettings, oOpts )
{ {
/* Santiy check that we are a new instance */ /* Santiy check that we are a new instance */
if ( !this.CLASS || this.CLASS != "ColReorder" ) if ( !this.CLASS || this.CLASS != "ColReorder" )
@ -401,9 +400,12 @@ ColReorder = function( oTable, oOpts )
/* Constructor logic */ /* Constructor logic */
this.s.dt = oTable.fnSettings(); this.s.dt = oDTSettings.oInstance.fnSettings();
this._fnConstruct(); this._fnConstruct();
/* Add destroy callback */
oDTSettings.oApi._fnCallbackReg(oDTSettings, 'aoDestroyCallback', jQuery.proxy(this._fnDestroy, this), 'ColReorder');
/* Store the instance for later use */ /* Store the instance for later use */
ColReorder.aoInstances.push( this ); ColReorder.aoInstances.push( this );
return this; return this;
@ -527,7 +529,7 @@ ColReorder.prototype = {
{ {
if ( a.length != this.s.dt.aoColumns.length ) if ( a.length != this.s.dt.aoColumns.length )
{ {
this.s.dt.oInstance.oApi._fnLog( oDTSettings, 1, "ColReorder - array reorder does not "+ this.s.dt.oInstance.oApi._fnLog( this.s.dt, 1, "ColReorder - array reorder does not "+
"match known number of columns. Skipping." ); "match known number of columns. Skipping." );
return; return;
} }
@ -611,8 +613,8 @@ ColReorder.prototype = {
{ {
var that = this; var that = this;
$(nTh).bind( 'mousedown.ColReorder', function (e) { $(nTh).bind( 'mousedown.ColReorder', function (e) {
e.preventDefault();
that._fnMouseDown.call( that, e, nTh ); that._fnMouseDown.call( that, e, nTh );
return false;
} ); } );
}, },
@ -812,7 +814,7 @@ ColReorder.prototype = {
} }
$('thead tr:eq(0)', this.dom.drag).each( function () { $('thead tr:eq(0)', this.dom.drag).each( function () {
$('th:not(:eq('+that.s.mouse.targetIndex+'))', this).remove(); $('th', this).eq(that.s.mouse.targetIndex).siblings().remove();
} ); } );
$('tr', this.dom.drag).height( $('tr:eq(0)', that.s.dt.nTHead).height() ); $('tr', this.dom.drag).height( $('tr:eq(0)', that.s.dt.nTHead).height() );
@ -845,6 +847,29 @@ ColReorder.prototype = {
document.body.appendChild( this.dom.pointer ); document.body.appendChild( this.dom.pointer );
document.body.appendChild( this.dom.drag ); document.body.appendChild( this.dom.drag );
},
/**
* Clean up ColReorder memory references and event handlers
* @method _fnDestroy
* @returns void
* @private
*/
"_fnDestroy": function ()
{
for ( var i=0, iLen=ColReorder.aoInstances.length ; i<iLen ; i++ )
{
if ( ColReorder.aoInstances[i] === this )
{
ColReorder.aoInstances.splice( i, 1 );
break;
}
}
$(this.s.dt.nTHead).find( '*' ).unbind( '.ColReorder' );
this.s.dt.oInstance._oPluginColReorder = null;
this.s = null;
} }
}; };
@ -914,7 +939,7 @@ ColReorder.prototype.CLASS = "ColReorder";
* @type String * @type String
* @default As code * @default As code
*/ */
ColReorder.VERSION = "1.0.5"; ColReorder.VERSION = "1.0.8";
ColReorder.prototype.VERSION = ColReorder.VERSION; ColReorder.prototype.VERSION = ColReorder.VERSION;
@ -930,7 +955,7 @@ ColReorder.prototype.VERSION = ColReorder.VERSION;
*/ */
if ( typeof $.fn.dataTable == "function" && if ( typeof $.fn.dataTable == "function" &&
typeof $.fn.dataTableExt.fnVersionCheck == "function" && typeof $.fn.dataTableExt.fnVersionCheck == "function" &&
$.fn.dataTableExt.fnVersionCheck('1.9.0') ) $.fn.dataTableExt.fnVersionCheck('1.9.3') )
{ {
$.fn.dataTableExt.aoFeatures.push( { $.fn.dataTableExt.aoFeatures.push( {
"fnInit": function( oDTSettings ) { "fnInit": function( oDTSettings ) {
@ -938,7 +963,7 @@ if ( typeof $.fn.dataTable == "function" &&
if ( typeof oTable._oPluginColReorder == 'undefined' ) { if ( typeof oTable._oPluginColReorder == 'undefined' ) {
var opts = typeof oDTSettings.oInit.oColReorder != 'undefined' ? var opts = typeof oDTSettings.oInit.oColReorder != 'undefined' ?
oDTSettings.oInit.oColReorder : {}; oDTSettings.oInit.oColReorder : {};
oTable._oPluginColReorder = new ColReorder( oDTSettings.oInstance, opts ); oTable._oPluginColReorder = new ColReorder( oDTSettings, opts );
} else { } else {
oTable.oApi._fnLog( oDTSettings, 1, "ColReorder attempted to initialise twice. Ignoring second" ); oTable.oApi._fnLog( oDTSettings, 1, "ColReorder attempted to initialise twice. Ignoring second" );
} }
@ -951,7 +976,7 @@ if ( typeof $.fn.dataTable == "function" &&
} }
else else
{ {
alert( "Warning: ColReorder requires DataTables 1.9.0 or greater - www.datatables.net/download"); alert( "Warning: ColReorder requires DataTables 1.9.3 or greater - www.datatables.net/download");
} }
})(jQuery, window, document); })(jQuery, window, document);