开发者

How can I retrieve a row based on column values using jQuery DataTables?

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 t开发者_运维百科he 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.


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


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.


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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜