免费jqGrid - 显示/隐藏列,如果为空

时间:2017-06-20 06:47:58

标签: jquery jqgrid

我想显示或隐藏列,具体取决于列是否包含Free jqGrid版本4.14.1中的数据。数据馈送来自JSON格式的服务器数据库。

首先,我有一个显示所有可能列的colModel。

colModel: [
            {name:'consultation', label:'Consultation 3'},
            {name:'diagnosis', label:'Diagnosis', formatter:fancyBoxFormatter},
            {name:'prescription', label:'Prescription', formatter:fancyBoxFormatter}, 
            {name:'tests', label:'Tests', formatter:fancyBoxFormatter},
            {name:'imaging', label:'Imaging', formatter:fancyBoxFormatter},
            {name:'generic', label:'Generic', formatter:fancyBoxFormatter},
            {name:'referral', label:'Referral', formatter:fancyBoxFormatter},
            {name:'management', label:'Management', formatter:fancyBoxFormatter},
            {name:'completed', label:'Completed'}
                             ], 

然后我将自定义格式化程序中的null值设置为空,fancyboxFormatter:

function fancyBoxFormatter(cellvalue, options) {
      if (cellvalue == '')
        return '';
    if (cellvalue == null || cellvalue == 'null')
        return '';
    return "<a class=\"fancybox\" href=\"#data" +
        options.rowId + "_" + options.colModel.name + "\">" + cellvalue + "</a>" +
        "<div style=\"display:none\"><div id=\"data" +
        options.rowId + "_" + options.colModel.name + "\">" +
        cellvalue + "</div></div>";
}

然后,执行隐藏/显示位,尽管有一个已定义的列...

beforeProcessing: function (){
                             if($subgrid_3.jqGrid('getCol', 'prescription') == ''){
                             $subgrid_3.hideCol('prescription');
                             } else if($subgrid_3.jqGrid('getCol', 'prescription') !== ''){
                             $subgrid_3.showCol('prescription');
                             }
                         },

如何更改此选项以检查所有列(即,而不是命名每一列...)并显示/隐藏为空?

2 个答案:

答案 0 :(得分:2)

最好使用beforeProcessing来分析从服务器返回的数据。 beforeProcessing的代码很大程度上取决于从服务器返回的数据格式。以下是beforeProcessing的示例:

beforeProcessing: function (data) {
    var i, foundPrescription = false;
    if (data.rows != null) {
        for (i = 0; i < data.rows.length) {
            if (data.rows[i].prescription) { // if not empty string
                foundPrescription = true;
                break;
            }
        }
        $(this).jqGrid(foundPrescription ? "showCol" : "hideCol", "prescription");
    }
}

答案 1 :(得分:0)

我会通过CSS尝试这个。 您可以使用:empty选择器或只是通过javascript添加类似“hidden”的类,并在.css文件中设置“.hidden {display:none;}”。

这应该可以解决问题

相关问题