我想在jQuery Datatable中添加row.add个条目,并在添加时(或之后,如果添加时不可能),根据内容更改outer <td>的类.

var mTableROH   = $('.mm-table.ROH').DataTable(
    {
        dom: 'Bfrtip',
        "stateSave": true,

    }
);

for(let i = 0; i < 10; i++)
{
  mTableROH.row.add(
          [
             (Math.random() * 10).toFixed(2),
             (Math.random() * 10).toFixed(2),
             (Math.random() * 10).toFixed(2),
             (Math.random() * 10).toFixed(2),
             (Math.random() * 10).toFixed(2),
             (Math.random() * 10).toFixed(2),
             (Math.random() * 10).toFixed(2),
             (Math.random() * 10).toFixed(2),
          ]
  );
 
}
 mTableROH.columns.adjust().draw();

    
.color-red{
  color: red;
}

color-green{
  color: green;
}
<link rel="stylesheet" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css">

                            <table id="mm-table-roh" class="table table-striped table-bordered mm-table ROH">
                                <thead>                             
                                    <tr>
                                        <th>Value 1</th>
                                        <th>Value 2</th>
                                        <th>Value 3</th>
                                        <th>Value 4</th>
                                        <th>Value 5</th>
                                        <th>Value 6</th>
                                        <th>Value 7</th>
                                        <th>Value 8</th>
        
                                    </tr>
                                </thead>
                                <tbody>
                                
                                </tbody>

                            </table>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>

如何在这样添加新行的同时添加这些类?

推荐答案

所以在读了你的 comments 后,我做了更多的研究,我认为这是更好的,因为我相信你关心的是表现:

var mTableROH = $('.mm-table.ROH').DataTable({
    dom: 'Bfrtip',
    "stateSave": true,
    "createdRow": function(row, data, dataIndex) { // add a callback function
        const length = row.children.length;
        for (let i = 0; i < length; i++) {
            row.children[i].className = data[i] > 5 ? "color-green" : "color-red";
        }
    }
});

// Below remains the same as my previous answer
for (let i = 0; i < 10; i++) {
    let randomNumbers = [];
    for (let j = 0; j < 8; j++) {
        randomNumbers.push((Math.random() * 10).toFixed(2));
    }
    mTableROH.row.add(randomNumbers)
}
mTableROH.columns.adjust().draw();

基本上,我们可以在.DataTable()的对象参数中添加一个createdRow(https://datatables.net/reference/option/createdRow)属性,该参数在a TR element is created (and all TD child elements have been inserted)时执行

另一个变化是,这次它替换了td个元素的所有类,而不是添加一个类,如果你只想添加你可以替换的类的话

row.children[i].className = data[i] > 5 ? "color-green" : "color-red";

具有

row.children[i].classList.add(data[i] > 5 ? "color-green" : "color-red");

Javascript相关问答推荐

Flutter:显示PDF和视频,但阻止下载

不渲染具有HTML参数的React元素

容器如何更改默认插槽中子项的显示?

我的glb文件没有加载到我的three.js文件中

react 路由加载程序行为

使用AJX发送表单后,$_Post看起来为空

没有输出到带有chrome.Devtools扩展的控制台

无法访问Vue 3深度监视器中对象数组的特定对象值'

如何从Intl.DateTimeFormat中仅获取时区名称?

在Three JS中看不到补间不透明度更改效果

JS—删除对象数组中对象的子对象

对网格项目进行垂直排序不起作用

在forEach循环中获取目标而不是父对象的属性

WP Bootstrap NavWaker:下拉菜单一次打开所有下拉菜单

如何使用TypeScrip设置唯一属性?

使用带有HostBinding的Angular 信号来更新样式?

如何在FastAPI中为通过file:/URL加载的本地HTML文件启用CORS?

FireBase云函数-函数外部的ENV变量

如何在一个对象Java脚本中获取不同键的重复值?

如何使本地html页面在重新加载时保持当前可隐藏部分的打开状态?