Row drag and drop in Vue Grid component
28 Mar 20237 minutes to read
The Grid Row drag and drop allows you to drag grid rows and drop to another Grid or custom component.To enable Row drag and drop in the Grid, set the allowRowDragAndDrop
to true. The target component on which the Grid rows to be dropped can be set by using targetID
.
To use Row Drag and Drop, you need to inject RowDD
in the provide
section.
<template>
<div id="app">
<ejs-grid id='Grid' :dataSource='srcData' :allowPaging="true" :pageSettings="pageOptions" :allowSelection="true" :allowRowDragAndDrop="true"
:selectionSettings="selectionOptions" :rowDropSettings="srcDropOptions" width="49%">
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign='Right'></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='130'></e-column>
<e-column field='Freight' headerText='Freight' width='120' format='C2' textAlign='Right'></e-column>
</e-columns>
</ejs-grid>
<ejs-grid id='DestGrid' :dataSource='destData' :allowPaging="true" :pageSettings="pageOptions" :allowSelection="true"
:allowRowDragAndDrop="true" :selectionSettings="selectionOptions" :rowDropSettings="destDropOptions" width="49%">
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign='Right'></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='130'></e-column>
<e-column field='Freight' headerText='Freight' width='120' format='C2' textAlign='Right'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, RowDD, Selection, Page } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
srcData: data,
pageSettings: { pageSize: 7 },
destData: [],
pageOptions: { pageSize: 7 },
selectionOptions: { type: "Multiple" },
srcDropOptions: { targetID: "DestGrid" },
destDropOptions: { targetID: "Grid" }
};
},
provide: {
grid: [RowDD, Page, Selection]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
#Grid {
float: left;
}
#DestGrid {
float: right;
}
</style>
Drag and drop rows without drag icon
You can hide the drag and drop icon when performing a drag and drop operation within the grid. This can be achieved by setting the targetID of the rowDropSettings as the current Grid’s ID.
By setting the targetID, the Grid will render without a helper icon (for row drag). Now you can customize the drag and drop action. To control the drop action, you can bind the rowDrop event of the Grid. In the rowDrop event, you can prevent the default action(args.cancel as true) and reorder the rows using reorderRows method of the Grid.
<template>
<div id="app">
<ejs-grid ref='grid' id='Grid' :dataSource='data' :selectionSettings="selectionOptions" :rowDropSettings="dropOptions" height='273px' :rowDrop="rowDrop" :allowSelection="true" :allowRowDragAndDrop="true">
<e-columns>
<e-column field='OrderID' headerText='Order ID' isPrimaryKey={true} textAlign='Right' width=100></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='Freight' headerText='Freight' textAlign= 'Right' width=120 format= 'C2'></e-column>
<e-column field='ShipCountry' headerText='Ship Country' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, RowDD, Selection} from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
selectionOptions: { type: "Multiple" },
dropOptions: { targetID: "Grid" },
};
},
methods: {
rowDrop(args) {
args.cancel = true;
var value = [];
for (var r = 0; r < args.rows.length; r++) {
value.push(args.fromIndex + r);
}
this.$refs.grid.reorderRows(value, args.dropIndex);
}
},
provide: {
grid: [RowDD, Selection]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
- Selection feature must be enabled for row drag and drop.
- Multiple rows can be selected by clicking and dragging inside the grid. For multiple row selection, the
type
property must be set toMultiple
.
Limitations of row drag and drop
- Multiple rows can be drag and drop in the row selections basis.
- Single row is able to drag and drop in same grid without enable the row selection.
- Row drag and drop feature is not having built in support with sorting, filtering, hierarchy grid and grouping features of grid.