javascript - 当没有数据返回时如何完全隐藏jqgrid?

标签 javascript jquery plugins jqgrid

当记录从我的网络服务返回时,我试图仅显示我的 jqGrid,这很费劲。我也不希望它折叠到只能看到标题栏的位置,但如果这是我能做的最好的事情,我想我可以在标题中添加一条有意义的消息。不过,我宁愿隐藏网格并显示“未找到记录”消息 div block 。

我还猜想,如果最坏的情况发生,我可以解决这个问题 How to display information in jqGrid that there are not any data? (包含链接作为其他人的替代可能解决方案)。

我尝试在从函数加载数据时使用的函数和 GRIDCOMPLETE 事件内部执行 .hide() ,但都没有完成隐藏网格。我对 JQuery 还很陌生,更不用说对使用 jqGrid 还很陌生。

$(document).ready(function() {
    $("#list").jqGrid({
        url: 'Service/JQGridTest.asmx/AssetSearchXml',
        datatype: 'xml',
        mtype: 'GET',
        colNames: ['Inv No', 'Date', 'Amount'],
        colModel: [
            { name: 'invid', index: 'invid', width: 55 },
            { name: 'invdate', index: 'invdate', width: 90 },
            { name: 'amount', index: 'amount', width: 80, align: 'right' }],
        pager: jQuery('#pager'),
        postData: { "testvar": "whatever" },
        rowNum: 10,
        rowList: [10, 20, 30],
        sortname: 'id',
        sortorder: "desc",
        viewrecords: true,
        imgpath: 'themes/sand/images',
        caption: 'My first grid',
        gridComplete: function() {
            var recs = $("#list").getGridParam("records");
            if (recs == 0) {
                $("#list").hide();
            }
            else {
                alert('records > 0');
            }
        }
    });

    ...

    <table id="list" class="scroll"></table> 
    <div id="pager" class="scroll" style="text-align:center;"></div> 

也尝试过这个:

$(document).ready(function() {
    $("#list").jqGrid({
        datatype: function(postdata) {
            jQuery.ajax({
                url: 'Service/JQGridTest.asmx/AssetSearchXml',
                data: postdata,
                dataType: "xml",
                complete: function(xmldata, stat) {
                    if (stat == "success") {
                        var thegrid = $("#list")[0];
                        thegrid.addXmlData(xmldata.responseXML);
                        var recs = $("#list").getGridParam("records");

                        if (recs == 0) {
                            $("#list").hide();
                            alert('No rows - grid hidden');
                        }
                        else {
                            alert(recs);
                        }
                    }
                    else {
                        alert('FAIL');
                    }
                }
            });
        },
        mtype: 'GET',
        colNames: ['Inv No', 'Date', 'Amount'],
        colModel: [
            { name: 'invid', index: 'invid', width: 55 },
            { name: 'invdate', index: 'invdate', width: 90 },
            { name: 'amount', index: 'amount', width: 80, align: 'right' }],
        pager: jQuery('#pager'),
        postData: { "testvar": "whatever" },
        rowNum: 10,
        rowList: [10, 20, 30],
        sortname: 'id',
        sortorder: "desc",
        viewrecords: true,
        imgpath: 'themes/sand/images',
        caption: 'My first grid'
    });

    ...

    <table id="list" class="scroll"></table> 
    <div id="pager" class="scroll" style="text-align:center;"></div> 

感谢您提供的任何帮助。

最佳答案

jqGrid 用它特殊的酱汁和 div 包裹你的表格,所以你应该能够通过用你自己可以隐藏的 div 包裹该表格来完成你想做的事情:

 <div id="gridWrapper">
    <table id="list" class="scroll"></table> 
 </div>

然后在你的 gridComplete 中:

   gridComplete: function() {
        var recs = parseInt($("#list").getGridParam("records"),10);
        if (isNaN(recs) || recs == 0) {
            $("#gridWrapper").hide();
        }
        else {
            $('#gridWrapper').show();
            alert('records > 0');
        }
    }

希望这有帮助。

关于javascript - 当没有数据返回时如何完全隐藏jqgrid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1167477/

相关文章:

javascript - 如何设置一个新类不适用于 Bootstrap 和 jQuery?

css - 投资组合中的灯箱箭头问题(wordpress)

c# - Target 始终是 Entity 还是可以是 EntityReference?

plugins - Gradle 的 ivy-publish 插件中的 "SoftwareComponent"是什么?

javascript - promise 从我的服务中返回 undefined

javascript - 如何从左向右移动/滑动图像

javascript - 如何在 jQuery Mobile 中制作标签栏?

javascript - 对象文字连接字符串属性

javascript - 类中的异步/等待 : unexpected token `this`

jquery - 使用 CSS 和 jQuery 检测所选行的前一行