jqgrid - 调整大小和叠加

时间:2011-12-29 17:23:50

标签: jquery jqgrid

HTML

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Jquery Grid</title>
        <link rel="stylesheet" type="text/css" href="themes/redmond/jquery-ui-1.8.1.custom.css" />
        <link rel="stylesheet" type="text/css" href="themes/ui.jqgrid.css" />
        <link rel="stylesheet" type="text/css" href="themes/ui.multiselect.css" />
        <script src="js/jquery-1.6.4.js" type="text/javascript"></script>
        <script src="js/jquery-ui-1.8.14.custom.min.js" type="text/javascript"></script>
        <script src="js/jquery.layout.js" type="text/javascript"></script>
        <script src="js/i18n/grid.locale-en.js" type="text/javascript"></script>
        <script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
        <script src="plugins/ui.multiselect.js" type="text/javascript"></script>
        <script src="plugins/jquery.tablednd.js" type="text/javascript"></script>
        <script src="plugins/jquery.contextmenu.js" type="text/javascript"></script>
        <style>
            .ui-jqgrid .ui-jqgrid-bdiv {
                overflow: inherit !important;
            }
            .overlay{
                position: absolute;
                border: 1px solid black;
                background-color: black;
                color: #FFFFFF;
            }
        </style>
    </head>
    <body>
         <table id="myjqgrid"></table>
         <div id="Pager"></div>
    </body>
</html>

JSON

{
    "colModel": [
        {
            "name": "Overlay",
            "label": "Overlay",
            "width": 60,
            "align": "left",
            "jsonmap": "cells.0.value",
            "sortable": true      
        },
        {
            "name": "ID",
            "label": "ID",
            "width": 60,
            "align": "left",
            "jsonmap": "cells.1.value",
            "sortable": true      
        },
        {
            "name": "FirstName",
            "label": "FirstName",
            "width": 100,
            "align": "left",
            "jsonmap": "cells.2.value",
            "sortable": false       
        },
        {
            "name": "LastName",
            "label": "LastName",
            "width": 100,
            "align": "left",
            "jsonmap": "cells.3.value",
            "sortable": false       
        }
    ],
    "colNames": [
        "Overlay",
        "ID",
        "FirstName",
        "LastName"
    ],
    "mypage": {
        "outerwrapper": {
            "page":"1",
            "total":"1",
            "records":"20",
            "innerwrapper": {
                "rows":[
                    {
                        "id":"1",
                        "cells":
                        [               
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },
                            {
                                "value":"12345",
                                "label": "ID"                       
                            },
                            {
                                "value":"David",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Smith",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"2",
                        "cells":
                        [               
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },
                            {
                                "value":"37546",
                                "label": "ID"                       
                            },
                            {
                                "value":"Willy",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Peacock",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"3",
                        "cells":
                        [               
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },
                            {
                                "value":"62345",
                                "label": "ID"                       
                            },
                            {
                                "value":"Kim",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Holmes",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"4",
                        "cells":
                        [  
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },             
                            {
                                "value":"186034",
                                "label": "ID"                       
                            },
                            {
                                "value":"Andy",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Wills",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"5",
                        "cells":
                        [
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },               
                            {
                                "value":"67345",
                                "label": "ID"                       
                            },
                            {
                                "value":"Paul",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Lawrence",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"6",
                        "cells":
                        [
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },               
                            {
                                "value":"12906",
                                "label": "ID"                       
                            },
                            {
                                "value":"Andy",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Charlery",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"7",
                        "cells":
                        [ 
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },              
                            {
                                "value":"564565",
                                "label": "ID"                       
                            },
                            {
                                "value":"Bets",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Josilyn",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"8",
                        "cells":
                        [
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },               
                            {
                                "value":"34345",
                                "label": "ID"                       
                            },
                            {
                                "value":"Pink",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Floyd",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"9",
                        "cells":
                        [ 
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },              
                            {
                                "value":"3429",
                                "label": "ID"                       
                            },
                            {
                                "value":"Rose",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Ben",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"10",
                        "cells":
                        [
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },               
                            {
                                "value":"6668",
                                "label": "ID"                       
                            },
                            {
                                "value":"Amy",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Wills",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"11",
                        "cells":
                        [
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },               
                            {
                                "value":"22786",
                                "label": "ID"                       
                            },
                            {
                                "value":"Zarine",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Khan",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"12",
                        "cells":
                        [
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },               
                            {
                                "value":"56444",
                                "label": "ID"                       
                            },
                            {
                                "value":"Dolly",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Mathews",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"13",
                        "cells":
                        [ 
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },              
                            {
                                "value":"99453",
                                "label": "ID"                       
                            },
                            {
                                "value":"Ginger",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Wills",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"14",
                        "cells":
                        [
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },               
                            {
                                "value":"1234",
                                "label": "ID"                       
                            },
                            {
                                "value":"Fabrice",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Papa",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"15",
                        "cells":
                        [
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },               
                            {
                                "value":"45000",
                                "label": "ID"                       
                            },
                            {
                                "value":"Helen",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Kites",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"16",
                        "cells":
                        [
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },               
                            {
                                "value":"1255",
                                "label": "ID"                       
                            },
                            {
                                "value":"Barbara",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Dorris",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"17",
                        "cells":
                        [
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },               
                            {
                                "value":"5555",
                                "label": "ID"                       
                            },
                            {
                                "value":"Eugene",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Mark",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"18",
                        "cells":
                        [ 
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },              
                            {
                                "value":"1867",
                                "label": "ID"                       
                            },
                            {
                                "value":"Harry",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"King",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"19",
                        "cells":
                        [ 
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },              
                            {
                                "value":"45888",
                                "label": "ID"                       
                            },
                            {
                                "value":"Lorraine",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Williams",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"20",
                        "cells":
                        [ 
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },              
                            {
                                "value":"4567777",
                                "label": "ID"                       
                            },
                            {
                                "value":"Mark",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Sheppard",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    },
                    {
                        "id":"21",
                        "cells":
                        [ 
                            {
                                "value":"Click Me!",
                                "label": "Overlay"                       
                            },              
                            {
                                "value":"7777",
                                "label": "ID"                       
                            },
                            {
                                "value":"Davis",
                                "label": "FirstName"    
                            },
                            {                           
                                "value":"Sheppard",
                                "label": "LastName"                         
                            }                                                                                       
                        ]       
                    }
                ]
            }
        }
    }
}

JQGrid定义

$(document).ready(function () { 
    $.ajax({
        type: "GET",
        url: "myjqgrid.json",
        data: "",
        dataType: "json",
        success: function(response){
            var columnData = response.mypage.outerwrapper,
                columnNames = response.colNames,
                columnModel = response.colModel;

            $("#myjqgrid").jqGrid({
                datatype: 'jsonstring',
                datastr: columnData,                
                colNames: columnNames,
                colModel: columnModel,
                jsonReader: {
                    root: "innerwrapper.rows",              
                    repeatitems: false
                },
                gridview: true,
                pager: "#Pager",
                rowNum: 21,
                rowList: [21],
                viewrecords: true,              
                recordpos: 'left',
                multiboxonly: true,
                multiselect: true,
                sortname: 'ID',
                sortorder: "desc",  
                sorttype: "text",   
                sortable: true,
                caption: "<h2>MY JQGRID</h2>",
                width: "1406",      
                height: "100%",
                scrolloffset: 0,    
                loadonce: true,     
                cache: true,
                loadComplete: function(){
                    $("td[title = 'Click Me!']").live("click", function(){
                        $("div.overlay").remove();
                        var RowID = $(this).closest("tr").attr("id");
                        if ($(this).siblings("div").length == 0) {
                            $(this).closest("td").append("<div class='overlay' id='"+RowID+"'>This is an overlay.</div>");
                            $(this).siblings("div#" + RowID).css("display", "block");
                        }   
                    })
                }
            });
            $("#myjqgrid").jqGrid('navGrid','#Pager', {add:false, edit:false, del:false, position: 'right'});
            $("#myjqgrid").jqGrid('gridResize',{minWidth:800,maxWidth:1405,minHeight:350,maxHeight:680});               
        }
    });
});

我遇到的问题

  • 当我调整网格大小时,分页工作正常如果我不更改CSS

  • 如果我更改CSS FROM .ui-jqgrid .ui-jqgrid-bdiv {overflow: auto;} TO .ui-jqgrid .ui-jqgrid-bdiv {overflow: inherit !important;}分页栏会在记录之间流动

  • 我更改CSS的原因是我点击“点击我”时会显示一个叠加层链接在每行的第二列。更改CSS会正确显示页面上最后一条记录的叠加层。如果我不更改CSS,则会隐藏页面上最后一条记录的叠加层。

因此,

我不会更改CSS

  • 当我调整网格大小时,分页工作正常
  • 隐藏页面上最后一条记录的叠加层

我更改了CSS

  • 当我调整网格大小时,在记录之间显示分页栏
  • 正确显示页面上最后记录的叠加层

我尝试使用CSS定位和z-index进行叠加,但它不起作用。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您需要执行以下操作

onCellSelect: function (rowid, iCol, cellcontent, e) {
    $("#myoverlay").remove();
    if (this.p.colModel[iCol].name === 'Overlay') { // iCol === 1
        $('<div class="overlay" id="myoverlay">This is an overlay.</div>').css({
            top: e.pageY,
            left: e.pageX
        }).appendTo('body');
    }
}

而不是loadComplete中的代码。重要的是,上面的代码会将d​​iv附加到<body>而不是<td>元素。可以使用点击的event将div放在相应的位置。

请参阅演示here

您可以轻松修改代码以使用div的内容,具体取决于rowidcellcontent或任何其他条件。

顺便说一下。在您使用的HTML代码中,您应该在<!DOCTYPE html ...之前使用<html>,并在type="text/css"中使用<style>属性。 jqGrid没有cache: true选项。