如何将单元格与Javascript DataTables垂直合并(如果单元格在列中重复)?

时间:2018-08-17 01:12:20

标签: javascript jquery html twitter-bootstrap datatables

我想使用javascript datatables创建一个表,如果该单元格值在列中重复自身,则将其“合并”到一个单元格中。例如,如果在“办公室”字段中有三个连续的记录具有“纽约”值,则该字段的那三个记录应合并到一个单元格中(见下文)

下面是当前输出的图像 This is what currently the output is from DataTables

下面是我正在尝试使数据表输出的图像 This is what I'd like to have the DataTable output look like

在这里查看我的JS Fiddle,在下面查看我的代码

<html>

<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <link href='https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css' rel='stylesheet' />


  <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

  <script src='https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js'></script>

</head>

<body>

  <div class="container">
    <div class="row">
      <div class="col-lg-12">

        <table id="example" class="display" style="width:100%">
        </table>
      </div>
    </div>
  </div>
<script>
  data = [{
    DT_RowId: "row_1",
    first_name: "Garrett",
    position: "Accountant",
    email: "g.winters@datatables.net",
    office: "New York",
    extn: "8422",
  },
  {
    DT_RowId: "row_2",
    first_name: "Chris",
    position: "Accountant",
    email: "g.winters@datatables.net",
    office: "New York",
    extn: "8422",
  },
  {
    DT_RowId: "row_3",
    first_name: "Tiger",
    position: "Analyst",
    email: "g.winters@datatables.net",
    office: "New York",
    extn: "8422",
  },
  {
    DT_RowId: "row_4",
    first_name: "Bob",
    position: "Analyst",
    email: "g.winters@datatables.net",
    office: "Tokyo",
    extn: "8422",
  }]

table = $("#example").DataTable({
  data: data,
     // drawCallback: callback_function,
  columns: [
    { data: "office",title:'office'},
    { data: "position",title:'position'},
    { data: "extn" ,title:'extn'},
    { data: "first_name",title:'Name'}
  ]
});

</script>
</body>

</html>

0 个答案:

没有答案