【问题标题】:Bootstrap table - dynamic button in row引导表 - 行中的动态按钮
【发布时间】:2017-10-04 19:49:41
【问题描述】:

我正在使用 bootstrap v3。我试着得到如图所示的效果。我想要一个按钮,在popup 中显示"Item Name" 的内容。但是我的按钮没有显示出来。

问题是我的表格的这种性质是动态的(表格JSON) - 这让事情变得困难。

图片:

var $table = $('#table');
var mydata = [{
    "id": 0,
    "name": "test0",
    "price": "$0"
  },
  {
    "id": 1,
    "name": "test1",
    "price": "$1"
  },
  {
    "id": 2,
    "name": "test2",
    "price": "$2"
  }
];

$(function() {
  $('#table').bootstrapTable({
    data: mydata
  });
});
<html>

<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.10.1/bootstrap-table.min.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflarenter code heree.com/ajax/libs/bootstrap-table/1.10.1/bootstrap-table.min.js"></script>
</head>

<body>
  <div class="container">
    <table id="table" data-search="true">
      <thead>
        <tr>
          <th data-field="id" data-sortable="true">Item ID</th>
          <th data-field="name" data-sortable="true">Item Name</th>
          <th data-field="price" data-sortable="true">Item Price</th>
          <th>Show Name</th>
        </tr>
      </thead>
      <tbody>
        <tr></tr>
        <tr></tr>
        <tr></tr>
        <tr>
          <td>
            <button type="button" class="btn btn-primary btn-sm">Small button</button>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</body>

</html>

我会很感激你的帮助。

【问题讨论】:

  • 那么,您希望在单击项目名称时打开一个弹出窗口(模式)?
  • 是的,弹出窗口。但是点击按钮后。单击按钮 1 后显示 test0,单击按钮 2 后显示 test1,依此类推
  • 嗨,这是你需要的吗? jsfiddle.net/tmyfssw2。我没有添加CSS。所以复制粘贴它:P
  • 在静态代码中,很容易做到。问题是从 JSON 读取时如何做到这一点?

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


【解决方案1】:

希望这会有所帮助..

    <html>

<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.10.1/bootstrap-table.min.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
  <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.1/bootstrap-table.min.js"></script>
</head>

<body>
  <div class="container">
    <table id="table" data-search="true">
      <thead>
        <tr>
          <th data-field="id" data-sortable="true">Item ID</th>
          <th data-field="name" data-sortable="true">Item Name</th>
          <th data-field="price" data-sortable="true">Item Price</th>
          <th>Show Name</th>
        </tr>
      </thead>
      <tbody>

      </tbody>
    </table>
  </div>

    <!-- Modal -->
    <div id="myModal" class="modal fade" role="dialog">
      <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title"></h4>
          </div>
          <div class="modal-body">
            <p></p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>

      </div>
    </div>


    <script>


            var $table = $('#table');
            var mydata = [{
                "id": 0,
                "name": "test0",
                "price": "$0"
              },
              {
                "id": 1,
                "name": "test1",
                "price": "$1"
              },
              {
                "id": 2,
                "name": "test2",
                "price": "$2"
              }
            ];

            $(function() {
              $('#table').bootstrapTable({
                data: mydata,
                columns: [ {},{},{},  
                {
                  field: 'operate',
                  title: 'Edit',
                  align: 'center',
                  valign: 'middle',
                  clickToSelect: false,
                  formatter : function(value,row,index) {
                    //return '<input name="elementname"  value="'+value+'"/>';
                    return '<button class=\'btn btn-primary \' pageName="'+row.name+'" pageDetails="'+row.price+'"  >Edit</button> ';
                  }
                }
              ]               
              });


            $(".btn").click(function(){
                var pageDetails = $(this).attr('pageDetails');
                var pageName = $(this).attr('pageName');
                $(".modal .modal-title").html(pageName);
                $(".modal .modal-body").html(pageDetails);
                $(".modal").modal("show");

            });

            });




    </script>




</body>

</html>

【讨论】:

  • 你能解释一下你在做什么而不是仅仅发布一个可能的答案吗?所以 OP 和其他人可以从你的代码中学习
  • 我添加了代码以在您的数据表中添加该列。格式化程序添加包含按钮的列。按钮数据属性包含要在模式中使用的信息。美元。 Btn click 函数在按钮单击时获取数据属性,然后将它们传递给模态。希望这能解释...
  • 是的,先生!这很清楚:) 但是这一次,我问的是“你能解释一下你在做什么,而不是仅仅发布一个可能的答案吗?” :)
  • 嗯......实际上你没有做错什么......你不只是在做它!在您启动引导表的函数中,我添加了为每一行添加一列的代码,添加了模式代码并添加了 jquery 函数以在每次按钮单击时使用相关信息填充模式。这真的是我最擅长的解释......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-18
  • 2018-02-22
  • 1970-01-01
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多