How to hide a jqgrid column dynamically

为君一笑 提交于 2019-12-09 03:29:12

问题


I am implementing jqgrid in my asp.net MVC web application.

In my grid i have two columns edit and delete. The delete should be visible only if the user is logged as admin .

How can we dynamically hide.show columns in jqgrid. I am having a session variable to check whether the logged in user is Admin or not.

I am accessing that variable in javascript. but, not sure how can i hide/show column in jqgrid

Please help..


回答1:


Use this code,

jQuery("#list").jqGrid('hideCol',["colModel1_name","colModel2_name"]);
jQuery("#list").jqGrid('showCol',["colModel1_name","colModel2_name"]);

May this help you.




回答2:


This one worked:

$("#list").hideCol("ColumnName")



回答3:


Newer API

jQuery("#list").jqGrid('hideCol',["ColumnName","ColumnName2"]);

Older API

$("#list").hideCol("ColumnName")



回答4:


This is not the best practice to use js to manage your security. You should not show this column on your server side!




回答5:


I had to dive into some legacy stuff today. One of the requirements was to conditionally set the visibility of some columns. There was a drop down on the page that set a category parameter in the where clause for the grid. Long story short, watching the change event of the drop down wasn't possible making most of the methods in the answers here invalid.

I was able to use a ternary in the hidden param to set the visibility.

{
    name: 'mfg',
    index: 'mfg',
    width: 150,
    sortable: true,
    hidden: $('#evCategory').val() == 'Calibration' ? false : true
},

And it simply evals to if the dropdown has a value of calibration hidden should be false, if calibration is not the value hidden should be true.

Might also be able to shorten it to;

!$('#evCategory').val() == 'Calibration' || true

Although I haven't tested that.



来源:https://stackoverflow.com/questions/19517092/how-to-hide-a-jqgrid-column-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!