【问题标题】:How do I perform a check on each value of a particular column in jQuery datatable?如何检查 jQuery 数据表中特定列的每个值?
【发布时间】:2019-08-26 12:36:09
【问题描述】:

我正在为表格使用 jquery 数据表插件。我想对我选择的列中的每个值进行检查。我该怎么做?

我已经尝试过了,但它会检查数据表中存在的所有值。有没有办法对特定列中的值而不是整个数据表值执行此操作?

这是 HTML

<table id="example">
    <thead>
        <tr>
            <th>id</th>
            <th>Currency</th>
            <th>Lots</th>
        </tr>
    </thead>
</table>

这里是 JavaScript

var table = $('#example').DataTable({
  "bPaginate": false,
  "bInfo" : false,
  "columns": [
      {"data": "id"},
      {"data": "currency"},
      {"data": "lots"}
  ]
});
table.cells().every( function () {
    if ( this.data() > 5000 ) {
        $(this.node()).addClass( 'warning' );
    }
} );

我希望批次列中大于 5000 的所有值都具有“警告”类。

【问题讨论】:

  • 让它使用这个 ---------- table.column(0).cells( function (idx, data, node) { return data > 5000 ? true : false ; }).nodes().to$().addClass('警告');
  • 你已经走了很长一段路。选项columns.createdCell 是在表格渲染时实现这一权利的一种自然且简单得多的方法。
  • @YevgenGorbunkov 谢谢伙计,这要简单得多。

标签: javascript jquery html css datatables


【解决方案1】:

我认为您可以使用 column().nodes(),如文档中所述:

https://datatables.net/reference/api/column().nodes()

table.column(1).nodes().each( function () {
    if ( this.data() > 5000 ) {
        $(this.node()).addClass( 'warning' );
    }
} );

【讨论】:

    猜你喜欢
    • 2021-10-20
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 2011-12-17
    • 2018-08-03
    相关资源
    最近更新 更多