PDA

View Full Version : grid remember selection



pex
18 Apr 2007, 3:05 AM
Hello,

I have a paged grid with checkboxes as one of the columns. When someone select a checkbox and pressed the 'next page' or 'refresh' I want the selections to be saved so when a user goes back one page he can still see his selections.

I was thinking there are 2 ways of going about this. First would be to listen to the 'beforeload' event handler of the dataStore. This would trigger a function that loops over all the rows in the selectionModel and saves a record's checked state.
The second option would be to keep an internal array of selections and update this anytime a checkbox is checked/unchecked.

Performace wise, what would be the better approach or are there more alternatives?

Thanks

tryanDLS
18 Apr 2007, 8:53 AM
Look at RowSelectionModel doc - getSelections()

august
21 Sep 2007, 3:04 AM
Look at RowSelectionModel doc - getSelections()
the getSelections() only has few words list below:
getSelections</I>

public function getSelections() Returns the selected records Parameters:
None.Returns:
Array Array of selected records


This method is defined by RowSelectionModel.

Can you give some more words to explain this?

Animal
21 Sep 2007, 3:49 AM
Does it make much sens to have elements "A", "D" and "H" selected, and then you go to page 14 and elements "Q", "T" and "Z" are selected? The user selected certain elements, but then changed pages. Those elements are gone and what is on the new page... well, the user won't know it or want anything preselected!

august
21 Sep 2007, 4:33 AM
Does it make much sens to have elements "A", "D" and "H" selected, and then you go to page 14 and elements "Q", "T" and "Z" are selected? The user selected certain elements, but then changed pages. Those elements are gone and what is on the new page... well, the user won't know it or want anything preselected!

It does make sence!
a user want to delete some records,in the first page,he chooses some records and goes to next page to choose other records.then he want to delete all the records but before that he would like to check what he has choosen in the previous page.This is a common needs in project.
Note:delete all records he has selected in different pages once make performance highly and if delete in each page,there will be more contacts with server side.

Animal
21 Sep 2007, 5:06 AM
Ah, I get it. You want to remember the unique row ids of all the rows selected.

Yes, you'll have to keep track of using an "selectionchange" listener, and add/remove them from your array as the user selects/deselects.

You'll also have to reselect any if the use pages back to a previous page where some were selectes.

august
21 Sep 2007, 7:32 AM
Yes,you are right,i am now working on it.but like this

ds.on("beforeload",function (grid){
//here to init the checkbox state
})

but i have a simple question,how to get each row's first column which is a checkbox by id???
i have tried Ext.get(id),getDom()...but doesn't work.i use this method to set id:
var selectionModel = grid.getSelectionModel();
var record = selectionModel.getSelected();
var id = record.data['id'];//get each bind data'id

Animal
21 Sep 2007, 8:15 AM
Why would you need to get a column? Aren't you just collecting the selected Record IDs?

august
21 Sep 2007, 4:31 PM
Why would you need to get a column? Aren't you just collecting the selected Record IDs?
yeah,i just want to collect the selected Record IDs,and how to implement that?
thanks in advance

Animal
21 Sep 2007, 10:42 PM
/** A hash keyed by Record id. Each property is true if the record of that id is selected
this.selectedIds = {};


Before page change, you'd collect the selected ids:



var sm = this.grid.getSelectionModel();
this.grid.getDataSource().each(function(rec) {
if (sm.isSelected(rec)) {
this.selectedIds[rec.id] = true;
} else {
delete this.selectedIds[rec.id];
}
}


Or you could set and delete on selectionchange...

That would ensure you'd saved all the selected ids.

Then after page change, you'd reselect those which you have marked as selected:



var sm = this.grid.getSelectionModel();
this.grid.getDataSource().each(function(rec) {
if (this.isSelected[rec.id]) {
sm.selectRecords([rec], true);
}
});

Nivas81
25 Oct 2007, 1:36 AM
HI,

I am also having the same problem. I am trying out with the given code. But I am getting error that "getDataSource" method is not available. Can any body help me out? Did any body got the solution for rememering the selected rows on multiple pages.

Please find my code in the given attachment.


Thank you very much in advance..

yubuaishui
22 Feb 2011, 9:46 AM
Hello, why i can't use the getDataSource() method ? Thank you!!!