【问题标题】:How can I create a link using the data source in Datatables Row Grouping?如何使用 Datatables Row Grouping 中的数据源创建链接?
【发布时间】:2015-09-07 20:34:51
【问题描述】:

我正在使用带有行分组高级初始化的 Jquery 的 Datatables 1.10 插件:https://datatables.net/examples/advanced_init/row_grouping.html,我想添加一个从数据库到分组列的链接,在示例中链接将是 Edinburgh、伦敦

在我使用 column.render:https://datatables.net/reference/option/columns.render 添加链接之前,它正在工作。正如您在代码中看到的,我的目标是第 1 列 (工作),并希望定位第 0 列,即分组的隐藏列 (不工作)。。 p>

第 0 列被隐藏并包含组名。 第 3 列被隐藏并包含链接。

JS代码如下:

$(document).ready(function() {
var dataTable = $('#example').DataTable({
"processing": true,
"serverSide": true,
"ajax":{
        url :"example.php", // json datasource
        type: "post",  // method  , by default get
            error: function(){  // error handling
              $(".example-error").html("");
              $("#example").append('<tbody class="example-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
              $("#example_processing").css("display","none");
            }
          },

          "columnDefs": [
          { "visible": false, "targets": [ 0, 3] },
          { "width": "50%", "targets": [ 1, 2 ] },
          { "orderable": false, "targets": [ 1, 2 ] },
          { "targets": 1,
          "data": null,
          "render": function ( data ) {
            return '<a href=//'+data[ 3 ]+' target="_blank">'+data[ 1 ]+'</a>';
          }}
          ],

          "order": [[ 0, 'asc' ]],
          "displayLength": 25,
          "drawCallback": function ( settings ) {
            var api = this.api();
            var rows = api.rows( {page:'current'} ).nodes();
            var last = null;

            api.column(0, {page:'current'} ).data().each( function ( group, i ) {
              if ( last !== group ) {
                $(rows).eq( i ).before(
                  '<tr class="group"><td colspan="5">'+group+'</td></tr>'
                  );

                last = group;
              }
            } );
          }
        } );
        } );

谢谢!

【问题讨论】:

    标签: javascript jquery datatables datatables-1.10


    【解决方案1】:

    解决方案

    需要修改drawCallback函数如下图:

    api.rows({page:'current'} ).data().each( function ( data, i ) {
       var group = data[0];
       var groupLink = '<a href="' + data[3] + '">' + $('<div>').text(group).html() + '</a>';
    
       if ( last !== group ) {
          $(rows).eq( i ).before(
             '<tr class="group"><td colspan="5">'+group+'</td></tr>'
          );
    
          last = group;
       }
    });
    

    演示

    有关代码和演示,请参阅 this jsFiddle

    注意事项

    我还使用了orderFixed,因此该表始终按包含组名的列排序(在我的示例中为2)。

    【讨论】:

    • 您先生,真是个天才!谢谢!
    • 这个例子似乎正是我需要的@Gyrocode.com ...除了我需要用上面的代码中的数据[3]替换#。我的数据表正在使用“ajax”:{url:“myurl.json”,“type”:“POST”,“dataSrc”:“myname”}。所以我相信我可能需要将 dataSrc 添加到 data[3]?你能帮我解决这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 2021-09-07
    • 2011-04-14
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多