JqGrid第一次不显示数据,但之后工作

本文关键字:之后 工作 数据 第一次 显示 JqGrid | 更新日期: 2023-09-27 18:07:30

当我从隐藏字段传递参数srchkey和srchvalue时,附加的代码段运行。但是在做同样的事情时,JqGrid并没有第一次显示数据,尽管JqGrid获得了数据,正如我们在开发人员工具中看到的那样。当第二次开始时,它正在加载数据并完美地显示它。请帮助我们,因为我们被这个问题困了几天

提前感谢。

 function BindGrid() {
            $("#divgraph").hide();
            debugger;
            $('#grid').jqGrid('GridUnload');
            var arr = $("#hdnSearch").val().split('|');
            var srchkey = arr[0];
            var srchval = arr[1];
            
            // dynamic columns in grid
            $.ajax({
                url: '/Admin/GetGenericSearch',
                type: 'POST',
                data: { key: srchkey, value: srchval },
                datatype: 'json',
                success: function (result) {
                    $("#ExportExcel").show();
                    var colM = JSON.parse(result.colModel);
                    var colN = JSON.parse(result.colNames);
                    if (srchkey.indexOf("PId") > 0 || srchkey.indexOf("PName") > 0) {
                        for (var i = 0; i < colM.length; i++) {
                            if (colM[i].name.toString().indexOf("PROJECTID") > -1) {
                                colM[i].key = true;
                            }
                        }
                    }
                    if (srchkey.indexOf("AId") > 0 || srchkey.indexOf("AName") > 0) {
                        var myMonthsTemplate = {
                            sorttype: 'float', search: true, searchtype: 'float', searchoptions: { sopt: ['eq', 'ne', 'le', 'lt', 'gt', 'ge'] }, formatter: function (cellvalue, options) {
                                var value = parseFloat(cellvalue), retult, op = $.extend({}, $.jgrid.formatter.float);
                                if (!$.fmatter.isUndefined(options.colModel.formatoptions)) {
                                    op = $.extend({}, op, options.colModel.formatoptions);
                                } retult = $.fmatter.util.NumberFormat(Math.abs(value), op); return (value >= 0 ? retult : '(' + retult + ')');
                             }, cellattr: function (rowid, cellvalue) { return parseFloat(cellvalue) < 7 ? 'style="color:red ;"' : parseFloat(cellvalue) < 8 ? 'style="color:orange ;"' : 'style="color:green ;"' }
                        };
                        for (var i = 0; i < colM.length; i++) {
                            if ((typeof colM[i].template != "undefined")) {
                                colM[i].template = myMonthsTemplate;
                            }
                        }
                    }
                    var colData = result.data;
                    $("#grid").jqGrid({
                           datatype: 'json',
                         
                        mtype: 'GET',
                        url: '/Admin/GetGenericSearchData',
                        colNames: colN,
                        colModel: colM,
                        jsonReader: {
                            
                            root: "rows",
                            page: "page",
                            total: "total",
                            records: "records",
                            repeatitems: false,
                        },
                        postData: { key: srchkey, value: srchval },
                        gridview: true,
                        pager: '#pager',
                        height: "140px",
                        width: "1200px",
                        rowNum: 6,
                        rowList: [6, 12, 18, 24],
                        viewrecords: true,
                         loadonce: true,
                        autowidth: true,
                        shrinkToFit: true,
                        ignoreCase: true,
                        
                         
                        loadComplete: function () {
     
                            $("#mygrid").jqGrid('setGridParam', { postData: { key: srchkey, value: srchval } }).trigger('reloadGrid');;
                          
                            if (srchkey.indexOf("PId") > 0 || srchkey.indexOf("PName") > 0) {
                                var columnNames = $("#grid").jqGrid('getGridParam', 'colNames');
                                var columnModel = $("#grid").jqGrid('getGridParam', 'colModel');
                                var ids = jQuery("#grid").jqGrid('getDataIDs');
                                for (var i = 0; i < ids.length; i++) {
                                    for (var j = 0; j < columnNames.length; j++) {
                                        if (columnNames[j] == "GREEN") {
                                            // assign color to grid row
                                            //$("#grid").jqGrid('setRowData', ids[1], false, 'time_ingreen');
                                            jQuery('#grid').setCell(ids[i], columnModel[j].name, '', 'time_ingreen');
                                        }
                                        else if (columnNames[j] == "AMBER") {
                                            jQuery('#grid').setCell(ids[i], columnModel[j].name, '', 'time_inorange');
                                        }
                                        else if (columnNames[j] == "RED") {
                                            jQuery('#grid').setCell(ids[i], columnModel[j].name, '', 'time_inred');
                                        }
                                    }
                                }
                            }
                           
                        },
                    })
                    .navGrid('#pager', { edit: false, add: false, del: false, search: false });
                    if (srchkey.indexOf("PId") > 0 || srchkey.indexOf("PName") > 0) {
                    jQuery("#grid").jqGrid('setGroupHeaders', {
                        useColSpanStyle: true,
                        groupHeaders: [
                          { startColumnName: 'AMBERJanuary', numberOfColumns: 3, titleText: 'January' },
                          { startColumnName: 'AMBERFebruary', numberOfColumns: 3, titleText: 'February' },
                           { startColumnName: 'AMBERMarch', numberOfColumns: 3, titleText: 'March' },
                          { startColumnName: 'AMBERApril', numberOfColumns: 3, titleText: 'April' },
                           { startColumnName: 'AMBERMay', numberOfColumns: 3, titleText: 'May' },
                           { startColumnName: 'AMBERJune', numberOfColumns: 3, titleText: 'June' }
                    ]
                    });
                }
                },
                error: function (result) {
                    alert("error");
                }
            }); 
        }
  <table id="grid"></table>
                             <div id="pager"></div>

JqGrid第一次不显示数据,但之后工作

我的JqGrid需要在文档上初始化。ready函数,为此我创建了一个没有数据的虚拟网格。然后代码就可以正常工作了。虽然这不是一个合适的和推荐的解决方案,但这是有效的:)