jquery 数据表行分组

发布于 2024-10-20 14:15:42 字数 3417 浏览 1 评论 0原文

我正在使用 jQuery DataTables(版本 1.7.6)。我想将两个连续行分组,这样我就可以为这两行提供编辑选项。

这是我的表格:

<table class="display" id="specificproduct_table"
       width="100%" cellpadding="0" cellspacing="0" border="0" >
  <thead>
    <tr>
      <th width="7%">column1</th>
      <th width="16%">column2</th>
      <th width="5%">Edit</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td colspan="5" class="dataTables_empty">
        Loading data from server
      </td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th>column1</th>
      <th>column2</th>
      <th width="5%">Edit</th>
    </tr>
  </tfoot>
</table>

这是我的脚本,采用 关于行分组的 DataTable 文档

$(function() {
  var oTable = $('#specificproduct_table').dataTable({
    "aoColumns": [{
      "sClass": "nonedit"
    }, {
      "sClass": "nonedit"
    }, {
      "sClass": "nonedit",
      "bSortable": false
    }, ],
    "bProcessing": true,
    "bServerSide": true,
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "sAjaxSource": "ajax_shotgun_table",
    "fnDrawCallback": function(oSettings) {
      if (oSettings.aiDisplay.length == 0) {
        return;
      }

      var nTrs = $('tbody tr', oSettings.nTable);
      var iColspan = nTrs[0].getElementsByTagName('td').length;
      var sLastGroup = "";
      for (var i = 0; i < nTrs.length; i++) {
        var iDisplayIndex = oSettings._iDisplayStart + i;
        var sGroup = oSettings.aoData[oSettings.aiDisplay[iDisplayIndex]]._aData[0];
        if (sGroup != sLastGroup) {
          var nGroup = document.createElement('tr');
          var nCell = document.createElement('td');
          nCell.colSpan = iColspan;
          nCell.className = "group";
          nCell.innerHTML = sGroup;
          nGroup.appendChild(nCell);
          nTrs[i].parentNode.insertBefore(nGroup, nTrs[i]);
          sLastGroup = sGroup;
        }
      }
    },
    "aoColumnDefs": [{
      "bVisible": false,
      "aTargets": [0]
    }],
    "aaSortingFixed": [
      [0, 'asc']
    ],
    "aaSorting": [
      [1, 'asc']
    ],
    "sDom": 'lfr<"giveHeight"t>ip'
  });
});

我正在寻找这样的输出:

<table width="98%" border="0">
  <tr>
    <td>column1</td>
    <td>column2</td>
    <td>Edit</td>
  </tr>
  <tr>
    <td>product A </td>
    <td>A</td>
    <td rowspan="2">edit</td>
  </tr>
  <tr>
    <td>product A 1 </td>
    <td>A</td>
  </tr>
  <tr>
    <td>product B </td>
    <td>B</td>
    <td rowspan="2">edit</td>
  </tr>
  <tr>
    <td>product B 1 </td>
    <td>B</td>
  </tr>
  <tr>
    <td>product C </td>
    <td>C</td>
    <td rowspan="2">edit</td>
  </tr>
  <tr>
    <td>product C 1 </td>
    <td>C</td>
  </tr>
</table>

I'm using jQuery DataTables (version 1.7.6). I'd like to group two consecutive rows so I can have an edit option for both rows.

Here's my table:

<table class="display" id="specificproduct_table"
       width="100%" cellpadding="0" cellspacing="0" border="0" >
  <thead>
    <tr>
      <th width="7%">column1</th>
      <th width="16%">column2</th>
      <th width="5%">Edit</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td colspan="5" class="dataTables_empty">
        Loading data from server
      </td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th>column1</th>
      <th>column2</th>
      <th width="5%">Edit</th>
    </tr>
  </tfoot>
</table>

Here's my script, adopted from the DataTable docs on Row grouping

$(function() {
  var oTable = $('#specificproduct_table').dataTable({
    "aoColumns": [{
      "sClass": "nonedit"
    }, {
      "sClass": "nonedit"
    }, {
      "sClass": "nonedit",
      "bSortable": false
    }, ],
    "bProcessing": true,
    "bServerSide": true,
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "sAjaxSource": "ajax_shotgun_table",
    "fnDrawCallback": function(oSettings) {
      if (oSettings.aiDisplay.length == 0) {
        return;
      }

      var nTrs = $('tbody tr', oSettings.nTable);
      var iColspan = nTrs[0].getElementsByTagName('td').length;
      var sLastGroup = "";
      for (var i = 0; i < nTrs.length; i++) {
        var iDisplayIndex = oSettings._iDisplayStart + i;
        var sGroup = oSettings.aoData[oSettings.aiDisplay[iDisplayIndex]]._aData[0];
        if (sGroup != sLastGroup) {
          var nGroup = document.createElement('tr');
          var nCell = document.createElement('td');
          nCell.colSpan = iColspan;
          nCell.className = "group";
          nCell.innerHTML = sGroup;
          nGroup.appendChild(nCell);
          nTrs[i].parentNode.insertBefore(nGroup, nTrs[i]);
          sLastGroup = sGroup;
        }
      }
    },
    "aoColumnDefs": [{
      "bVisible": false,
      "aTargets": [0]
    }],
    "aaSortingFixed": [
      [0, 'asc']
    ],
    "aaSorting": [
      [1, 'asc']
    ],
    "sDom": 'lfr<"giveHeight"t>ip'
  });
});

I'm looking for output like this:

<table width="98%" border="0">
  <tr>
    <td>column1</td>
    <td>column2</td>
    <td>Edit</td>
  </tr>
  <tr>
    <td>product A </td>
    <td>A</td>
    <td rowspan="2">edit</td>
  </tr>
  <tr>
    <td>product A 1 </td>
    <td>A</td>
  </tr>
  <tr>
    <td>product B </td>
    <td>B</td>
    <td rowspan="2">edit</td>
  </tr>
  <tr>
    <td>product B 1 </td>
    <td>B</td>
  </tr>
  <tr>
    <td>product C </td>
    <td>C</td>
    <td rowspan="2">edit</td>
  </tr>
  <tr>
    <td>product C 1 </td>
    <td>C</td>
  </tr>
</table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

草莓味的萝莉 2024-10-27 14:15:42

目前(1.7.6)dataTables 不支持 col 或 rowspan,

您可能可以使用 fnDrawCallback 遍历表格,并用最后一个单元格的 rowspan 的两个新行替换每两行,因为 fnDrawCallback 在表格之后调用已绘制(它将导致重绘)

不确定它将如何处理排序/搜索

at the moment (1.7.6) dataTables doesn't support col or rowspan

you could probably use fnDrawCallback to go through the table and replace every two lines with two new lines with a rowspan for the last cell, as fnDrawCallback is invoked after the table is drawn (it will cause a redraw)

not sure how it would handle sorting/searching

萌能量女王 2024-10-27 14:15:42

fnFakeRowspan() 函数可以做到这一点,可能

是链接: http://datatables.net/plug -ins/api#fnFakeRowspan

fnFakeRowspan() function can do this, probably

Here is the link: http://datatables.net/plug-ins/api#fnFakeRowspan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文