数据表查找字符串并添加类

标签 datatables find addclass

我尝试在表中查找字符串并将类添加到该行。但这段代码不起作用。只是什么也没发生。这是我的代码,后面我调用 myTable() 函数:

function myTable() {
    var selectDateVar = $('#selectDate').val();

        var table = $('#example').DataTable( { // Таблица
            "processing": true,
            "serverSide": true,
            "deferRender": true,
            "bDestroy": true,

            "sAjaxSource": "server_processing.php?data=30/09/2015",
            "order": [[ 2, "desc" ]],
            initComplete: function(){
                var api = this.api();

                new $.fn.dataTable.Buttons(api, {
                    buttons: [
                        {
                        extend: 'print',
                        text: 'Принтиране',
                        'className': 'btn-lg btn btn-warning printBTN',
                        },
                    ]
                });

                api.buttons().container().appendTo( '.printButton' );  
            }

        });

        var indexes = table.rows().eq( 0 ).filter( function (rowIdx) {
            return table.cell( rowIdx, 3 ).data() === '180' ? true : false;
        } );
        table.rows( indexes ).nodes().to$().addClass( 'highlight' );
    }

我的 table :

enter image description here

我使用这个例子https://datatables.net/reference/type/row-selector

最佳答案

function myTable() {
    var selectDateVar = $('#selectDate').val();

    var table = $('#example').DataTable( { // Таблица
        "processing": true,
        "serverSide": true,
        "deferRender": true,
        "bDestroy": true,

        "sAjaxSource": "server_processing.php?data=30/09/2015",
        "order": [[ 2, "desc" ]],
        initComplete: function(){
            var api = this.api();
            new $.fn.dataTable.Buttons(api, {
                buttons: [
                    {
                    extend: 'print',
                    text: 'Принтиране',
                    'className': 'btn-lg btn btn-warning printBTN',
                    },
                ]
            });

            api.buttons().container().appendTo( '.printButton' );  

            //filtering code should be inside of initComplete function
            //but in your case an empty table is filtered
            var indexes = table.rows().eq( 0 ).filter( function (rowIdx) {
                 return table.cell( rowIdx, 3 ).data() === '180' ? true : false;
            } );

            table.rows( indexes ).nodes().to$().addClass( 'highlight' );
        }

    });
} 

您需要在数据加载时调用代码。目前,您在表填充服务器数据之前调用它。只需将您的代码示例添加到 initComplete 函数即可。 initComplete 将在 Ajax 数据加载后调用。

第二个问题的答案:如果您需要跨多个列进行搜索,只需添加以下代码:

var indexes = table.rows().eq( 0 ).filter( function (rowIdx) {
    return table.cell( rowIdx, 3 ).data() === '180' &&
           table.cell( rowIdx, 0 ).data() === '521' ? true : false;
} );

关于数据表查找字符串并添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35251449/

相关文章:

jquery - 使数据表在更新后保留页面并在表刷新时运行外部函数

javascript - 使用 jquery 动态添加的 css 打印 html 表

jquery - 使用 jQuery.attr() 设置属性时,浏览器不会从 css 更新样式

jquery - 优化jquery添加和删除类

bash - 复制不带文件的目录结构

jquery - 在 jquery 中使用 attr() 相对于 addClass 的优点

javascript - Bootstrap 3中Datatables插件显示格式的自定义

javascript - Jquery - Bootstrap DataTable 刷新问题 DATA

ruby-on-rails - 带有rails的mongodb,通过数组中的id查找

linux - 如何在没有绝对地址的情况下在 Linux 中查找文件?