3

Is there a way to lookup a row in jQuery DataTables based upon column values? For example, can I determine which row contains a value of "foo" in column 1 and a value of "bar" in column 2? I would like to programmatically determine which row contains these values. Is the best solution to simply keep a separate data structure that ties the values and the row indexes together? I am wanting to delete a table row with columns containing the given values.

I am using DataTables 1.8.0 and jQuery 1.5.1.

3 Answers 3

6

I would get the data with fnGetData, loop through the rows and use $.inArray:

function getRow(table_id, arg1, arg2) {
    var oTable = $('#' + table_id).dataTable(),
        data = oTable.fnGetData(),
        row, i, l = data.length;

    for ( i = 0; i < l; i++ ) {
        row = data[i];

        // columns to search are hard-coded, but you could easily pass this in as well
        if ( $.inArray(arg1, row) == 0 && $.inArray(arg2, row) == 1 ) {
            return $('#' + table_id + ' tr').eq(i);
        }
    }
    return false;
}

$row = getRow('table_id', 'foo', 'bar');

NOTE: Untested

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

Comments

2

Try using this selector:

$("tr td:nth-child(2):contains('value')").siblings(":contains('test')")

Take a look at this fiddle:

http://jsfiddle.net/B4wMp/8/

DataTable just gets rendered as a plain old html table.

1 Comment

Nice and concise, though contains will return true on partial values when you don't want it to – returning true for new york when you've asked for new. To target a specific column, you'll also have to add ...siblings.eq(column_n)..., making sure you choose the right one from the array based on the new indices that result from excluding the originally selected element.
0

I had a similar requirement. I'm using Datatable as well, but with a version >=1.10. fnGetData() is now missing.

Here is an adaptation of @glortho 's answer:

function getRow(tableId, propName, propValue) {
        var oTable = $('#' + tableId).DataTable(),
            data = oTable.rows().data(),
            row, i, l = data.length;

        for ( i = 0; i < l; i++ ) {
            row = data[i];


            if (row[propName] == propValue) {

                return $(`#${tableId}  tr`).eq(i+1);
            }
        }


        return false;
    }

Comments

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.