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

[Solved] Enable\Disable grid column editor based on value in a model

2 Answers 242 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Johan
Top achievements
Rank 1
Johan asked on 14 Mar 2011, 12:55 PM
I have a grid with editors that I would like to disable in edit mode based on the values of fields in the model the grid is bound to.  Let's say, for arguments sake my model contains the following fields:

IsUser1Modifyable (bool)
IsUser2Modifyable (bool)
UserName1 (string)
UserName2 (string)

If IsUser1Modifyable is false I don't want the editor associated with UserName1 to be enabled.  The same with UserName2.

What would be the best way to achieve this?

2 Answers, 1 is accepted

Sort by
0
Johan
Top achievements
Rank 1
answered on 15 Mar 2011, 10:07 AM
Since posting this I have resolved the problem.  Here's my solution but would love to hear if there are better ways to do this, possibly as part of the grid configuration without having to use client scripts.

Step 1:

On the grid subscribe to a client-side event

.Name("Grid")
        .ClientEvents(events => events.OnEdit("Grid_onEdit"))

Step 2:

The Client Side Script

<script type="text/javascript">
    function Grid_onEdit(e) {
        var dataItem = e.dataItem;
        var mode = e.mode;
        var form = e.form;
        var nominee1UserNameTextBox = $(form).find('#Nominee1UserName');
        var nominee2UserNameTextBox = $(form).find('#Nominee2UserName');


        if (!dataItem.IsNominee1Modifyable) {
            nominee1UserNameTextBox.attr('disabled', 'disable');
        }


        if (!dataItem.IsNominee2Modifyable) {
            nominee2UserNameTextBox.attr('disabled', 'disable');
        }
    }
</script>

0
Rajesh
Top achievements
Rank 1
answered on 10 Apr 2012, 12:08 PM
Are you using in-line editing or pop-up editing? I do not think this would work for in-line editing, as the control that you want to disable may not even be rendered when OnEdit is fired.
Tags
Grid
Asked by
Johan
Top achievements
Rank 1
Answers by
Johan
Top achievements
Rank 1
Rajesh
Top achievements
Rank 1
Share this question
or