View Full Version : Comparing two records in a grid to check if a particular column has same value or not

20 Nov 2014, 1:19 PM
Hello all,

I have a grid with some 5 columns. There is a right click option available here to open the record. That part works fine as expected. Now I have a scenario where user can select multiple records 2,3 or 4 and do a right click and select the "open" option. Then the record will open only if some particular columns for all those selected records are the same else I should display a message that the records cannot be opened as they are of different type.

For example, say I have 2 columns, User and status. All the records that the user selects and tries to open should have the same user and status values. if out of the 4 records that a user selects, 2 of them have "Completed" status, 1 has "Cancelled" status and the other has "Open" status, I should display a error msg.

I am using the following code to get each records data.
var clickContext = this.searchResultsGrid.getClickContext();
var grid = clickContext.grid;
var record = clickContext.record;
var status = record.getData().status;

I am using this.searchResultsGrid.getSelectionModel().getSelection().length to denote the number of records the user selected.

I am not sure how to traverse through the multiple records the user selects and compare each record's data with all the other selected records data. Can someone give me an idea on how to traverse through all the selected records and see if a particular column has the same value for all the selected records. I don't want to do this on the server side.


20 Nov 2014, 7:02 PM
Hi varmaj80--

This should be pretty easy to do. When you use getSelection (http://docs.sencha.com/extjs/4.2.3/#!/api/Ext.selection.Model-method-getSelection)(), you get back an array of currently selected records (model instances). These should all have the data you need to compare.

So armed with these selections, you should simply be able to iterate the array, and check to see that all selected records share the attributes that you want.

Give that a shot, and please check back if you run into any issues. Also, if you need more detailed help, I'd suggest creating a test case at https://fiddle.sencha.com which will make it immensely easier for others to interact with your code, offer additional suggestions, etc.

Hope that helps!