1

I am struggling trying to find the right method to do this. Basically, I have an array of id values that correspond to which rows have been selected in my table. To construct this list, I use the following code. this.options.ajaxId is the key that accesses my Id value in the data object passed to the table.

this.getRowData(target)[this.options.ajaxId]

where my getRowData function is:

getRowData: function (HTMLrow) {
    return this.dataTable.row(HTMLrow).data();
},

This works great, but then I am stumped on my next step which is re-selecting the correct rows when the table is re-drawn via paging, sorting, or searching. My plan was to cycle through the ID's and find which table row corresponded to that ID value, but I cannot find a function to input a key value search pair and return the html row. Something like the following is what I was thinking,

this.dataTable.findRow( key, value );
// then my usage would be the following:
var that = this;
_.each(this.selectedList, function (id) {
    var row = that.dataTable.findRow( that.options.ajaxId, id );
    // code to select the row
});

I haven't written it yet, but I know I can cycle through each of the rows, get the data for that row, and check it against what I am looking for, but in cases where the user is viewing 100 rows and has only one selection I would like to avoid that.

Any insight?

Thanks

4
  • Hmm thanks for the tip, that does help with retaining the paging etc, unfortunately I have added the selection mechanism manually so it isn't included in this feature Commented Sep 22, 2015 at 20:31
  • @davidkonrad I never professed to be expert at it...how does your snotty abrasive comment bring anything constructive to the situation? I made a casual suggestion was all. Does trying to help merit nasty rude ignorant comments? get a grip Commented Sep 22, 2015 at 20:36
  • So provide a solution that works , no need for cryptic remarks. I am never one to not say i was wrong Commented Sep 22, 2015 at 20:38
  • Did you have a chance to try the code in my answer? Commented Oct 1, 2015 at 14:55

1 Answer 1

1

SOLUTION #1

You can use the following code to locate and highlight rows based on row IDs if row ID is stored in one of the fields.

// Index of column containing IDs
var colIdIndex = 0;

// List of row IDs
var rowIds = ['2', '4', '6'];

// Find indexes of rows which have IDs in the desired column
var rowIndexes = table.rows().eq(0).filter( function (rowIdx) {
    return ($.inArray(table.cell( rowIdx, colIdIndex ).data(), rowIds) !== -1) 
        ? true 
        : false;
});    

// Select rows based on array of found row indexes
table.rows(rowIndexes)
    .nodes()
    .to$()
    .addClass('selected');

See filter() API method for more details.

Please note that this method will work for client-side processing mode only.

DEMO

See this jsFiddle for code and demonstration.

SOLUTION #2

Alternative approach that would work both in client-side and server-side processing modes would be to use createdRow callback.

For example:

// Index of column containing IDs
var colIdIndex = 0;

// List of row IDs
var rowIds = ['2', '4', '6'];

var table = $('#example').DataTable({
    createdRow: function( row, data, dataIndex ) {
        if ( $.inArray(data[colIdIndex], rowIds) !== -1) {
            $(row).addClass('selected');
        }
    }        
});

DEMO

See this jsFiddle for code and demonstration.

Sign up to request clarification or add additional context in comments.

2 Comments

awesome you actually did answer :)
@davidkonrad, I couldn't pass it after you mentioned that I will answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.