添加行运行时如何在数据表中为td添加类?

时间:2019-02-25 07:53:09

标签: javascript jquery html datatables bootstrap-4

我想通过ajax获取列表数据时将类添加到td

  

默认我的代码html

<tr>
                    <td>@fisrt.System_Code.Code</td>
                    <td>@fisrt.System_Code.Caption</td>
                    <td class="text-success">@string.Format("{0:N0}", maden)</td>
                    <td class="text-danger">@string.Format("{0:N0}", den)</td>
                    <td class="text-warning nrt-bd" dir="ltr">@string.Format("{0:N0}", maden - den)</td>
                </tr>
  

当我想在过滤后获取列表数据时,我不知道如何添加类

$.ajax({
                type: 'GET',
                url: '/Record_Professor/Search_Budget/',
                data: { from: from, to: to },
                success: function (results) {
                    results.forEach(function (item) {
                        $('#table_id').dataTable().fnAddData([
                            item.Code,
                            item.Caption,
                            item.Maden,
                            item.Daeen,
                            item.Balance
                        ]);
                    });
                },
                error: function (error) {
                    alert('error; ' + eval(error));
                }
            });

1 个答案:

答案 0 :(得分:0)

“ className”:“ Classname”,用于在运行时添加类

$('#table_id').DataTable({
  data: data2,
  "autoWidth": false,
  deferRender: true,
  pageLength: 10,
  responsive: true,
  scrollCollapse: true,
  select: {
    style: 'os',
    selector: 'td:first-child'
  },
  "lengthMenu": [
    [10, 25, 50, -1],
    [10, 25, 50, "All"]
  ],
  "columnDefs": [{
    "className": "Classname",
    "targets": [5,2,3,4,9,]
  }]
});

$('#example').dataTable( {
  "columnDefs": [
    { className: "my_class", "targets": [ 3 ] }
  ]
} );