我試圖使用bootstrap-table插件在表中呈現我的json數據。數據成功加載,但插件不會將數據呈現到表中,它只是說No matching records found
。Bootstrap-Table不會呈現JSON數據(但會成功加載它)
我一直跟隨在文檔的例子,試圖用像load
和refresh
方法,雖然根據the example I almost copy-pasted,你不需要使用任何方法來加載和渲染數據,你只需要指定table
標記中的data-url
屬性,或者在js文件中的表對象上添加url
屬性。我嘗試了兩種變體,似乎都沒有工作。
這裏是我如何定義我的表:
<h1>Table</h1>
<div id="toolbar">
<button id="remove" class="btn btn-danger" disabled="">
<i class="glyphicon glyphicon-remove"></i>
Delete
</button>
</div>
<table
id="table"
data-url="/books/all"
data-toolbar="#toolbar"
data-search="true"
data-sortable="true"
data-show-refresh="true"
data-show-toggle="true"
data-show-columns="true"
data-show-export="true"
data-detail-view="true"
data-detail-formatter="detailFormatter"
data-minimum-count-columns="2"
data-show-pagination-switch="true"
data-pagination="true"
data-id-field="id"
data-page-list="[10, 25, 50, 100, ALL]"
data-show-footer="false"
data-side-pagination="server"
data-response-handler="responseHandler">
</table>
的/books/all
返回JSON數據是這樣的:
[{"id":42
"name":"whatever",
"description":"whatever"
"cover_img":"https://whatever.jpg"
"available_count":10,
"price":6.99,
"author_id":21,
"publisher_id":5,
"author_first_name":"Harper",
"author_last_name":"Lee",
"author_birthday":"1926-04-27T22:00:00.000Z",
"publisher_name":"Penguin Fiction"},...]
我定義JS我的專欄:
let $table = $('#table'),
$remove = $('#remove'),
selections = [];
const initTable =() => {
$table.bootstrapTable({
url: '/books/all',
height: getHeight(),
columns: [
[
{
field: 'state',
checkbox: true,
rowspan: 2,
align: 'center',
valign: 'middle'
}, {
title: 'Book ID',
field: 'id',
rowspan: 2,
align: 'center',
valign: 'middle',
sortable: true,
footerFormatter: totalTextFormatter
}, {
title: 'Book Detail',
colspan: 3,
align: 'center'
}
],
[
{
field: 'name',
title: 'Book Name',
sortable: true,
editable: true,
align: 'center',
footerFormatter: totalNameFormatter
}, {
field: 'price',
title: 'Book Price',
sortable: true,
align: 'center',
editable: {
type: 'text',
title: 'Book Price',
validate(value) {
value = $.trim(value);
if (!value) {
return 'This field is required';
}
if (!/^\$/.test(value)) {
return 'This field needs to start with $';
}
const data = $table.bootstrapTable('getData'),
index = $(this).parents('tr').data('index');
console.log(data[index]);
return '';
}
},
footerFormatter: totalPriceFormatter
}, {
field: 'operate',
title: 'Book Operate',
align: 'center',
events: operateEvents,
formatter: operateFormatter
}
]
]
});
setTimeout(() => {
$table.bootstrapTable('resetView');
}, 200);
$table.on('check.bs.table uncheck.bs.table ' +
'check-all.bs.table uncheck-all.bs.table',
() => {
$remove.prop('disabled', !$table.bootstrapTable('getSelections').length);
selections = getIdSelections();
});
$table.on('expand-row.bs.table', (e, index, row, $detail) => {
if (index % 2 == 1) {
$detail.html('Loading from ajax request...');
$.get('LICENSE', res => {
$detail.html(res.replace(/\n/g, '<br>'));
});
}
});
$table.on('all.bs.table', (e, name, args) => {
console.log(name, args);
});
$remove.click(() => {
const ids = getIdSelections();
$table.bootstrapTable('remove', {
field: 'id',
values: ids
});
$remove.prop('disabled', true);
});
$(window).resize(() => {
$table.bootstrapTable('resetView', {
height: getHeight()
});
});
};
function getIdSelections() {
return $.map($table.bootstrapTable('getSelections'), row => row.id)
}
function responseHandler(res) {
$.each(res.rows, (i, row) => {
row.state = $.inArray(row.id, selections) !== -1;
});
return res;
};
的load-success.bs.table
事件每次刷新頁面或ta時都會收到數據BLE。 responseHandle
函數也被觸發,並接收相同的有效數據。
的JSON格式數據是有效的,如果我只是從/books/all
請求複製的響應,並將其粘貼到在bootstrapTable初始化對象(只是columns
屬性之後)data
屬性,則數據將被正常再現。
你能幫我理解我做錯了什麼,並解決這個問題嗎?