【问题标题】:jQuery datatables row.add is not adding td attribute hiddenjQuery datatables row.add 没有添加隐藏的 td 属性
【发布时间】:2017-07-23 14:49:27
【问题描述】:

我有一个 $datatables$datatables row.add() API 一起工作,所以我可以实现我想要的。现在,我有一个对象e.data,它将使用row.add() 作为新行添加。一切正常,直到有一天我需要添加td class="hidden" td。它添加成功,但邪恶的情况来了。 td class="hidden"td 没有发生,tdtd 发生了。我的数百万美元的问题是如何在使用数据表添加 td 类时保留它。

Buttons 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>

【问题讨论】:

  • 请告诉我您是否正在研究解决方案或其他问题
  • 也许使用 DataTable 来创建你的按钮而不是硬编码它们,它们添加你的数组但没有标记?
  • 按钮不是问题, 是问题所在,每次我附加一些东西时我都需要隐藏 td,但是当我附加一个隐藏的 span 类时会显示一个空格新的 td。

标签: javascript jquery html css datatables


【解决方案1】:

我找到了答案!!!!我加了

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

然后我在我的项目中添加了一个css文件并添加了这个

.hide_column{
display : none;
}

那么隐藏列现在在 DOM 中可见。

感谢来自 stackoverflow 的 Daniel jQuery DataTables hide column without removing it from DOM

【讨论】:

    【解决方案2】:

    这种方法应该可以满足您的需要:

    let num = 4,
        table = $("#product-table").DataTable({
            columnDefs: [{
                "targets": [1],
                "visible": false
            }, {
                "targets": [9],
                "render": () => $("<button></button>", {
                    "type": "button",
                    "class": "btn-edit btn btn-info btn-default",
                    "text": "Edit"
                }).prop("outerHTML")
            }, {
                "targets": [10],
                "render": () => $("<button></button>", {
                    "type": "button",
                    "class": "btn-delete btn btn-info btn-default",
                    "text": "Delete"
                }).prop("outerHTML")
            }]
        });
    $("#AddRow").on("click", () => {
        let newRow = [
            num + " Id", 
            num + " CategoryId", 
            num + " CategoryDescription", 
            num + " ManufacturerId", 
            num + " Name", 
            num + " Description", 
            num + " Model", 
            num + " ReleasedDate", 
            num + " ReleasedYear", 
            num, 
            num];
        num++;
        table.row.add(newRow).draw();
    });
    

    它可以让 DataTables 完成渲染和隐藏数据的繁重工作。希望对您有所帮助!

    您显然需要更改触发添加行的内容;-)

    工作example

    【讨论】:

    • 谢谢,我试试这个
    • 嗨,thnks 令人讨厌的鼠标,从一开始的问题仍然存在。使用 firefox 的调试模式下缺少类别 ID。我们应该能够正确地看到它。我需要那个 id,所以我可以用它做别的事。
    • 我不确定我是否理解您的要求。单击按钮时,您想访问相关行中的所有数据吗?通过在行上添加事件侦听器并使用该行中的数据,这应该很容易,检查更新的 JSFiddle。如果这不是您所追求的,请进一步解释。
    • 添加时隐藏列,这是我主要关心的问题。我希望列在查看时不可见,同时在 Inspect 元素或 Inspector 中可见,问题是您可以在查看时将其隐藏,但在检查元素时 td 不存在。您选择隐藏的列
    • datatable 应该让事情变得更容易.. 这维护起来太复杂了.. 下一个开发人员不知道为什么所有这些代码都被写成“只是隐藏一列”
    猜你喜欢
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2015-04-10
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 2019-05-21
    相关资源
    最近更新 更多