JqG​​rid不是第一次显示数据,而是在此之后工作

时间:2015-08-11 14:37:01

标签: c# model-view-controller jqgrid

当我从隐藏字段传递参数srchkey和srchvalue时,附加的代码段会运行。 但是在做同样的事情时,JqGrid并没有第一次显示数据,尽管jqGrid获取数据,我们可以在Developer Tool中看到它。 第二次开始时,它正在加载数据并完美显示。 请帮助我们,因为我们最近几天陷入了这个问题

提前致谢。

 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>

1 个答案:

答案 0 :(得分:0)

我的JqGrid需要在document.ready函数上进行初始化,为此我创建了一个没有数据的虚拟网格。然后代码工作正常。 虽然它不是一个正确的和推荐的解决方案,但这工作:)