1. 程式人生 > 實用技巧 >bootstrap table 合併列(只合並相鄰的相同項)

bootstrap table 合併列(只合並相鄰的相同項)

/**
 * 
 * @param bstable    divid
 * @param fieldname  要合併的列屬性名
 */
function mergeTable(bstable, fieldname) {
    var rows = bstable.bootstrapTable('getData');
    var rowspan = 0;
    var index = 0;
    var first_yjzbmc = '';
    $.each(rows, function (i, row) {
        if (i == 0) {
            first_yjzbmc 
= row[fieldname]; rowspan = 1; index = i; } else { if (first_yjzbmc === row[fieldname]) { rowspan++; } else { $(bstable).bootstrapTable('mergeCells', { index: index, field: fieldname, colspan: 1, rowspan: rowspan }); first_yjzbmc
= row[fieldname]; rowspan = 1; index = i; } if (i == (rows.length - 1)) { $(bstable).bootstrapTable('mergeCells', { index: index, field: fieldname, colspan: 1, rowspan: rowspan }); } } }); }

呼叫:

 onLoadSuccess:function(data){
            mergeTable($(
'#mianTable'),"qyname"); mergeTable($('#mianTable'),"sblxName"); },

效果: