0

i created a table using html . now i want to populate my table using json data. i have been using the example https://www.youtube.com/watch?v=A20PY5RxdI8

my javascript code is here

$(document).ready(function () {
	
	
	$.getJSON("my.php",  function(json){
	
    myjson = json;
	
	
}).done(function() {

 $('#tble').bootstrapTable('load', myjson);
			
			var $table = $('#tble');
$table.bootstrapTable('load', myjson);
		     $table.bootstrapTable({
	search: true,
			      pagination: true,
			      buttonsClass: 'primaryt',
			      showFooter: true,
				   minimumCountColumns: 2,
    columns: [ {
        field: 'one',
        title: 'ID'
    }, {
        field: 'two',
        title: 'f name'
    }, {
        field: 'three',
        title: 'last name'
    }],
    data: myjson
	

});
	
});
});
<table id = 'tble'>
<tr class=xl70 height=42 style='mso-height-source:userset;height:31.5pt'>
  <th height=42 class=xl72 width=57 style='height:31.5pt;border-top:none;
  border-left:none;width:43pt' data-field="one">1</th>
  <th class=xl72 width=80 style='border-top:none;border-left:none;width:60pt'>2</th>
  <th class=xl72 width=97 style='border-top:none;border-left:none;width:73pt'>3</th>
  
 </tr>
</table>

i want to populate json into my table but it does not display any data in the said table

2
  • Are you using any external jquery for bootstrap table function? Commented Jul 17, 2019 at 15:23
  • no i am not using external query Commented Jul 17, 2019 at 15:24

3 Answers 3

1

You can use the following code example:

$.getJSON("my.php")
  .done(function(data) {
    $('#tble').bootstrapTable('load', data);
  })

Read the $.getJson() documentation for more example.

Sign up to request clarification or add additional context in comments.

3 Comments

then what should i write in html fields?
i am stuck at displaying coming data into html table
Use data-field attributes in <th> tags, like in the video or in the documentation examples: bootstrap-table.com/docs/getting-started/usage
0

I have checked the documentation of Bootstrap table which you are using in your example. The document is poorly written without explaining the parameters. Which results in confusion. When you read the document and do some JS fiddle you will realise that data-toggle attribute is very important. The value of this attribute should be the same as the Id of your table. you can try removing data attribute filed in below fiddle to verify it.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://unpkg.com/[email protected]/dist/bootstrap-table.min.css" rel="stylesheet">

<script src="https://unpkg.com/[email protected]/dist/bootstrap-table.min.js"></script>

<div id="toolbar">
  <button id="button" class="btn btn-secondary">load</button>
</div>
<table
  id="table"
  data-toggle="table"
  data-height="460">
  <thead>
    <tr>
      <th data-field="id">ID</th>
      <th data-field="name">Item Name</th>
      <th data-field="price">Item Price</th>
    </tr>
  </thead>
</table>

<script>
  var $table = $('#table')
  var $button = $('#button')

  function randomData() {
    var startId = ~~(Math.random() * 100)
    var rows = []

    for (var i = 0; i < 10; i++) {
      rows.push({
        id: startId + i,
        name: 'test' + (startId + i),
        price: '$' + (startId + i)
      })
    }
    return rows
  }

  $(function() {
  var someData = randomData();
     
     debugger;
     $table.bootstrapTable('load', someData)
  
    
  })
</script>

2 Comments

what should i do in my case?
i am stuck at displaying data in table.
0

You will need to modify your code as below. HTML

<table id = 'tble' data-toggle="tble">
<thead>
<tr>
  <th data-field="one">ID</th>
  <th data-field="two">f name</th>
  <th data-field="three">last name</th>
</tr>
</thead>
</table>

and JS

$(document).ready(function () {
$('#table').bootstrapTable({
            url: 'my.php',
            pagination: true,
            search: true,
    columns: [{
    field: 'one',
    title: 'ID'
             }, {
       field: 'two',
      title: 'f name'
   }, {
     field: 'three',
      title: 'last name'
    }]
  })
   });

Note: your URL should return a json response. Else instead of getJSON you can use $.ajax

4 Comments

no it created a table new one i want to insert data in that html table
how are you creating that table?
create static head using html as i have given my html code
Yes, But you are missing <thead> tag which seems necessary for bootstrap table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.