【问题标题】:How to show nested row inside JQuery DataTable?如何在 JQuery DataTable 中显示嵌套行?
【发布时间】:2020-07-31 05:10:07
【问题描述】:

我正在从我的 asp net-core 应用程序调用一个 API,它是 跟随 Json 数据返回我。 在这个 Json companyRegionList 内部是动态的,如果里面没有数据,那么我们将得到它的 Value null

"result":[
      {
         "id":1,
         "stateName":"Ow",
         "regionDirector":"Cassy Fusset",
         "status" : Active,
         "phoneNumber":"123456",
         "email":"cassy.f@gmail.com",
         "companyRegionList":{
            "companyList":[
               {
                  "companyId":4,
                  "code":"ATL",
                  "Name":"Atlanta",
                  "Director":"corey Cullen ",
                  "contact":"123456",
                  "Status":"Active"
               },
               {
                  "companyId":4,
                  "code":"ATL",
                  "Name":"Atlanta",
                  "Director":"corey Cullen ",
                  "contact":"123456",
                  "Status":"Active"
               },
               {
                  "companyId":4,
                  "code":"ATL",
                  "Name":"Atlanta",
                  "Director":"corey Cullen ",
                  "contact":"123456",
                  "Status":"Active"
               },
            ]
         }
      },
      {
         "id":1,
         "stateName":"Florida",
         "regionDirector":"Boone Williams",
         "status" : Active,
         "phoneNumber":"123456",
         "email":"Boone.w@gmail.com",
         "companyRegionList":
         "companyList":[{
                  "companyId":4,
                  "code":"ATL",
                  "Name":"Atlanta",
                  "Director":"corey Cullen ",
                  "contact":"123456",
                  "Status":"Active"
               }
      },    ]
      {
         "id":1,
         "stateName":"NW Georgia",
         "regionDirector":"Mikel Stapp",
         "status" : Active,
         "phoneNumber":"123456",
         "email":"mikel.s@gmail@gmail.com",
         "companyRegionList": null
      }
   ]

I want to Show my Json Data to this way using Jquery Datatable. if CompanyRegionList inside json data is null then i want to show No data available .

【问题讨论】:

标签: jquery datatables


【解决方案1】:

这样的事情怎么样?

var result = [
      {
         "id": 1,
         "stateName": "Ow",
         "regionDirector": "Cassy Fusset",
         "status": "Active",
         "phoneNumber": "(987)767 67678",
         "email": "cassy.f@gmail.com",
         "companyRegionList": {
            "companyList": [
               {
                  "companyId": 1,
                  "code":"ATL",
                  "Name":"Atlanta",
                  "Director":"corey Cullen ",
                  "contact":"(336) 876 9874",
                  "Status":"Active"
               },
               {
                  "companyId": 2,
                  "code":"ATL2",
                  "Name":"Atlanta  II",
                  "Director":"corey Cullen ",
                  "contact":"(336) 876 9874",
                  "Status":"Active"
               },
               {
                  "companyId": 3,
                  "code":"ATL",
                  "Name":"Atlanta Pinnacle",
                  "Director":"corey Cullen ",
                  "contact":"(336) 876 9874",
                  "Status":"Active"
               },
               {
                  "companyId": 4,
                  "code":"BDC",
                  "Name":"BDC Staff",
                  "Director":"corey Cullen ",
                  "contact":"(336) 876 9874",
                  "Status":"Active"
               }
            ]
         }
      },
      {
         "id": 2,
         "stateName":"Florida",
         "regionDirector":"Boone Williams",
         "status": "Active",
         "phoneNumber":"(987)767 67678",
         "email":"Boone.w@gmail.com",
         "companyRegionList": {
                "companyList": [
                     {
                  "companyId":4,
                  "code":"ATL",
                  "Name":"Atlanta",
                  "Director":"corey Cullen ",
                  "contact":"123456",
                  "Status":"Active"
               }
                ]
         }
      },
      {
         "id": 3,
         "stateName":"NW Georgia",
         "regionDirector":"Mikel Stapp",
         "status": "Active",
         "phoneNumber":"(987)767 67678",
         "email":"mikel.s@gmail@gmail.com",
         "companyRegionList": null
      }
   ];

function format ( d ) {

    // `d` is the original data object for the row
     var html = '<table class="table">'+
        '<thead><tr style="background-color: #f5f8fb;">'+
            '<th>CITY CODE</th>'+
            '<th>CITY NAME</th>'+
            '<th>DIRECTOR</th>'+
            '<th>CONTACT</th>'+
            '<th>STATUS</th>'+
        '</tr></thead><tbody>';
      
        if(d.companyRegionList == null)
             html += '<tr><td colspan="5" style="text-align: center;">No data available</td></tr>';
        else {
            for (let i in d.companyRegionList.companyList) {
        
               html += '<tr><td>' + d.companyRegionList.companyList[i].code + '</td>';
               html += '<td>' + d.companyRegionList.companyList[i].Name + '</td>';
               html += '<td>' + d.companyRegionList.companyList[i].Director + '</td>';
               html += '<td>' + d.companyRegionList.companyList[i].contact + '</td>';
               html += '<td>' + d.companyRegionList.companyList[i].Status + '</td></tr>';
            }
        }
        
    html += '</tbody</table>';
    
    return html;
}

var oTable = $('#tblExample').DataTable({
  ordering: false,
  paging: false,
  searching: true,
  info: false,
  data: result,
  columns: [
      { data: 'id', visible: false },
      { data: 'stateName', className: 'textUC' },
      { data: 'regionDirector', className: 'textUC' },
      { data: 'status', className: 'textUC' },
      { data: 'phoneNumber', className: 'textUC' },
      { data: 'email', className: 'textUC' },
      {
        "className":      'details-control',
        "orderable":      false,
        "data":           null,
        "defaultContent": ''
      }
  ]
});

// Add event listener for opening and closing details
$('#tblExample tbody').on('click', 'td.details-control', function () {

  $('#tblExample tbody > tr').addClass('rowInactive');
  
  var tr = $(this).closest('tr');
  var row = oTable.row( tr );
  $(tr).removeClass('rowInactive');

  if ( row.child.isShown() ) {
    // This row is already open - close it
    row.child.hide();
    tr.removeClass('shown');
    $('#tblExample tbody > tr').removeClass('rowInactive');
  }
  else {
    // Open this row
    row.child( format(row.data()) ).show();
    tr.addClass('shown');
  }
});
td.details-control {
    background: url('https://i.imgur.com/I8lvlcC.jpg') no-repeat center center;
    /*background: url('https://datatables.net/examples/resources/details_open.png') no-repeat center center;*/
    cursor: pointer;
}

tr.shown td.details-control {
    background: url('https://imgur.com/wUcJYSG.jpg') no-repeat center center;
    /*background: url('https://datatables.net/examples/resources/details_close.png') no-repeat center center;*/
}

.textUC {
  text-transform: uppercase;
  font-weight: bold;
}

.rowActive {
  color: black;
}

.rowInactive {
  color: #97aaba;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap.min.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap.min.js"></script>

<table id="tblExample" class="table" style="width: 100%">
    <!--<thead>
    <tr>
        <th>id</th>
        <th>stateName</th>
        <th>regionDirector</th>
        <th>status</th>
        <th>phoneNumber</th>
        <th>email</th>
    </tr>
    </thead>-->
  </table>

【讨论】:

  • 我想在整个页面的宽度上显示它@Sagnalrac。
  • 为什么使用这个函数 format(d) 以及为什么 columns.data 属性为空?
  • @Coder:关于全宽,您是否尝试将style="width: 100%" 添加到您的表格中?关于您的第二个问题,实际上您可以随意命名该函数,我只按照您可以在 jQuery DataTable 的网站 文档中找到的示例:Child rows (show extra / detailed information)
  • @Coder:顺便说一下,关于您的第三个问题,有时您需要在 DataTable 中添加一个额外的列,但不能精确地显示数据(来自服务器端响应)。例如,假设您想在您的案例,用于展开/折叠子行的图像。在这些情况下,您可以输入data: null,因为您不需要在该列中显示数据。虽然我没有尝试省略那个参数,看看会发生什么。
  • @Coder:经过进一步研究,现在我可以更好地向您解释何时在列中使用data: null。此操作对其他两个初始化选项有影响: - defaultContent 属性:当给出 null 作为数据选项并为列指定 defaultContent 时,将使用 defaultContent 定义的值作为单元格。 - render 属性:当 null 用于数据选项并且为列指定渲染选项时,行的整个数据源用于渲染器。更多信息here
猜你喜欢
  • 2012-03-15
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 2017-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-09
相关资源
最近更新 更多