我想在JSON数据的DataTables中的记录字段中创建链接

Ada*_*dam 15 json datatables

我正在创建一个dataTables表,用作生成漫画的网站页面的存档.在那个档案页面上,我想让漫画的标题链接到该漫画的页面.

初始化:

    <script type="text/javascript" charset="utf-8">
            $(document).ready(function() {
            $('#example').dataTable( {
            "bProcessing": true,
            "sAjaxSource": "archive/archive.txt"
    } ); 
} );

        </script>
Run Code Online (Sandbox Code Playgroud)

HTML:

<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
    <thead>
        <tr>
            <th width="20%">Author</th>

            <th width="25%">Title</th>
            <th width="25%">Episode</th>
            <th width="15%">Date</th>
            <th width="15%">Tags</th>
        </tr>
    </thead>
    <tbody>


    </tbody>

</table>
Run Code Online (Sandbox Code Playgroud)

JSON数据:

{ "aaData": [
    ["Bob","Title One","Episode 1","9/30/2010","tag1,tag2,tag3"],
    ["Bob","Title One","Episode 2","10/2/2010","tag1,tag2,tag3"],
    ["Bob","Title One","Episode 3","10/4/2010","tag1,tag2,tag3"],
    ["Bob","Title Four","Episode 1","10/8/2010","tag1,tag2,tag3"],
    ["Bob","Title Five","Episode 1","10/11/2010","tag1,tag2,tag3"],
    ["Bob","Title Six","Episode 1","10/12/2010","tag1,tag2,tag3"],
    ["Kevin","Title Seven","Episode 1","10/15/2010","tag1,tag2,tag3"],
    ["Kevin","Title Eight","Episode 1","10/17/2010","tag1,tag2,tag3"],
    ["Kevin","Title Eight","Episode 2","10/20/2010","tag1,tag2,tag3"],
    ["Kevin","Title Ten","Episode 1","10/22/2010","tag1,tag2,tag3"],
    ["Kevin","Title Eleven","Episode 1","10/23/2010","tag1,tag2,tag3"],
    ["Kevin","Title Twelve","Episode 1","10/24/2010","tag1,tag2,tag3"]
] }
Run Code Online (Sandbox Code Playgroud)

其中"Title One"或"Title Four"等将是该漫画页面的链接.诚然,我没有太多关于使用dataTables的chops的方式,所以如果你可能在你的解决方案中明确,那将是非常值得赞赏的.

a d*_*ren 24

您还可以使用以下mRender功能aoColumnDefs:

$('#example').dataTable({
  "bProcessing": true,
  "bServerSide": true,
  "sAjaxSource": "archive/archive.txt",
  "aoColumnDefs": [            
     {
       "aTargets": [ 2 ], // Column to target
       "mRender": function ( data, type, full ) {
         // 'full' is the row's data object, and 'data' is this column's data
         // e.g. 'full[0]' is the comic id, and 'data' is the comic title
         return '<a href="/comics/' + full[0] + '">' + data + '</a>';
       }
     }
   ]
});
Run Code Online (Sandbox Code Playgroud)

这更加明确,可能更易于维护,因为您可以指定单个列在列级别的呈现方式,而不是通过行级别的DOM选择它们,这有助于您稍后添加列.

  • 谢谢!这非常有用,但对我来说完全[0]没有用.我正在使用DataTables.net 1.10.但是,您可以使用点表示法(例如full.Id)或括号表示法(full ["Id"])访问属性. (3认同)

jcu*_*bic 21

你应该使用fnRowCallback选项参见文档.

$('#example').dataTable({
     "bProcessing": true,
     "bServerSide": true,
     "sAjaxSource": "archive/archive.txt",
     "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
            $('td:eq(2)', nRow).html('<a href="view.php?comic=' + aData[2] + '">' +
                aData[2] + '</a>');
            return nRow;
        },
});
Run Code Online (Sandbox Code Playgroud)

  • 在较新版本的DataTables中,事件被称为:`createdRow`,这是它的回调函数:`createdRow:function(row,data,index){..` (2认同)