分享一下jqgrid合并单元格

转载 2017年01月03日 10:08:14

原文:http://bbs.blueidea.com/thread-3067461-1-1.html


Merger这个方法大家可以直接调用 里边都不用修改  参数1为jqgrid的id  参数2为列名 只需关注1和2
主要三个步骤 代码里标明了

就这么简单
phone1

phone2


大伙运行试试

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Just simple local grid</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/themes/redmond/jquery-ui.css" />
    <link rel="stylesheet" type="text/css" href="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.0.0/css/ui.jqgrid.css" />

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

    <script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.0.0/js/i18n/grid.locale-en.js"></script>

    <script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.0.0/js/jquery.jqGrid.src.js"></script>

    <script type="text/javascript">
        //<![CDATA[
        $(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";
                    Merger(gridName, 'amount');
                    Merger(gridName, 'tax');
                    Merger(gridName, 'total');
                    Merger(gridName, 'name');
                    Merger(gridName, 'ship_via');
                }

            });

            //公共调用方法
            function Merger(gridName, 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 (before[CellName] == end[CellName]) {
                            rowSpanTaxCount++;
                            $("#" + gridName + "").setCell(mya[j], CellName, '', { display: 'none' });
                        } else {
                            rowSpanTaxCount = 1;
                            break;
                        }
                        $("#" + CellName + "" + mya[i] + "").attr("rowspan", rowSpanTaxCount);
                    }
                }
            }
        });
        
        
        //]]>

    </script>

</head>
<body>
    <table id="list">
        <tr>
            <td />
        </tr>
    </table>
    <div id="pager">
    </div>
</body>
</html>


C#中使用EventLog类写Windows事件日志

在程序中经常需要将指定的信息(包括异常信息和正常处理信息)写到日志中。从C#3.0开始可以使用EventLog类(在System.Diagnostics命名空间中)将各种信息写入Windows日志。在...

jqgrid表头合并和行合并,基于jquery的脚本插件

/* * jquery tui tablespan plugin 0.2 * * Copyright (c) 2010 china yewf * * Dual licensed unde...

jqgrid合并单元格

根据这个的启发 How to merge cells in jqGrid 4.0 参考这里:jqgrid官方的事件和方法 http://www.trirand.com/jqgridwik ...

JQGRID格式化/合并单元格/滚动条问题和多重子表介绍及datepicker参数使用详解(jqgrid表格中应用)

jqgrid格式化字符串(整型、数字、货币、日期、显示连接) jqGrid的格式化是定义在语言包中的JS中的,有如下已定义好的类型 formatter : {       integer : {th...

jqgrid-1获取或设置行及单元格的值

JqGrid官网:http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options 1、获取选中行ID         se...

jqGrid单元格编辑配置,事件及方法

单元格编辑 单元格编辑提供给用户修改一行中的单个单元格内容的功能,开发者可以通过ajax或者单元格编辑事件对修改的数据进行处理。 通过下面的行为,单元格编辑支持键盘导航和执行单元格编辑...

jqGrid显示数据,属性,多重表头,子表,行编辑,单元格编辑等 基本

第一次发帖 可能格式有点差  见谅! JavaScipt代码: var lastsel3; //选择一行进行编辑时用到的 $(function(){ $("#list").jqG...

Jqgrid插件实现单元格编辑,以及弹出选择数据后赋值。

这段时间因为做了个erp系统,要求能实现表格内编辑数据保存,于是采用了文档非常坑的jqgrid。 不多废话,跳过前戏。 过程中遇到如下几个坑: 1、保存的时候最后编辑的行数据无法保存; 2、自...

jqGrid学习总结_1 获取或设置行及单元格的值

JqGrid官网:http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options 1、获取选中行ID         se...

jqgrid 单元格数据格式化 formatter formatoptions

jqGrid中对列表cell属性格式化设置主要通过colModel中formatter、formatoptions来设置.jQuery("#jqGrid_id").jqGrid({ ... co...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:分享一下jqgrid合并单元格
举报原因:
原因补充:

(最多只允许输入30个字)