【问题标题】:bootstrap-table: how to nest one table into another?bootstrap-table:如何将一张表嵌套到另一张表中?
【发布时间】:2016-01-24 22:21:33
【问题描述】:

我正在使用这个bootstrap-table。我想将一张表嵌套到另一张表中。

$(document).ready(function() {
  var t = [{
    "id": "1",
    "name": "john",
  }, {
    "id": "2",
    "name": "ted"
  }];


  //TABLE 1
  $('#summaryTable').bootstrapTable({
    data: t,
    detailFormatter: detailFormatter
  });

  function detailFormatter(index, row, element) {
    $(element).html(row.name);
    $(element).html(function() {
      //I was thinking of putting the code here, but I don't know how to do it so it will work,
      //except javascript, it needs to be put this html code <table id="detailTable"></table>

    });


    //   return [

    //    ].join('');
  }

  //TABLE 2
  var t2 = [{
    "id": "1",
    "shopping": "bike",
  }, {
    "id": "2",
    "shopping": "car"
  }];
  $('#detailTable').bootstrapTable({
    data: t2,
    columns: [{
      field: 'id',
      title: 'id'
    }, {
      field: 'shopping',
      title: 'Shopping detail'
    }, {
      field: 'status',
      checkbox: true
    }],
    checkboxHeader: false

  });
  $('#detailTable').on('check.bs.table', function(e, row, $el) {
    alert('check index: ' + row.shopping);
  });
  $('#detailTable').on('uncheck.bs.table', function(e, row, $el) {
    alert('uncheck index: ' + row.shopping);
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.10.0/bootstrap-table.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.10.0/bootstrap-table.min.js"></script>



<div class="container">
  <p>TABLE 1</p>
  <table id="summaryTable" data-detail-view="true">
    <thead>
      <tr>
        <th data-field="id" data-align="center" data-width="20px">id</th>
        <th data-field="name" data-align="center">Name</th>
      </tr>
    </thead>
  </table>
  <br>

  <p>TABLE 2</p>
  <table id="detailTable"></table>

</div>

这是我的演示链接:fiddle

有两张桌子。表 1 具有可扩展的行,我想将表 2 放入表 1 的每一行。有一个名为 detailFormatter 的函数可以执行此操作,您可以在其中返回字符串或渲染元素(我正在使用它,但我不能让它工作)。对于这个演示,表 1 是使用一些 html 代码创建的,而表 2 仅使用 javascript,但是有这个启动 div &lt;table id="detailTable"&gt;&lt;/table&gt;(对我来说,采用哪种方式并不重要)。 所以,问题是如何将表 2 放入表 1 中,并有可能使用其事件(如选中或取消选中复选框)作为 table2 的演示显示?稍后,我想使用此事件onExpandRow 所以当用户展开表 1 的行时,它将从数据库中获取数据并填写表 2。

【问题讨论】:

    标签: javascript jquery twitter-bootstrap-3 bootstrap-table


    【解决方案1】:

    只需克隆您的 detailTable 并将其放入每个 rowdetailFormatter 如下:

    function detailFormatter(index, row, element) {
        $(element).html($('#detailTable').clone(true));
    }
    

    现在,当您执行clone 时,将创建重复的id,因此您只需通过更改其id 将其删除,如下所示:

    function detailFormatter(index, row, element) {
         $(element).html($('#detailTable').clone(true).attr('id',"tbl_"+row.id));
    }
    

    此外,您可以隐藏#detailTable,因为您只是将其附加到另一个table 中,一旦您隐藏它,您需要将show 添加到cloned tables,因为所有属性都将被复制到在clone(true) 中可以这样做:

    function detailFormatter(index, row, element) {
        $(element).html($('#detailTable').clone(true).attr('id',"tbl_"+row.id).show());
    }
    //.....
    //.....
    //.....
    //At the end
    $("#detailTable").hide();
    

    A complete DEMO

    【讨论】:

    • 谢谢。这个对我有用。我只需要将表 2 放入 Div 以删除表 1 下的这一行,并同时对一个打开的行进行限制,因此用户不会选中两个不同打开的行中的复选框。我还有两个关于 CSS 的问题。如何为表 2 制作padding:0?以及如何更改表 1 的行背景颜色,其中行展开并在折叠后恢复旧颜色? updated demo我已经添加了expand-row函数和一些CSS代码。
    • 没错,再次感谢,第一个问题呢?表 2 放置了 8px 填充,我发现 bootstrap-table.css 中的行 .bootstrap-table .table:not(.table-condensed) &gt; tbody &gt; tr &gt; td 会导致这种行为,但如果我尝试覆盖它,那么它会对整个表 1 和 2 产生影响。
    • 你可以给你的第二张桌子一个额外的 class 名字,并在那个类上写额外的 CSS .. 但是它将被复制到 clone.. Something like this
    • 我没有看到任何效果,我注意到你添加了一个类,但表 2 周围仍然有 8px 的填充。
    • 怎么样 this.. 我添加了 ``tr.detail-view>td { padding:0 !important; //它对一切都有影响 }` 替换了您创建css 的问题,即对所有tds 产生影响
    【解决方案2】:

    我找到了另一种使用(部分)动态表创建将一个表嵌套到另一个表的方法(在使用 bootstrap-table 时参考我发布的问题)。

    var expandedRow = row.id;
    $(element).html(
        "<table id='detailTable"+expandedRow+"'><thead><tr><th data-field='id2' data-align='center' data-width='20px'>id2</th>" +
        "<th data-field='shopping' data-align='center'>Shopping detail</th>" +
        "<th data-field='status' data-checkbox='true'></th>" +
        "</tr></thead></table>");
    
    $('#detailTable'+expandedRow).bootstrapTable({
      data: t2,
      checkboxHeader: false
    });
    

    我认为这种方式更独立。当时不需要打开一个面板。事件和方法似乎运行良好。

    Full code here.

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      • 2017-11-24
      相关资源
      最近更新 更多