如何创建具有多个子行(嵌套表)的jQuery数据表?

时间:2018-11-20 09:27:19

标签: json asp.net-mvc linq jquery-ui datatables

问题:       我需要使用嵌套表格式创建一个表。当用户单击加号按钮时,它应显示嵌套表。如果他们单击减号按钮,它将隐藏。

我已经完成了jquery datatable的正常工作。但我无法为子表显示多行。我已经尝试了很多次,无法显示正确的格式。

这是我引用创建表的链接 https://datatables.net/examples/api/row_details.html

数据库: 按照以下格式来自数据库的实际数据,并且我已将JSON格式转换为显示: enter image description here

C#代码

 return new JsonResult { Data = new { data = test }, JsonRequestBehavior = 
JsonRequestBehavior.AllowGet };

我需要一个带有嵌套表的输出: enter image description here

用户界面代码:

/* Formatting function for row details - modify as you need */
 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>City Name</td>' +
            '<td>Permission</td>' +
        '</tr><tr>' +
            '<td>' + d.City+ '</td>' +
            '<td>' + d.Permission+ '</td>' +
        '</tr>' +
'</table>';
}

$(document).ready(function() {
var table = $('#example').DataTable( {
    "ajax": "../ajax/data/objects.txt",
    "columns": [
        {
            "className":      'details-control',
            "orderable":      false,
            "data":           null,
            "defaultContent": ''
        },
        { "data": "UserName" },
        { "data": "Email" },
        { "data": "UserId" },

    ],
    "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');
    }
  } );
 } );

JSON格式:

{"data":[
{"UserId":"ABC","UserName":"Peter","Email":"abc@gmail.com.com","CityName":"Chennai","Permission":"Manager,LocalUser"},
{"UserId":"ABC","UserName":"Peter","Email":"abc@gmail.com.com","CityName":"Bangalore","Permission":"Admin,LocalUser"},
{"UserId":"xyz","UserName":"Haiyan","Email":"abc2@gmail.com.com","CityName":"Bangalore","Permission":"LocalUser"},
{"UserId":"xyz","UserName":"Haiyan","Email":"abc2@gmail.com.com","CityName":"Chennai","Permission":"LocalUser,Manager"}]}

使用的技术:ASP.Net MVC5

通过其他任何方式,我都可以使用linq或修改JSON数据格式。

2 个答案:

答案 0 :(得分:1)

请参考此示例,该示例显示数据表中的嵌套行

http://jsfiddle.net/survivant/p1uuq0ew/2/

或者如果您有很多数据[!行]

,则可以使用它

https://datatables.net/examples/api/row_details.html

答案 1 :(得分:0)

您可以像这样将ID赋予子表

function format ( d ) {
   // `d` is the original data object for the row
  return '<table id="childtable" cellpadding="5" cellspacing="0" border="0" style="padding- 
left:50px;">'+
      '<tr>' +
            '<td>City Name</td>' +
            '<td>Permission</td>' +
        '</tr><tr>' +
            '<td>' + d.City+ '</td>' +
            '<td>' + d.Permission+ '</td>' +
        '</tr>' +
'</table>';
}

并执行与父表相同的操作

var childTable = $('#childtable').DataTable( {
    "ajax": "../ajax/data/objects.txt",
    "columns": [

    ],
    "order": [[1, 'asc']]
} );

将您的json对象绑定到“列”部分。