josei josei - 2 months ago 8
Javascript Question

Duplicate a row in a specific position

With a grid, I want to duplicate a row selected.

Then I want to insert a copy of the row in the grid position immediately following the row selected.

FIDDLE: https://fiddle.sencha.com/#fiddle/1hc6

var grid = this.up('grid');
var record = grid.getSelectionModel().getSelection()[0].copy(null);

var store = grid.getStore();
var row = store.indexOf(record);

store.insert(row, record); //store.insert(row + 1, record);?????


I EDITED fiddle with solution

Answer

This is down to variable naming. Your variable called record is in fact the copy. Hence when you search record against the store, your row index variable returned index -1 and hence was inserted at the end (probably default behavior of the store API).

The below changes will get what you want:

var record = grid.getSelectionModel().getSelection()[0];
var copy = record.copy(null);
...
var row = store.indexOf(record); // this used to be -1
store.insert(row, copy);