ColReorder breaks ColVis's exclude and restore functionality

ColReorder breaks ColVis's exclude and restore functionality

richardprichardp Posts: 10Questions: 0Answers: 0
edited January 2012 in Bug reports
When using ColReorder to change the order of columns, it messes up ColVis due to its storing indices instead of column names. This affects exclusion (aiExclude option) but also restoring original visibility (via the internal aiOriginal).

The exclude part was fixed by stefan (http://datatables.net/forums/discussion/comment/15964#Comment_15964) although I have tweaked it a little to refer to columns by mDataProp instead of sTitle. Additionally, I have changed the aiExclude option to asExclude since it now takes an array of strings. Another benefit of this is that the exclude parameter is now independent of column order and indices.

[code]
"_fnAddButtons": function ()
{
var
// ...
sExclude = "|"+this.s.asExclude.join('|')+"|";

for ( var i=0, iLen=this.s.dt.aoColumns.length ; i

Replies

  • richardprichardp Posts: 10Questions: 0Answers: 0
    A bit of extra functionality I also added allows the "restore" button in ColVis to also reset the columns in ColReorder. To do this, simply add the following option:
    [code]
    this.s = {
    // ...

    /**
    * Restore button resets ColReorder's column order too
    * @property bRestoreColOrder
    * @type Boolean
    * @default false
    */
    "bRestoreColOrder": false,

    // ...
    }

    // ...

    "_fnApplyCustomisation": function ()
    {
    // ...
    if ( typeof oConfig.bRestoreColOrder != 'undefined' )
    {
    this.s.bRestoreColOrder = oConfig.bRestoreColOrder;
    }
    // ...
    },
    [/code]

    And then update the restore button's click event:
    [code]
    "_fnDomRestoreButton": function ()
    {
    // ...

    $(nButton).click( function (e) {
    // ColReorder triggers a refresh of ColVis's buttons but they don't get restyled
    // until the next time it is shown so it will look ugly if not hidden now
    that._fnCollectionHide();

    for ( var i=0, iLen=that.s.dt.aoColumns.length ; i
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Brilliant! Thanks very much for your post. I'll look at integrating your fixes shortly.

    Allan
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Been thinking about this over the weekend - I don't really want to have to mandate the use of mDataProp or sName for columns when using these plug-ins (although it would make a lot of things much easier!), so I'm not sure about committed these changes into the trunk at the moment.

    For now, what I think would work is simply initialising the ColReorder plug-in after ColVis, since ColReorder will store the state in the original column order, when when ColVis is initialised first, it will get the order expected and ColReorder can shunt the values around as needed after that.

    I'll keep my thinking cap on about how to make this a lot more flexible though! It could be that an "original order" index gets added to DataTables core.

    Regards,
    Allan
  • richardprichardp Posts: 10Questions: 0Answers: 0
    Why not simply add the asExclude option in addition to aiExclude, where asExclude takes precedence. If only aiExclude is provided, it populates the asExclude property itself upon instantiation using the original column indices.

    Something like this perhaps? (untested)
    [code]
    "_fnAddButtons": function ()
    {
    // Populate asExclude from aiExclude using current column order
    if (this.s.aiExclude.length && !this.s.asExclude.length)
    for (var i=0; i < this.s.aiExclude.length; i++)
    this.s.asExclude[i] = this.s.dt.aoColumns[this.s.aiExclude[i]].mDataProp;

    // ...
    }
    [/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Yes - that is certainly an option. There is an argument that for this kind of thing that sName / mDataProp must always be defined - a possibility for future versions...

    Allan
This discussion has been closed.