jqgrid中的滚动条没有填充数据

时间:2013-05-02 13:45:46

标签: jquery jquery-ui jqgrid

我正在尝试使用jqgrid创建一个最初没有数据但具有固定大小的网格,其中列宽总计大于网格宽度,因此用户只能滚动标题。用户将单击一个按钮,将数据填充到网格中。这听起来可能类似于已回答的问题jqGrid vertical scrollbar

然而,在那种情况下,数据已经存在。我最初没有数据,并希望网格有滚动条。我注意到在填充数据之前,没有创建类.ui-jqgrid-bdiv的div。

这是jqgrid中的错误吗?有没有解决方法呢?

这是小提琴http://jsfiddle.net/yNw3C/2630/

以下是代码:

的javascript

$(document).ready(function () {
    $("#results").jqGrid({
        datatype: "local",
        height: 150,
        scroll:true,
        width:300,
        shrinkToFit:false,
        colNames: ['Inv No', 'Date', 'Client', 'Amount', 'Tax', 'Total', 'Notes'],
        colModel: [
            { name: 'id', index: 'id', width: 160, sorttype: "int" },
            { name: 'invdate', index: 'invdate', width: 90, sorttype: "date" },
            { name: 'name', index: 'name', width: 100 },
            { name: 'amount', index: 'amount', width: 80, align: "right", sorttype: "float" },
            { name: 'tax', index: 'tax', width: 180, align: "right", sorttype: "float" },
            { name: 'total', index: 'total', width: 280, align: "right", sorttype: "float" },
            { name: 'note', index: 'note', width: 150, sortable: false }
           ],
       });
            var mydata = [
                            { id: "1", invdate: "2007-10-01", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" },
                            { id: "2", invdate: "2007-10-02", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" },
                            { id: "3", invdate: "2007-09-01", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" },
                            { id: "4", invdate: "2007-10-04", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" },
                            { id: "5", invdate: "2007-10-05", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" },
                            { id: "6", invdate: "2007-09-06", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" },
                            { id: "7", invdate: "2007-10-04", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" },
                            { id: "8", invdate: "2007-10-03", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" },
                            { id: "9", invdate: "2007-09-01", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" }
                            ];
            //if you uncomment this, the scrollbars would appear
            /*for (var i = 0; i <= mydata.length; i++)
                $("#results").jqGrid('addRowData', i + 1, mydata[i]);*/
        });

HTML

<div>
    <table id="results"></table>
</div>

编辑: 更清楚的是,在上面的例子中,我想要水平滚动标题。填充数据时,标题在垂直滚动时应保持静态,并与数据一起水平滚动。

谢谢!

4 个答案:

答案 0 :(得分:1)

@ user1719160:

loadComplete: function () {

        if ($(this).jqGrid('getGridParam', 'reccount') == 0)
        {         
            $(".jqgfirstrow").css("height","1px");
        }

这种作品......不是很优雅......但它确实有效。

答案 1 :(得分:0)

要在垂直滚动期间保持标题可见,请参阅Oleg的回答: fixing column headers while scrolling - jqgrid

要启用标题滚动,您可以在加载完成

中执行此操作
 loadComplete: function () {
        if ($(this).jqGrid('getGridParam', 'reccount') == 0){
          $(".ui-jqgrid-hdiv").css("overflow-x", "auto")
        }
          else{
              $(".ui-jqgrid-hdiv").css("overflow-x", "hidden")
          }

    }//loadComplete

答案 2 :(得分:0)

这是我在没有数据时在jqgrid中找到hbar的最终解决方案。

loadComplete: function () {
    if ($(this).jqGrid('getGridParam', 'reccount') == 0)
    {                
        $("#grid > tbody").append("<tr id=\"my_row\" role=\"row\"><td colspan=\"15\" style=\"height:1px;\" role=\"gridcell\"></td></tr>")
    }        
},

答案 3 :(得分:0)

我知道这是一个老问题,但是如果有其他人遇到它,以下代码对我来说效果很好(将 grid_client 更改为正确名称):

loadBeforeSend: function() {
    var headerWidth = $('#gview_grid_client .ui-jqgrid-hdiv div:first').width();
    $('#gview_grid_client .ui-jqgrid-bdiv div:first').css({
        width: headerWidth,
        height: 1
    })
}

找到解决方案here。这在我的情况下工作得很好,因为我的列和过滤器都有过滤器。如果有人选择过滤器,没有结果,则需要保持水平滚动位置,然后取消选择该过滤器以重新填充表格。如果没有添加代码,表格将重新填充并重置数据。滚动条左侧,但标题仍然向右滚动。

希望这有助于其他人!