Reputation: 2613
I want to create a table using bootstrap table. It seems that bootstrap creates the correct amount of rows according to my json data input, but it doesn't populate it with the data itself.
// Javascript
function getStocksData () {
$.ajax('getStocksAvailable/', {
method: 'GET',
async: "True",
dataType: "json",
success: function (response) {
var received_data = response.data;
console.log(received_data);
$('#stocksTable').bootstrapTable({
columns: [{
field: 'stockName',
title: 'Company'
}, {
field: 'priceLast',
title: 'Last'
}],
data: received_data
});
}
});
};
getStocksData();
// html
<div id="tableWrapper">
<table id="stocksTable"></table>
</div>
This is the currently rendered output:
And the json data:
Might the format of the json cause the issue, so to say that bootstrap is not able to enter into the indexes of the json items? If so, how to work around this? Any way to remove the id_s from the json in order to enable bootstrap to read the data?
I don't get any errors in the console.
Upvotes: 0
Views: 148
Reputation: 18249
It seems that the bootstrapTable
method expects the data
passed to it to be an array of objects (which yours is), where each object contains the properties specified in the field
properties of the columns you specify. Unfoe, your data doesn't fit this pattern, as the objects with the properties you want are held within the fields
property of the top level objects.
If you can't change how your API returns this data, the solution on the javascript side is to use map
to get those "field" objects to the top level, like this:
data: received_data.map(item => item.fields)
Upvotes: 1