mjvoodoo
mjvoodoo

Reputation: 57

accessing data of bootstrap dynamic table

this is a sample bootstrap dynamic table : I want to access the data in the table

HTML

<div class="container">
   <div class="row clearfix">
    <div class="col-md-12 column">
        <table class="table table-bordered table-hover" id="tab_logic">
            <thead>
                <tr >
                    <th class="text-center">
                        #
                    </th>
                    <th class="text-center">
                        Name
                    </th>
                    <th class="text-center">
                        Mail
                    </th>
                    <th class="text-center">
                        Mobile
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr id='addr0'>
                    <td>
                    1
                    </td>
                    <td>
                    <input type="text" name='name0'  placeholder='Name' class="form-control"/>
                    </td>
                    <td>
                    <input type="text" name='mail0' placeholder='Mail' class="form-control"/>
                    </td>
                    <td>
                    <input type="text" name='mobile0' placeholder='Mobile' class="form-control"/>
                    </td>
                </tr>
                <tr id='addr1'></tr>
            </tbody>
        </table>
    </div>
</div>
<a id="add_row" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete Row</a>
 </div>
  <p id="qoz">s</p>

JavaScript

 $(document).ready(function(){
  var i=1;
 $("#add_row").click(function(){
  $('#addr'+i).html("<td>"+ (i+1) +"</td><td><input name='name"+i+"' type='text' placeholder='Name' class='form-control input-md'  /> </td><td><input  name='mail"+i+"' type='text' placeholder='Mail'  class='form-control input-md'></td><td><input  name='mobile"+i+"' type='text' placeholder='Mobile'  class='form-control input-md'></td>");

  $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
  i++; 
});
$("#delete_row").click(function(){
     if(i>1){
     $("#addr"+(i-1)).html('');
     i--;
     }
 });
});

how can I access the data in the table?

Upvotes: 2

Views: 2326

Answers (2)

Bourbia Brahim
Bourbia Brahim

Reputation: 14712

To display data , kindly See bellow code (I've added show data button click )

$(document).ready(function(){
  var i=1;
  
  $("#add_row").click(function(){
  var numTr = $("#tab_logic tbody tr").length;
  
  $('#tab_logic').append("<tr id='addr"+(numTr)+"'><td>"+ (numTr+1) +"</td><td><input name='name"+numTr+"' type='text' placeholder='Name' class='form-control input-md'  /> </td><td><input  name='mail"+numTr+"' type='text' placeholder='Mail'  class='form-control input-md'></td><td><input  name='mobile"+numTr+"' type='text' placeholder='Mobile'  class='form-control input-md'></td></tr>");

});
$("#delete_row").click(function(){
     var notr = $("#tab_logic tbody tr").length
     if(notr>1){
        $("#addr"+(notr-1)).remove();
     }
 });
 
 
 $("#show_data").click(function(){
    var htmlString="";
    $("#tab_logic tbody tr").each(function(index,el){
       if(index<$("#tab_logic tbody tr").length) {
          var name = $("[name='name"+index+"']").val();
          var mail = $("[name='mail"+index+"']").val();
          var mobile = $("[name='mobile"+index+"']").val();
          console.log("Row "+index+" : [Name="+name+"] - [Mail="+mail +"] - [Mobile="+mobile+"]");
       htmlString += showDataHtml(index,name,mail,mobile)
       }
       $("#data-row").html(htmlString);
    });
 });
});

function showDataHtml(index,name,mail,mobile) {
  index++;
  return "<div class='col-md-12'>Row "+index+" : Name = "+name+" - Mail = "+mail+" - Mobile = "+mobile+"</div>"
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.css" rel="stylesheet"/>

<div class="container">
   <div class="row clearfix">
    <div class="col-md-12 column">
        <table class="table table-bordered table-hover" id="tab_logic">
            <thead>
                <tr >
                    <th class="text-center">
                        #
                    </th>
                    <th class="text-center">
                        Name
                    </th>
                    <th class="text-center">
                        Mail
                    </th>
                    <th class="text-center">
                        Mobile
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr id='addr0'>
                    <td>
                    1
                    </td>
                    <td>
                    <input type="text" name='name0'  placeholder='Name' class="form-control"/>
                    </td>
                    <td>
                    <input type="text" name='mail0' placeholder='Mail' class="form-control"/>
                    </td>
                    <td>
                    <input type="text" name='mobile0' placeholder='Mobile' class="form-control"/>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<div class="row">
  <a id="add_row" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete Row</a>
</div>
<div class="row">
<a id='show_data' class=" btn btn-default">SHow Data</a>
</div>

<div class="container">
  <div id="data-row" class="row clearfix">
    
  <div>
</div>

Upvotes: 1

ankit verma
ankit verma

Reputation: 616

$(document).ready(function(){
  var i=1;
 $("#add_row").click(function(){
  $('#addr'+i).html("<td>"+ (i+1) +"</td><td><input name='name"+i+"' type='text' placeholder='Name' class='form-control input-md'  /> </td><td><input  name='mail"+i+"' type='text' placeholder='Mail'  class='form-control input-md'></td><td><input  name='mobile"+i+"' type='text' placeholder='Mobile'  class='form-control input-md'></td>");

  $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
  i++; 
});
$("#delete_row").click(function(){
     if(i>1){
     $("#addr"+(i-1)).html('');
     i--;
     }
 });
  $('#Get_row_data').click(function(){
    $('[id^=addr]').each(function(){
         var id = $(this).attr('id');
        $('#'+id +' td input').each(function(){
          console.log($(this).val());
        });  
    });
 
 });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
   <div class="row clearfix">
    <div class="col-md-12 column">
        <table class="table table-bordered table-hover" id="tab_logic">
            <thead>
                <tr >
                    <th class="text-center">
                        #
                    </th>
                    <th class="text-center">
                        Name
                    </th>
                    <th class="text-center">
                        Mail
                    </th>
                    <th class="text-center">
                        Mobile
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr id='addr0'>
                    <td>
                    1
                    </td>
                    <td>
                    <input type="text" name='name0'  placeholder='Name' class="form-control"/>
                    </td>
                    <td>
                    <input type="text" name='mail0' placeholder='Mail' class="form-control"/>
                    </td>
                    <td>
                    <input type="text" name='mobile0' placeholder='Mobile' class="form-control"/>
                    </td>
                </tr>
                <tr id='addr1'></tr>
            </tbody>
        </table>
    </div>
</div>
<a id="add_row" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete Row</a>
<a id='Get_row_data' class="pull-right btn btn-default">Get Row Data</a>

Hello Please check this code This might help you out in solving your purpose. What i did is i have added a new button to get the data from row. I used wildCard Id of Row and fetch the value of input boxes.

Thanks

Upvotes: 2

Related Questions