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

How to keep existing row selected if rowselecting event cancelled for another row

1 Answer 38 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Som
Top achievements
Rank 1
Som asked on 27 Oct 2010, 06:01 PM
When a users clicks on a row in the datagrid  I need to prompt the user to "OK" or "Cancel" based on certain conditions. If the users select OK I select the row - everything great but if the user selects cancel I cancel the event in the rowselecting event but the current selected row does not stay selected. Any ideas?


Thanks for your help
Som


1 Answer, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 01 Nov 2010, 02:48 PM
Hi Som,

If your conditions for canceling the row do not depend on the row that is being clicked, you could wire the OnRowDeselecting client-side event and check the conditions there and cancel the deselecting of the row itself which will preserve it selected. However if you do depend on the row that is being clicked you should add additional logic to select back the previous row. The MasterTableView for the grid has a method called selectItem that takes as an argument the html element of the row that you wish to select. So if you preserve the previously selected row you can select it back prior to canceling the event. However there is one catch, the call to selectItem will raise again the rowSelecting event and again pop up a confirmation which is quite unnecessary in this case so you could maintain additional boolean variable that indicates whether to enter the code for the rowSelecting event in first place. Here is how I realized in practice this approach in order to handle selection of the previous row when user cancels confirmation:

var selectedRow;
            var shouldCancel = false;
            function rowSelecting(sender, args) {
                if (!shouldCancel) {
                    var answer = confirm("Do you want to select row?");
                    if (answer) {
                        selectedRow = sender.get_masterTableView().get_dataItems()[args._itemIndexHierarchical];
                    }
                    else {
                        if (selectedRow != null) {
                            shouldCancel = true;
                            sender.get_masterTableView().selectItem(selectedRow.get_element());
                        }
                        args.set_cancel(true);
                    }
                }
                else {
                    shouldCancel = false;
                }
            }

Let me know how the proposed solution works at your end. Do not hesitate to ask if you have any other questions.

Greetings,
Marin
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Som
Top achievements
Rank 1
Answers by
Marin
Telerik team
Share this question
or