Quantcast
Channel: Telerik Forums RSS
Viewing all articles
Browse latest Browse all 84751

Moving multiple records up/down in Grid while maintaining selections

$
0
0

After an afternoon of playing around on fiddle, I finally came up with a solution that works.  Here's a snippet of the code that moves each selected row up one row:

01./* move up */
02.$("#moveItemUp").on("click", function(idx, elem) {
03.     
04.    varfirstItemSelected = false;
05. 
06.    selected = visibleGrid.select();
07.    selectedIndexes = [];
08. 
09.    if(selected.length > 0) {
10. 
11.        /* capture the indexes of those that are selected */
12.        $.each(selected, function(idx, elem) {
13. 
14.            vardataItem = visibleGrid.dataItem($(this));
15.            varindex = visibleGrid.dataSource.indexOf(dataItem);
16. 
17.            if(index == 0) {
18.                firstItemSelected = true;
19.            }
20. 
21.            selectedIndexes.push(index);
22.        });
23. 
24.        /* if they've selected the first item, don't let them do anything */
25.        if(firstItemSelected == true) {
26.            return;
27.        }
28. 
29.        /* move each item to the new location - one row higher than the old row */
30.        for(i = 0; i < selectedIndexes.length; i++) {
31.            varoldIndex = selectedIndexes[i];
32.            varnewIndex = oldIndex - 1;
33. 
34.            moveDataItem(oldIndex, newIndex);
35.        }
36.    }
37.});
38. 
39./* used by each of the up/down buttons to move the selected items */
40.functionmoveDataItem(oldIndex, newIndex){
41.    vardataItem = visibleGrid.dataSource.at(oldIndex);
42. 
43.    visibleGrid.dataSource.remove(dataItem);
44.    visibleGrid.dataSource.insert(newIndex, dataItem);
45.}

The project on fiddle has been updated in case you want to take a look, but that being said, if you have other suggestions on how to better accomplish this, I'm all ears!


Viewing all articles
Browse latest Browse all 84751

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>