免费 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'}
], 

然后,如果在自定义格式化程序中为空,我将单元格值设置为空,花式框格式化程序:

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');
}
},

我如何更改它以检查所有列(即而不是命名每个列...)并在空时显示/隐藏?

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

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

这应该可以解决问题

最新更新