码迷,mamicode.com
首页 > 其他好文 > 详细

jqGrid整合篇

时间:2017-08-08 00:44:29      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:add   logs   can   ==   int   box   width   column   table   

table转jqGrid—只支持单行表头

//table转jqGrid—只支持单行表头
function tableToGrid(selector, options) {
jQuery(selector).each(function() {
    if(this.grid) {return;} //Adedd from Tony Tomov
    // This is a small "hack" to make the width of the jqGrid 100%
    jQuery(this).width("99%");
    var w = jQuery(this).width();

    // Text whether we have single or multi select
    var inputCheckbox = jQuery(‘tr td:first-child input[type=checkbox]:first‘, jQuery(this));
    var inputRadio = jQuery(‘tr td:first-child input[type=radio]:first‘, jQuery(this));
    var selectMultiple = inputCheckbox.length > 0;
    var selectSingle = !selectMultiple && inputRadio.length > 0;
    var selectable = selectMultiple || selectSingle;
    //var inputName = inputCheckbox.attr("name") || inputRadio.attr("name");

    // Build up the columnModel and the data
    var colModel = [];
    var colNames = [];
    jQuery(‘th‘, jQuery(this)).each(function() {
        if (colModel.length === 0 && selectable) {
            colModel.push({
                name: ‘__selection__‘,
                index: ‘__selection__‘,
                width: 0,
                hidden: true
            });
            colNames.push(‘__selection__‘);
        } else {
            colModel.push({
                name: jQuery(this).attr("id") || jQuery.trim(jQuery.jgrid.stripHtml(jQuery(this).html())).split(‘ ‘).join(‘_‘),
                index: jQuery(this).attr("id") || jQuery.trim(jQuery.jgrid.stripHtml(jQuery(this).html())).split(‘ ‘).join(‘_‘),
                width: jQuery(this).width() || 150
            });
            colNames.push(jQuery(this).html());
        }
    });
    var data = [];
    var rowIds = [];
    var rowChecked = [];
    jQuery(‘tbody > tr‘, jQuery(this)).each(function() {
        var row = {};
        var rowPos = 0;
        jQuery(‘td‘, jQuery(this)).each(function() {
            if (rowPos === 0 && selectable) {
                var input = jQuery(‘input‘, jQuery(this));
                var rowId = input.attr("value");
                rowIds.push(rowId || data.length);
                if (input.is(":checked")) {
                    rowChecked.push(rowId);
                }
                row[colModel[rowPos].name] = input.attr("value");
            } else {
                row[colModel[rowPos].name] = jQuery(this).html();
            }
            rowPos++;
        });
        if(rowPos >0) { data.push(row); }
    });

    // Clear the original HTML table
    jQuery(this).empty();

    // Mark it as jqGrid
    jQuery(this).addClass("scroll");

    jQuery(this).jqGrid(jQuery.extend({
        datatype: "local",
        width: w,
        colNames: colNames,
        colModel: colModel,
        multiselect: selectMultiple
        //inputName: inputName,
        //inputValueCol: imputName != null ? "__selection__" : null
    }, options || {}));

    // Add data
    var a;
    for (a = 0; a < data.length; a++) {
        var id = null;
        if (rowIds.length > 0) {
            id = rowIds[a];
            if (id && id.replace) {
                // We have to do this since the value of a checkbox
                // or radio button can be anything 
                id = encodeURIComponent(id).replace(/[.\-%]/g, "_");
            }
        }
        if (id === null) {
            id = a + 1;
        }
        jQuery(this).jqGrid("addRowData",id, data[a]);
    }

    // Set the selection
    for (a = 0; a < rowChecked.length; a++) {
        jQuery(this).jqGrid("setSelection",rowChecked[a]);
    }
});
};

 使用方式

tableToGrid("#table1");
tableToGrid("#table2");
$("#table1").jqGrid(‘gridDnD‘, {connectWith: "#table2"});
$("#table2").jqGrid(‘gridDnD‘, {connectWith: "#table1"});

 

JqGrid合并列的单元格方法

//合并列的单元格方法 
//gridName 表ID,cellName 需合并列,tCellNames 需根据某行合并 [‘AreaName‘]
function jqGridMerger(gridName, cellName, tCellNames) {
    if (!tCellNames) {
        tCellNames = [];
    }
    else if (!(tCellNames instanceof Array)) {
        tCellNames = tCellNames.split(‘,‘);
    }
    tCellNames.push(cellName);

    var $gridName = $("#" + gridName + "");
    //tCellNames = tCellNames.concat([cellName]);

    //得到显示到界面的id集合 
    var mya = $gridName.getDataIDs();
    //当前显示多少条 
    var length = mya.length;
    for (var i = 0; i < length; i++) {
        //从上到下获取一条信息 
        var before = $gridName.jqGrid(‘getRowData‘, mya[i]);
        //定义合并行数 
        var rowSpanTaxCount = 1;
        for (j = i + 1; j <= length; j++) {
            //和上边的信息对比 如果值一样就合并行数+1 然后设置rowspan 让当前单元格隐藏 
            var end = $gridName.jqGrid(‘getRowData‘, mya[j]);
            if (MergerCondition(before, end, tCellNames)) {//同一个城市的数据  before[tCellName] == end[tCellName]
                rowSpanTaxCount++;
                $gridName.setCell(mya[j], cellName, ‘‘, { display: ‘none‘ });
            } else {
                rowSpanTaxCount = 1;
                break;
            }
            $("#" + cellName + "" + mya[i] + "", $gridName).attr("rowspan", rowSpanTaxCount);
        }
    }
}
//合并列的单元格 - 根据行条件判断
function MergerCondition(before, end, tCellNames) {
    //if (tCellNames && tCellNames.length > 0) {
    for (var i = 0; i < tCellNames.length; i++) {
        if (before[tCellNames[i]] != end[tCellNames[i]]) {
            return false
        }
    }
    return true;
}

合并列的单元格方法需结合设置ID使用,使用方法如下:

 $(document).ready(function() { 
            var mydata = [ 
                    { id: "1", invdate: "2007-10-01", name: "test_test_test_test_test", note: "note", amount: "200.00", tax: "10.00", closed: true, ship_via: "TN", total: "210.00" }, 
                    { id: "2", invdate: "2007-10-02", name: "test2222222222222222", note: "note2", amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00" }, 
                    { id: "3", invdate: "2007-09-01", name: "test3", note: "note3", amount: "400.00", tax: "30.00", closed: false, ship_via: "FE", total: "430.00" }, 
                    { id: "4", invdate: "2007-10-04", name: "test4444444444444444", note: "note4", amount: "200.00", tax: "10.00", closed: true, ship_via: "TN", total: "210.00" }, 
                    { id: "5", invdate: "2007-10-31", name: "test5", note: "note5", amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00" }, 
                    { id: "6", invdate: "2007-09-06", name: "test6", note: "note6", amount: "400.00", tax: "30.00", closed: false, ship_via: "FE", total: "430.00" }, 
                    { id: "7", invdate: "2007-10-04", name: "test7", note: "note7", amount: "300.00", tax: "10.00", closed: true, ship_via: "TN", total: "210.00" }, 
                    { id: "8", invdate: "2007-10-03", name: "test8", note: "note8", amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00" }, 
                    { id: "9", invdate: "2007-09-01", name: "test9", note: "note9", amount: "400.00", tax: "30.00", closed: false, ship_via: "TN", total: "430.00" }, 
                    { id: "10", invdate: "2007-09-08", name: "test10", note: "note10", amount: "500.00", tax: "30.00", closed: true, ship_via: "TN", total: "530.00" }, 
                    { id: "11", invdate: "2007-09-08", name: "test11", note: "note11", amount: "500.00", tax: "30.00", closed: false, ship_via: "FE", total: "530.00" }, 
                    { id: "12", invdate: "2007-09-10", name: "test12", note: "note12", amount: "300.00", tax: "30.00", closed: false, ship_via: "FE", total: "530.00" }, 
                    { id: "13", invdate: "2007-10-01", name: "test_test_test_test_test", note: "note", amount: "200.00", tax: "10.00", closed: true, ship_via: "TN", total: "210.00" }, 
                    { id: "14", invdate: "2007-10-02", name: "test2222222222222222", note: "note2", amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00" }, 
                    { id: "15", invdate: "2007-09-01", name: "test3", note: "note3", amount: "400.00", tax: "30.00", closed: false, ship_via: "FE", total: "430.00" }, 
                    { id: "16", invdate: "2007-10-04", name: "test4444444444444444", note: "note4", amount: "200.00", tax: "10.00", closed: true, ship_via: "TN", total: "210.00" }, 
                    { id: "17", invdate: "2007-10-31", name: "test5", note: "note5", amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00" }, 
                    { id: "18", invdate: "2007-09-06", name: "test6", note: "note6", amount: "500.00", tax: "30.00", closed: false, ship_via: "FE", total: "430.00" }, 
                    { id: "19", invdate: "2007-10-04", name: "test7", note: "note7", amount: "200.00", tax: "10.00", closed: true, ship_via: "TN", total: "210.00" }, 
                    { id: "20", invdate: "2007-10-03", name: "test8", note: "note8", amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00" }, 
                    { id: "21", invdate: "2007-09-01", name: "test9", note: "note9", amount: "400.00", tax: "30.00", closed: false, ship_via: "TN", total: "430.00" }, 
                    { id: "22", invdate: "2007-09-08", name: "test10", note: "note10", amount: "500.00", tax: "30.00", closed: true, ship_via: "TN", total: "530.00" }, 
                    { id: "23", invdate: "2007-09-08", name: "test11", note: "note11", amount: "500.00", tax: "30.00", closed: false, ship_via: "FE", total: "530.00" }, 
                    { id: "24", invdate: "2007-09-10", name: "test12", note: "note12", amount: "500.00", tax: "30.00", closed: false, ship_via: "FE", total: "530.00" } 
                ], 
                grid = $("#list"); 
 
            grid.jqGrid({ 
                datatype: ‘local‘, 
                data: mydata, 
                colNames: [‘Inv No‘, ‘Date‘, ‘Client A‘, ‘Client B‘, ‘Amount‘, ‘Tax‘, ‘Total‘, ‘Closed‘, ‘Shipped via‘, ‘Notes‘], 
                colModel: [ 
                    { name: ‘id‘, index: ‘id‘, width: 70, align: ‘center‘, sorttype: ‘int‘ }, 
                    { name: ‘invdate‘, index: ‘invdate‘, width: 80, align: ‘center‘, sorttype: ‘date‘, 
                        formatter: ‘date‘, formatoptions: { newformat: ‘d-M-Y‘ }, datefmt: ‘d-M-Y‘ 
                    }, 
                    { name: ‘name‘, index: ‘name‘, width: 70, 
                        cellattr: function(rowId, tv, rawObject, cm, rdata) { 
                            //合并单元格 
                            return ‘id=\‘name‘ + rowId + "\‘"; 
                            //if (Number(rowId) < 5) { return ‘ colspan=2‘ } 
                        } 
                    }, 
                    { name: ‘nameB‘, index: ‘nameB‘, width: 70, 
                        cellattr: function(rowId, tv, rawObject, cm, rdata) { 
                            //if (Number(rowId) < 5) { return ‘ style="display:none;"‘ } 
                        } 
                    }, 
                    { name: ‘amount‘, index: ‘amount‘, width: 100, formatter: ‘number‘, align: ‘right‘, 
                        cellattr: function(rowId, tv, rawObject, cm, rdata) { 
                            //合并单元格 
                            return ‘id=\‘amount‘ + rowId + "\‘"; 
                        } 
                    }, 
                    { name: ‘tax‘, index: ‘tax‘, width: 70, formatter: ‘number‘, align: ‘right‘, 
                        cellattr: function(rowId, tv, rawObject, cm, rdata) { 
                            //合并单元格 
                            return ‘id=\‘tax‘ + rowId + "\‘"; 
                        } 
                    }, 
                    { name: ‘total‘, index: ‘total‘, width: 120, formatter: ‘number‘, align: ‘right‘, 
                        cellattr: function(rowId, tv, rawObject, cm, rdata) { 
                            //合并单元格 
                            return ‘id=\‘total‘ + rowId + "\‘"; 
                        } 
                    }, 
                    { name: ‘closed‘, index: ‘closed‘, width: 110, align: ‘center‘, formatter: ‘checkbox‘, 
                        edittype: ‘checkbox‘, editoptions: { value: ‘Yes:No‘, defaultValue: ‘Yes‘ } 
                    }, 
                    { name: ‘ship_via‘, index: ‘ship_via‘, width: 120, align: ‘center‘, formatter: ‘select‘, 
                        edittype: ‘select‘, editoptions: { value: ‘FE:FedEx;TN:TNT;IN:Intim‘, defaultValue: ‘Intime‘ }, 
                        //①给当前想合并的单元格设置id 
                        cellattr: function(rowId, tv, rawObject, cm, rdata) { 
                            return ‘id=\‘ship_via‘ + rowId + "\‘"; 
                        } 
                    }, 
                    { name: ‘note‘, index: ‘note‘, width: 100, sortable: false } 
                ], 
                rowNum: 15, 
                rowList: [10, 15, 20, 30], 
                pager: ‘#pager‘, 
                gridview: true, 
                rownumbers: true, 
                sortname: ‘invdate‘, 
                viewrecords: true, 
                sortorder: ‘desc‘, 
                caption: ‘Just simple local grid‘, 
                height: ‘100%‘, 
                gridComplete: function() { 
                    //②在gridComplete调用合并方法 
                    var gridName = "list"; 
                    jqGridMerger(gridName, ‘amount‘); 
                    jqGridMerger(gridName, ‘tax‘); 
                    jqGridMerger(gridName, ‘total‘); 
                    jqGridMerger(gridName, ‘name‘); 
                    jqGridMerger(gridName, ‘ship_via‘); 
                    
                    /*根据某几列合并*/
                    // jqGridMerger(gridName, ‘total‘,[‘amount‘,‘tax‘]); 
                } 
 
            }); 

    

/*根据某几列合并*/
// jqGridMerger(gridName, ‘total‘,[‘amount‘,‘tax‘]);

 

jqGrid整合篇

标签:add   logs   can   ==   int   box   width   column   table   

原文地址:http://www.cnblogs.com/elves/p/7302383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!