Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap table - dynamic button in row

I am using bootstrap v3. I try to get the effect as in the picture. I want a button, what to display "Item Name" in popup. But my button is not displayed.

The problem is that this nature of my table is a dynamic (form JSON) - which makes things difficult.

Picture: enter image description here

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>

I will be grateful for your help.

like image 465
user3794273 Avatar asked Mar 08 '23 22:03

user3794273


1 Answers

Hope this helps..

    <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>
like image 52
T.Shah Avatar answered Mar 10 '23 11:03

T.Shah