How to hide a jqgrid column dynamically
Asked Answered
D

5

15

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..

Detonate answered 22/10, 2013 at 11:52 Comment(0)
D
15

This one worked:

$("#list").hideCol("ColumnName")
Detonate answered 22/10, 2013 at 12:59 Comment(0)
A
20

Use this code,

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

May this help you.

Archetype answered 22/10, 2013 at 11:56 Comment(4)
@Vinoth..what are colmodel1 and colmodel2?Detonate
Both are name in your colmodel definition which are the columns you want to hide.Archetype
What if I need to hide the colModel in the add form dialog? I tried this in beforeShowForm but it did not work,Ignacioignacius
Check this answerArchetype
D
15

This one worked:

$("#list").hideCol("ColumnName")
Detonate answered 22/10, 2013 at 12:59 Comment(0)
W
11

Newer API

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

Older API

$("#list").hideCol("ColumnName")
Woad answered 4/6, 2014 at 23:26 Comment(0)
T
2

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

Thumbstall answered 27/11, 2013 at 10:50 Comment(2)
I am calling that javascipt from server side based on session information. So , i guess i am dong it from server side itself. I think jqgrid is purely jquery plugin. i think there is no other way to do it rather than using jquery..Detonate
I agree with @Thumbstall - I use the server to programmatically write out the column model. If the user doesn't have permissions, then, the delete column is never written out to their browser. It's not 'hidden' where they might discover it in the view-source.. it's simply not thereSupereminent
P
1

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.

Phillie answered 24/7, 2018 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.