我在开发一个bootstrap表格时遇到了问题。我的目标是为每一列添加排序功能。对于我的用户案例,当用户单击按钮时,它会调用ajax从数据库中获取数据并返回结果(它可以工作,所以不用担心)。因此,我需要将结果显示为bootstrap表格,以便我可以使用每个列的排序功能。此外,我正在使用pagination.js,它使用json对象数组和表格来显示结果。
当我开发表格并将其附加到div中时,我看不到每个列的排序功能。当我创建一个简单的硬编码HTML表格,其中包含Bootstrap-table属性,如排序功能(data-sortable =“true”)时,它可以正常工作。我相信当页面加载时,Bootstrap会检测html主体上的表格及其属性。但是,当我动态创建bootstrap表格时,虽然HTML表格存在,但没有Bootstrap表格功能。
请帮帮我。以下是我使用javascript函数开发表格的代码。
当使用这种方法时,它只会显示HTML表格,而不是使用bootstrap-table。我正在尝试添加一个功能,使用户可以单击列标题进行排序。最初的回答:使用bootstrap-table插件来实现这一功能。
当我开发表格并将其附加到div中时,我看不到每个列的排序功能。当我创建一个简单的硬编码HTML表格,其中包含Bootstrap-table属性,如排序功能(data-sortable =“true”)时,它可以正常工作。我相信当页面加载时,Bootstrap会检测html主体上的表格及其属性。但是,当我动态创建bootstrap表格时,虽然HTML表格存在,但没有Bootstrap表格功能。
请帮帮我。以下是我使用javascript函数开发表格的代码。
// result is a large string of result when ajax is sending a response.
function displayResult(result) {
//convert the result into Array of JSON
var splitResult = result.split("*split*");
var getLast = splitResult.length - 1;
for(var i = 0; i < splitResult.length; i++){
if(i != getLast) {
splitResult[i] = JSON.parse(splitResult[i]);
} else {
splitResult.splice(i,1);
}
}
// the .pagination is a function that is use in pagination.js
// the #page is a DIV
$('#page').pagination({
dataSource: splitResult,
pageSize: 8,
callback: function(data, pagination) {
// template method
var html = template(data);
// grab the html String and append it into the #demo DIV
$("#demo").append(html);
}
})
}
function template(data) {
// table tag with bootstrap-table attributes
var html = '<table data-toggle="table" style="width: 100%;">';
// create the table headers
html = html + '<thead><tr><th data-field="IDCODE" data-sortable="true">ID</th><th scope="col" data-sortable="true">ZIP 11</th><th scope="col" data-sortable="true">Date</th></tr></thead>'
+ '<tbody>';
// input the results of the data
if (data[0].IDCODE) {
$.each(data, function(index, item) {
html += '<trid="tr-id-2" class="tr-class-2"><td>'
+ item.IDCODE+'</td>'
+ '<td>' + item.ZIP11_ID + '</td>'
+ '<td>' + item.DEL01_TS + '</td></tr>';
});
} else {
$.each(data, function(index, item) {
html += '<tr><td>'+ item +'</td></tr>';
});
}
html += '</tbody></table>';
return html;
}
当使用这种方法时,它只会显示HTML表格,而不是使用bootstrap-table。我正在尝试添加一个功能,使用户可以单击列标题进行排序。最初的回答:使用bootstrap-table插件来实现这一功能。