简体   繁体   English

如何使用渲染功能在数据表中添加json数组

[英]how to append json array in datatable using render function

I want to append json array into one popover attribute called data-content. 我想将json数组附加到一个名为data-content的popover属性中。 I am using jQuery DataTables plugin to perform UI functionalities. 我正在使用jQuery DataTables插件执行UI功能。 Using data variable table is populated with the data. 使用data变量表填充数据。

How could I insert titleDescription array from data variable into the attribute name called as data-content inside a tag in js , check my fiddle and go to datatable function there inside columnDefs there is render function. 我怎么能插入titleDescription从数组data变量成称为属性名称data-content里面a在JS代码,请我的小提琴,到数据表功能有内部columnDefsrender功能。 In that function I have return and append a tag, there only I have to append titleDescription array. 在该函数中,我具有return并附加a标签,仅需附加titleDescription数组。

Check this JSFiddle for demonstration. 检查此JSFiddle进行演示。

You can use the row attribute, and have the data in a non visible column. 您可以使用row属性,并将数据放在不可见的列中。 Check the example https://datatables.net/examples/advanced_init/column_render.html 查看示例https://datatables.net/examples/advanced_init/column_render.html

$(document).ready(function() {
    $('#example').DataTable( {
        "columnDefs": [
            {
                // The `data` parameter refers to the data for the cell (defined by the
                // `data` option, which defaults to the column being worked with, in
                // this case `data: 0`.
                "render": function ( data, type, row ) {
                    return data +' ('+ row[3]+')';
                },
                "targets": 0
            },
            { "visible": false,  "targets": [ 3 ] }
        ]
    } );
} );

That would do the trick. 那可以解决问题。

You just need to add another column at the last of each row then you can access it from row like this 您只需要在每行的最后添加另一列,然后就可以像这样从行中访问它

render : function(data, type, row) {      
   return '<span class="favlist-icon"></span><span class="immediate-attention-icon"> </span> <span class="docx-name-list-link"> <a class="apopoverdata" href="#" data-content="'+row[4]+'" rel="popover" >'+data+'</a></span>';                  
}

Here is working fiddle 这是工作提琴

https://jsfiddle.net/2cn4b8bz/4/ https://jsfiddle.net/2cn4b8bz/4/

I'm not sure what library you were using for your pop over so I used tooltipster seeing as you had jQuery already for the DataTable. 我不确定您要使用哪个库进行弹出操作,因此我使用了工具提示来查看数据表,因为您已经有了jQuery。 Your data didn't have everything you needed either so I've altered it a wee bit: 您的数据也没有您需要的所有内容,因此我做了一点改动:

$(function() {
  $('#documentListing-data').DataTable({
    data: json.documentAll.document,
    pageLength: 10,
    columns:[
        {
        "data": "documentTitle",
        "title": "Name",
        "render": (data, type, row) => '<a href="' + row.documentUrl + '" class="tooltip" title="' + row.titleDescription + '">' + data + '</a>',
      },
        {
        "data": "category",
        "title":"Category",
        "render": (data, type, row) => data.map((k, v) => k.trim()).join(", "),
      },
        {
        "data": "publishedDate",
        "title":"Published Date"
      },
        {
        "data": "lastUpdatedDate",
        "title": "Last Updated Date"
      },
    ],
    "drawCallback": function(settings) {
      $('.tooltip').tooltipster();
    }
  });
});

Working JSFiddle . 工作中的JSFiddle Hope that helps! 希望有帮助!

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

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