免费 jqGrid - Showing/Hiding 列如果为空

Free jqGrid - Showing/Hiding columns if empty

我想显示或隐藏列,具体取决于列是否包含免费 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'}
                             ], 

然后我在自定义格式化程序 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>";
}

然后,执行 hide/show 位,尽管有定义的列...

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

我如何更改它以检查所有列(即而不是命名每个列...)并且 show/hide 如果为空?

我会通过 css 试试这个。 您可以使用 :empty 选择器或仅通过 javascript 添加一个 class 就像 "hidden" 并在 .css 文件中设置 ".hidden{display:none ;}".

这应该可以解决问题

最好使用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");
    }
}