bootstrap-table:如何将一个表嵌套到另一个表中?

soo*_*nic 3 javascript jquery twitter-bootstrap-3 bootstrap-table

我正在使用这个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);
  });

});
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

这是我的演示的链接:fiddle

有两张桌子。表 1 有可扩展的行,我想将表 2 放入表 1 的每一行中。有一个名为detailFormatter的函数可以执行此操作,并且您可以在其中返回字符串或呈现元素(我正在使用它,但我可以'让它发挥作用)。对于这个演示,表 1 是使用一些 html 代码创建的,表 2 仅使用 javascript,但有这个启动 div <table id="detailTable"></table>(对我来说哪种方式完成并不重要)。那么,问题是如何将表 2 放入表 1 中,并可以使用其事件(例如选中或取消选中复选框),如表 2 的演示所示?稍后,我想使用此事件onExpandRow,以便当用户展开表 1 的行时,它将从数据库获取数据并填充表 2。

Gur*_*Rao 5

好吧,只需克隆你的detailTable并将其放入每个中,如下row所示detailFormatter

function detailFormatter(index, row, element) {
    $(element).html($('#detailTable').clone(true));
}
Run Code Online (Sandbox Code Playgroud)

现在,当您这样做时,将会id创建重复的clone,因此您只需通过更改它来删除它,id如下所示:

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

另外,您可以隐藏,#detailTable因为您只是将其附加到另一个中table,一旦隐藏它,您需要添加showcloned tables,因为所有属性都将被复制到其中clone(true),可以按如下方式完成:

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

完整的DEMO