【问题标题】:Child row are not creating in datatables?子行不在数据表中创建?
【发布时间】:2014-06-19 14:56:15
【问题描述】:

我正在尝试使用 json 数据在数据表中创建子行。某些原因表和子行正在使用数据创建。所以我不明白问题是什么。

<div class="container">
      <table id="example" class="display" cellspacing="0" width="100%">
          <thead>
              <tr>
                  <th></th>
                  <th>Name</th>
                  <th>Position</th>
                  <th>Office</th>
                  <th>Salary</th>
              </tr>
          </thead>

          <tfoot>
              <tr>
                  <th></th>
                  <th>Name</th>
                  <th>Position</th>
                  <th>Office</th>
                  <th>Salary</th>
              </tr>
          </tfoot>
      </table>
    </div>

Javascript

    function format ( d ) {
    // `d` is the original data object for the row
    return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
        '<tr>'+
            '<td>Full name:</td>'+
            '<td>'+d.name+'</td>'+
        '</tr>'+
        '<tr>'+
            '<td>Extension number:</td>'+
            '<td>'+d.extn+'</td>'+
        '</tr>'+
        '<tr>'+
            '<td>Extra info:</td>'+
            '<td>And any further details here (images etc)...</td>'+
        '</tr>'+
    '</table>';
}

$(document).ready(function() {

    var data ={
  "data": [ {
      "name": "Michael Bruce",
      "position": "Javascript Developer",
      "salary": "$183,000",
      "start_date": "2011/06/27",
      "office": "Singapore",
      "extn": "5384"
    },
    {
      "name": "Donna Snider",
      "position": "Customer Support",
      "salary": "$112,000",
      "start_date": "2011/01/25",
      "office": "New York",
      "extn": "4226"
    }
  ]
};
 var table = $('#example').DataTable( {
      "data":data,
        "columns": [
            {
                "class":          'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            },
            { "data": "name" },
            { "data": "position" },
            { "data": "office" },
            { "data": "salary" }
        ],
        "order": [[1, 'asc']]
    } );

    // Add event listener for opening and closing details
    $('#example tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = table.row( tr );

        if ( row.child.isShown() ) {
            // This row is already open - close it
            row.child.hide();
            tr.removeClass('shown');
        }
        else {
            // Open this row
            row.child( format(row.data()) ).show();
            tr.addClass('shown');
        }
    } );
} );

这是我的代码的 datatables live

【问题讨论】:

  • 您能描述一下您认为是什么问题吗?我看了你的例子 - 是表格没有显示data 对象中的数据的问题吗?
  • 表格没有显示data对象提供的数据。

标签: c# javascript jquery datatable


【解决方案1】:

您的数据变量是一个对象。它必须是一个数组:

var data =
[  
    {
      "name": "Tiger Nixon",
      "position": "System Architect",
      "salary": "$320,800",
      "start_date": "2011/04/25",
      "office": "Edinburgh",
      "extn": "5421"
    },
 ....
];

对比

var data =
{ // remove 
"data":  // remove
  [  
    {
      "name": "Tiger Nixon",
      "position": "System Architect",
      "salary": "$320,800",
      "start_date": "2011/04/25",
      "office": "Edinburgh",
      "extn": "5421"
    },
   ....
  ]}; // remove closing curly bracket

示例:http://live.datatables.net/waburuz/10/edit

【讨论】:

  • 感谢您的帖子。但是您的示例仍然没有显示子行。
  • 所以...您寻找的不仅仅是要显示的数据?您能否描述一下您确切希望此应用程序做什么?
  • 我希望数据显示,它也详细列在第一行。 datatables.net/examples/api/row_details.html
  • 参见live.datatables.net/waburuz/10/edit - 我将格式功能移到了文本的底部并且它起作用了。它在一些插入的 break 语句上引发错误,我找不到。不知道这是怎么回事。但是您的功能似乎是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-17
  • 2021-01-09
  • 1970-01-01
  • 2013-05-14
  • 2014-12-26
  • 2022-06-22
相关资源
最近更新 更多