Javascript 如何动态创建引导表?
单击按钮开发引导表时遇到问题。我的目标是为表中的每一列添加排序功能。所以对于我的用户案例,当用户单击按钮时,它调用ajax从数据库中获取数据并发送回结果(它可以正常工作,所以不用担心)。因此,我需要将结果显示为一个引导表,以便对每一列使用排序功能。另外,我正在使用pagination.js,它使用json对象数组和表来显示结果。 当我开发表并将其附加到div中时,我看不到每列上的排序功能 当我创建一个简单的硬代码HTML表,该表带有引导表属性,如sort feature(data sortable=“true”)。我相信,当页面加载引导程序时,会检测html正文中的内容及其属性。但是,当我动态创建引导表时,HTML表就在那里,而不是引导表特性 请帮忙。下面是我如何使用javascript函数开发表的代码Javascript 如何动态创建引导表?,javascript,jquery,html,pagination,bootstrap-table,Javascript,Jquery,Html,Pagination,Bootstrap Table,单击按钮开发引导表时遇到问题。我的目标是为表中的每一列添加排序功能。所以对于我的用户案例,当用户单击按钮时,它调用ajax从数据库中获取数据并发送回结果(它可以正常工作,所以不用担心)。因此,我需要将结果显示为一个引导表,以便对每一列使用排序功能。另外,我正在使用pagination.js,它使用json对象数组和表来显示结果。 当我开发表并将其附加到div中时,我看不到每列上的排序功能 当我创建一个简单的硬代码HTML表,该表带有引导表属性,如sort feature(data sortabl
// 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;
}
//当ajax发送响应时,result是一个大的结果字符串。
函数displayResult(结果){
//将结果转换为JSON数组
var splitResult=result.split(“*split*”);
var getLast=splitResult.length-1;
对于(var i=0;i
当使用这种方法时,它只显示html表。不使用引导表。我正在尝试添加一个功能,用户可以单击列标题进行排序。希望此代码可以帮助您,它的作用是 1.显示一个文本框,用户可以在其中输入db表名 2.后端(在我的例子中是Python)从db_name_表中获取信息 3.数据以数字方式显示在引导表中 HTML
$(文档).ready(函数(){
var i=1;
$('#tab_logic')。on('focusout','tbody tr:nth last child(2)td:nth last child(2)input',function(){
if($(this.val()!=''&$('#tab#logic tbody tr:n最后一个孩子(2)td:n最后一个孩子(3)input')。val()!=''&&$('#tab#logic tbody tr:n最后一个孩子(2.product')。val()!=''){
b=i-1;
$('addr'+i).html($('addr'+b.html()).find('td:first child').html(i+1);
$('#tab_logic')。追加('');
$(this.focus().select();
i++;
}
});
$('body')。在('click','delete#row',function()上{
如果(i>1){
var id=i;
$(“#tab_logic tbody tr:n最后一个子项(2)”.remove();
$('#tab#logic tbody tr:last').attr(“id”,“addr”+(id-1));
我--;
}
计算();
});
$('#tab_logic tbody')。在('keyup change',function()上{
计算();
});
$('#tax')。on('focusout',function(){
var total=parseInt($('sub#u total').val();
var tax_sum=eval(总计/100*$(“#tax”).val();
$('tax#amount').val(tax#sum.toFixed(2));
$('总额').val((税额+总额).toFixed(2));
});
});
函数计算(){
$('#tab_logic tbody tr')。每个(函数(i,元素){
var html=$(this.html();
如果(html!=''){
var qty=$(this.find('.qty').val();
var price=$(this.find('.price').val();
$(this).find('.total').val(数量*价格);
计算总数();
}
});
}
函数计算总计(){
总数=0;
$('.total')。每个(函数(){
total+=parseInt($(this.val());
});
$(‘小计’).val(total.toFixed(2));
税总额=总额/100*$(“#税”).val();
$('tax#amount').val(tax#sum.toFixed(2));
$('总额').val((税额+总额).toFixed(2));
}
请不要只发布代码作为答案,还要解释代码的作用以及如何解决问题
<form method="GET" id="id1" action="{% url 'request_page' %}">
<input type="text" value="db_name_table" name="mytextbox" id='mytextbox' size="1"/>
<input type="submit" class="btn" value="Click" name="mybtn">
</form>
$(document).ready( function(){
$('#id1').submit( function(e){
e.preventDefault();
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
data: $(this).serialize(),
success:function( value ){
$('#datatable').bootstrapTable({
striped: true,
pagination: true,
showColumns: true,
showToggle: true,
showExport: true,
sortable: true,
paginationVAlign: 'both',
pageSize: 25,
pageList: [10, 25, 50, 100, 'ALL'],
columns: {{ columns|safe }},
data: {{ data|safe }},
});
}
})
})
});
$(document).ready(function() {
var i = 1;
$('#tab_logic').on('focusout', 'tbody tr:nth-last-child(2) td:nth-last-child(2) input', function() {
if ($(this).val() != '' && $('#tab_logic tbody tr:nth-last-child(2) td:nth-last-child(3) input').val() != "" && $('#tab_logic tbody tr:nth-last-child(2) .product').val() != '') {
b = i - 1;
$('#addr' + i).html($('#addr' + b).html()).find('td:first-child').html(i + 1);
$('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
$(this).focus().select();
i++;
}
});
$('body').on('click', '#delete_row', function() {
if (i > 1) {
var id = i;
$('#tab_logic tbody tr:nth-last-child(2)').remove();
$('#tab_logic tbody tr:last').attr("id", "addr" + (id - 1));
i--;
}
calc();
});
$('#tab_logic tbody').on('keyup change', function() {
calc();
});
$('#tax').on('focusout', function() {
var total = parseInt($('#sub_total').val());
var tax_sum = eval(total / 100 * $('#tax').val());
$('#tax_amount').val(tax_sum.toFixed(2));
$('#total_amount').val((tax_sum+total).toFixed(2));
});
});
function calc() {
$('#tab_logic tbody tr').each(function(i, element) {
var html = $(this).html();
if (html != '') {
var qty = $(this).find('.qty').val();
var price = $(this).find('.price').val();
$(this).find('.total').val(qty * price);
calc_total();
}
});
}
function calc_total() {
total = 0;
$('.total').each(function() {
total += parseInt($(this).val());
});
$('#sub_total').val(total.toFixed(2));
tax_sum = total / 100 * $('#tax').val();
$('#tax_amount').val(tax_sum.toFixed(2));
$('#total_amount').val((tax_sum + total).toFixed(2));
}