简体   繁体   English

将数据属性放在DataTables 1.10上的行add中

[英]put data attribute on row add on DataTables 1.10

I dynamically add new row on DataTables 1.10.2 using the table.row.add() method using this code: 我使用table.row.add()方法使用以下代码在DataTables 1.10.2上动态添加新行:

table.row.add([
    '',
    name,
    target_l,
    details,
    panel.html()    
]).draw();

I produced this mark-up: 我制作了这个加价:

<tr role="row" class="odd">
    <th>1 .</th>
    <td class="sorting_1">ID Fee</td>
    <td>All students</td>
    <td></td>
    <td>
        <button class="btn btn-null btn-xs" onclick="_remove(59, 'ID Fee')">
            <span class="fui-cross"></span>
        </button>
        <button class="btn btn-null btn-xs" onclick="_edit(59, 'ID Fee', '', '')">
            <span class="icon-pencil"></span>
        </button>
    </td>
</tr>

What I want to do is to add a data-id (and other data) attribute to the newly added tr tag (on or after row insert) and make it something like this: 我想要做的是将data-id (和其他数据) 属性添加到新添加的tr标签 (在插入行中或之后),并使其像这样:

<tr data-id="59" role="row" class="odd">

I've managed to get the index of the newly added row using the code and it returns the last row index : 我已经设法使用代码获取新添加的行的索引,并返回最后一行索引

var i = table.row.add([
    '',
    name,
    target_l,
    details,
    panel.html()    
]).index();

And also tried to perform the following to add the data-id attribute using that index: 并尝试执行以下操作以使用索引添加data-id属性

var id = $("#department_id").val();
table.row(i).attr("data-id", id);
// table.row(i).data("id", id);
// I wanted to try this but there is also a method called data() already in
// DataTables so it will not work like in JQuery.

I'm new to DataTables and already scrolled its sourcecode, red the comments. 我是DataTables的新手,已经滚动了它的源代码,红色的评论。 Though not good at understanding its functions that begins with _fn*() . 虽然不善于理解以_fn*()开头的函数。 If there's any other way without relying on these _fn*() functions, thanks! 如果没有任何其他方式而不依赖于这些_fn*()函数,谢谢!

You can use rows().nodes() API function, see below: 您可以使用rows()。nodes() API函数,如下所示:

var i = table.row.add([
    '',
    name,
    target_l,
    details,
    panel.html()    
]).index();

var id = $("#department_id").val();
table.rows(i).nodes().to$().attr("data-id", id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM