ColReorder(WithResize) and Column... index...? Question

ColReorder(WithResize) and Column... index...? Question

GregPGregP Posts: 487Questions: 8Answers: 0
edited September 2011 in Plug-ins
Let me start off with the code. I'll get to the question sooner or later. ;-)

To initialize my DataTable, I build out a table in HTML with the number of visible columns I will have. However, I also have a bunch of hidden columns for most tables. So, in the initialization object for aoColumns I might have something like this:

[code]
"aoColumns": [
/* Source Name */ {
"mDataProp": "sourceName"
},
/* Destination Name */ {
"mDataProp": "destName"
},
/* Username */ {
"mDataProp": "username"
}
]
[/code]

These 3 columns match (in order) the visible columns created through HTML. They are all sortable. I then do some stuff in the fnRowCallback that makes use of the hidden columns I'm sending. At the beginning of the callback, I declare some variables to use:

[code]
/* Create some variables which will be used by utility functions. */
var
sourceLink = aData['sourceLink'],
destLink = aData['destLink'],
sourceType = aData['sourceType'],
destType = aData['destType'];

/* Indexes (starting from zero) of visible columns used in additional
* processing below */
var
visSource = 0,
visDest = 1;
[/code]

Here's an example of what we might do with these variables:

[code]
if (sourceLink.length > 0) $('td:eq('+visSource+')', nRow).addClass('clickable').attr('href', sourceLink);
[/code]

Here's the problem:

"Hard-coding" the (visible)columns that are going to be modified isn't truly ideal, but it made sense for performance reasons when we weren't considering making the columns re-orderable. "Source" was always going to be column 0, and "Destination" was always going to be column 1. With reordering, that's not the case. So if you were to reorder your columns, in the example above the 'clickable' class would still be added to the first column. Not the intended result!

Is there an object that I'm not aware of that can give me the visible index of any given mDataProp easily? (ie. "the visible column index for the column with mDataProp "sourceName"? It should be fairly easy to do with some jQuery, but it just seems kludgey because I would have to set classes on each cell, then find the index of the cell with the particular class, etc, etc... I can picture how to do it, but it just seems clumsy to do it that way.

Possibly because of these mismatched columns (the data column vs. the hard-coded visible column) I am also seeing artifacts if I re-order and then sort (data ends up in the wrong columns). I haven't gone down that road of exploration too much yet, since fixing the above may simultaneously fix this.

So, any ideas on how to more dynamically declare my "visSource" and "visDest" type variables?

Replies

  • GregPGregP Posts: 487Questions: 8Answers: 0
    Exploring the DOM, I see something about oApi._fnVisibleToColumnIndex that sounds promising.
This discussion has been closed.