开发者

jqgrid setting custom formatter to dynamic column collection

开发者 https://www.devze.com 2022-12-27 05:16 出处:网络
I am using jqgrid. We are building a dashboard functionality with jquery. Different application just have to register respective application page and dashboard will render that page.To achieve this we

I am using jqgrid. We are building a dashboard functionality with jquery. Different application just have to register respective application page and dashboard will render that page.To achieve this we are using jqgrid as one of the jquery plugin. Following is my code

 var ph = '#' + placeHolder;  
    var _prevSort;  
    $.ajax({  
        url: dataUrl,  
        dataType: "json",  
        async: true,  
        success: function(json) {  
            pager = $('#' + pager); 
            if (json.showPager === "false") {  
                pager = eval(json.showPager);  
            }  
            dataUrl += "&jqSession=true";  
            $(ph).jqGrid({  
                url: dataUrl,  
                datatype: "json",  
                sortclass: "grid_sort",  
                colNames: JSON.parse(json.colNames),  
                colModel: JSON.parse(json.colModel),                  
                forceFit: true,  
                rowNum: json.rowNum,  
                rowList: JSON.parse(json.rowList),  
                pager: pager,  
                sortname: json.sortName,  
                caption: json.caption,  
                viewrecords: true,  
                viewsortcols: true,  
                sortorder: json.sortOrder,  
                footerrow: summaryFooter,  
                userDataOnFooter: summaryFooter,  
                jsonReader: {  
                    root: "rows",  
                    row: "row",  
                    repeatitems: false,  
                    id: json.sortName  
                },  
                gridComplete: function() {  
                    if (showFooter) {  
                        $(ph).append("<tr class=\"ui-widget-content jqgrow\"><td   style=\"overflow:visible; text-align:left;width:100%;font-weight:bold;\" class=\"ui-jqgrid-  title\" colspan=\"" + json.colNum + "\">" + json.footerRow + "</td></tr>");
                    }  
                    if (json.additionalContent != null) {  
                        $("#" + xContID).html(json.additionalContent);  
                    }  
                    $("ui-icon-asc").append("IMG");  
                    var _rows = $(".jqgrow");  
                    if (json.rows.length > 0) {  
                        for (var i = 1; i < _rows.length; i += 1) {  
                            _rows[i].attributes["class"].value =   _rows[i].attributes["class"].value.replace(" ui-jqgrid-altrow", "");  
                            if (i % 2 == 1) {  
                                _rows[i].attributes["class"].value += " ui-jqgrid-altrow";  
                            }  
                        }  
                        var gMaxHeight = getGridMaxHeight();  
                        var gHeight = ($(ph + " tr").length + 1) * ($($(".jqgrow")  [0]).height());  
                        if (gHeight <= gMaxHeight) {  
                            $(ph).parent().height(gHeight);  
                        }  
                        else {  
                            $(ph).parent().height(gMaxHeight);  
                        }  
                    }  
                    else {  
                        $(ph).prepend("<tr class=\"ui-widget-content jqgrow\"><td   style=\"overflow:visible; text-align:center;width:100%;font-weight:bold;\" class=\"ui-  jqgrid-title\" colspan=\"" + json.colNum + "\">" + gridNoDataMsg + "</td></tr&开发者_运维问答gt;");  
                        $(ph).parent().height(60);  
                    }  
                },  
                onSortCol: function(index, iCol, sortorder) {                      
                    dataUrl = dataUrl.replace("&jqSession=true", "");  
                    $(ph).jqGrid().setGridParam({ url: dataUrl }).trigger("reloadGrid");  
                    var _colName = "#jqgh_" + index;  
//                    $(_prevSort).parent().removeClass("ui-jqgrid-sorted");  
//                    $(_prevSort).parent().addClass("ui-state-default");  
//                    $(_colName).parent().addClass("ui-jqgrid-sorted");  
//                    $(_colName).parent().removeClass("ui-state-default");  
                    _prevSort = _colName;  
                    var _rows = $(".jqgrow");  
                    for (var i = 1; i < _rows.length; i += 1) {  
                        _rows[i].attributes["class"].value =   _rows[i].attributes["class"].value.replace(" ui-jqgrid-altrow", "");  
                        if (i % 2 == 1) {  
                            _rows[i].attributes["class"].value += " ui-jqgrid-altrow";  
                        }  
                    }  
                }  
            }).navGrid('#' + pager, { search: false, sort: false, edit: false, add: false,   del: false, refresh: false }); // end of grid  
            $("#" + loadid).empty();  
            gGridIds[gGridIds.length] = placeHolder;  
            SetGridSizes();  
        },  
        error: function() {  
            $("#" + loadid).html(loadingErr);  
        }  
    });  

As you can see from the code i am getting column collection dynamically(Appication page which i am calling will give me JSON in the response and will have colNames collection in it. Evrything is working fine but, only issue is coming when we are trying to apply custom formatter to column. This issue comes only when we are dynamically assign "colModel" to jqgrid.

Appreciate help

Thanks in advance


I find your approach very interesting. Mostly for the purpose will be used jqGrid.jqGridExport see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:import_methods. The only thing which I find a little strange, that you don't use contentType: 'application/json' as a $.ajax option. In this case the received data will be converted to an object by $.ajax. Next suspected thing is that you don't decode the JSON respond with respect of one JSON.parse call.

If this advise will not help you I'll suggest that you post a test JSON response contains custom formatter with which you have problems. Then I'll be able to reproduce your problem without having connection to your server.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号