我有一个$datatables$datatables row.add()API一起工作,所以我可以实现我想要的。现在,我有一个对象e.data将使用row.add()作为新行添加。一切都很好,直到有一天我需要添加td class="hidden" td。它成功地增加了,但一个邪恶的情况来了。td class="hidden"td没有发生,tdtd发生。我的数百万美元的问题是如何在使用datatable添加时保留td的类。
按钮html属性添加成功。tds属性?我不知道为什么它不出现。
非常感谢!
下面是代码

if (e.success == "TrueAdd") {
                var table = $('#product-table').DataTable();
                var arr = $.map(e.data, function (value, index) { return [value]; });

                var newValue = [arr[0], '<td style="visibility:hidden">' + arr[1] + '</td>', arr[2], arr[3], arr[4], arr[5], arr[6], arr[7], arr[8],
                '<button type="button" class="btn-edit btn btn-info btn-default">Edit</button>',
                '<button type="button" class="btn-delete btn btn-info btn-default">Delete</button>'];
                table.row.add(newValue).draw(false);
            }

 <table id="product-table" class="table">
            <thead>
                <tr>
                    <th>Product Id</th>
                    <th class="hidden">CategoryId</th>
                    <th>Category</th>
                    <th>Manufacturer</th>
                    <th>Name</th>
                    <th>Description</th>
                    <th>Model</th>
                    <th>Released Date</th>
                    <th>Released Year</th>
                    <th>Action</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.ProductList)
                {
                    <tr>
                        <td>@item.Id</td>
                        <td>@item.CategoryId</td>
                        <td>@item.CategoryDescription</td>
                        <td>@item.ManufacturerId</td>
                        <td>@item.Name</td>
                        <td>@item.Description</td>
                        <td>@item.Model</td>
                        <td>@item.ReleasedDate</td>
                        <td>@item.ReleasedYear</td>
                        <td><button type="button" class="btn-edit btn btn-info btn-default">Edit</button></td>
                        <td><button type="button" class="btn-delete btn btn-info btn-default">Delete</button></td>
                    </tr>
                }
            </tbody>
        </table>

最佳答案

我找到了答案!!!!! 我补充说

"columnDefs": [
            { className: "hide_column", "targets": [1] }
            ]

然后我在我的项目中添加了一个css文件并添加了这个
.hide_column{
display : none;
}

然后隐藏列现在在DOM中可见。
感谢stackoverflowjQuery DataTables hide column without removing it from DOM的丹尼尔

07-24 09:44
查看更多