This is a migrated thread and some comments may be shown as answers.

How to Show/hide filter items based on user rights.

3 Answers 119 Views
Filter
This is a migrated thread and some comments may be shown as answers.
gc_0620
Top achievements
Rank 1
gc_0620 asked on 14 Mar 2013, 08:50 PM
Folks, Using RadControls for ASP.NET AJAX Q1 2013 with VS 2010. I would like to conditionally hide certain filter items based on user rights. Example I have a session variable i.e. Session["UserRights"].

If Session["UserRights"] == "Supervisor" then show all available filter items in a GridDateTimeColumn, if not then available filter items in that column will be No Filter & Equal To.

Attached is my screen shot. Any help will be appreciated.

Thanks

gc_0620

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 15 Mar 2013, 07:23 AM
Hi,

To limit the filter options displayed for a given column on the client, you need to intercept the OnFilterMenuShowingclient event of RadGrid/OnClientShown event of the filter menu and hide some of the possibles choices from within the body of the respective handler. Here is the sample code.
aspx:
<ClientSettings>
  <ClientEvents OnFilterMenuShowing="filterMenuShowing" /></ClientSettings>
<FilterMenu OnClientShown="MenuShowing" />

JS:
var column = null;
       function MenuShowing(sender, args) {
           if (column == null) return;
           var menu = sender; var items = menu.get_items();
           if (column.get_dataType() == "System.String") {
               var i = 0;
               while (i < items.get_count()) {
                   if (!(items.getItem(i).get_value() in { 'NoFilter': '', 'Contains': '', 'NotIsEmpty': '', 'IsEmpty': '', 'NotEqualTo': '', 'EqualTo': '' })) {
                       var item = items.getItem(i);
                       if (item != null)
                           item.set_visible(false);
                   }
                   else {
                       var item = items.getItem(i);
                       if (item != null)
                           item.set_visible(true);
                   } i++;
               }
           }
           if (column.get_dataType() == "System.Int64") {
               var j = 0; while (j < items.get_count()) {
                   if (!(items.getItem(j).get_value() in { 'NoFilter': '', 'GreaterThan': '', 'LessThan': '', 'NotEqualTo': '', 'EqualTo': '' })) {
                       var item = items.getItem(j); if (item != null)
                           item.set_visible(false);
                   }
                   else { var item = items.getItem(j); if (item != null) item.set_visible(true); } j++;
               }
           }
           column = null;
           menu.repaint();
       }
       function filterMenuShowing(sender, eventArgs) {
           column = eventArgs.get_column();
       }

Thanks,
Shinu
0
gc_0620
Top achievements
Rank 1
answered on 18 Mar 2013, 07:22 PM
Hi Shinu,

Thanks for your reply but I am sorry to say that we have a miscommunication here. The solution and the link is only reducing the filter items; my intention was to reduce the filter items based on user access rights not globally reduce the items.

The workflow in my project is Login.aspx->Default.aspx. After Login.aspx, I am storing the users access rights (i.e. Supervisor or regular user) into session variable Session["UserRights"].

In Default.aspx page load event, if value of Session["UserRights"] = 'Supervisor', show all available filter items of Telerik GridDateTimeColumn.  If not, available filter items of GridDateTimeColumn will be limit to No Filter & Equal To. That is my desired result.

Thanks

gc_0620





0
Marin
Telerik team
answered on 21 Mar 2013, 12:46 PM
Hello,

 You can also reduce the filter menu options conditionally on the server (for example in the RadGrid Init event or later if needed) as shown at the end of the help topic.
There you can also check for the session variable and decide whether you need to hide the menu items or not. The filter menu is shared control among all columns so if you need to modify the filter options only for a particular column (and leave the default options for the other columns) then this has to be done on the client where you have access to the column for which the menu is currently invoked. If this is the case then you will also need to serialize a variable with the user rights on the client so that they are accessible in the MenuShowing event.

I hope this helps.

Kind regards,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Filter
Asked by
gc_0620
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
gc_0620
Top achievements
Rank 1
Marin
Telerik team
Share this question
or