Skip to main content Skip to footer

Enable Row Dragging with No Row Header in Vue

Background:

By default, the Wijmo FlexGrid only allows rows to be dragged using the row header, which disappears when headersVisibility is set to "Column", disabling row dragging. With a bit of custom code in Vue, we can restore row dragging by making every data cell draggable and handling drag/drop logic manually.

Steps to Complete:

  1. Mark cells in every row as draggable using the formatItem event.
  2. Attach dragstart, dragover, and drop handlers to the grid to track the dragged row and drop location.
  3. Reorder the underlying data array based on where the row is dropped.
  4. Refresh the grid’s collection view so the UI updates.

Getting Started:

Mark cells in every row as draggable using the formatItem event

This runs for every rendered cell, restricts logic to data cells only, and enables native HTML drag events on each row’s cells.

flex.formatItem.addHandler((s, e) => {
  if (e.panel === s.cells) {
    e.cell.setAttribute('draggable', 'true');
  }
});

 

Attach dragstart, dragover, and drop handlers to the grid to track the dragged row and drop location

This captures which row is being dragged, enables dropping, and determines where the row is released.

const host = flex.hostElement;
let draggedRowIndex = -1;

host.addEventListener('dragstart', e => {
  const ht = flex.hitTest(e);
  draggedRowIndex = ht.row;
});

host.addEventListener('dragover', e => {
  e.preventDefault(); // required for drop to fire
});

host.addEventListener('drop', e => {
  const ht = flex.hitTest(e);
  const dropRowIndex = ht.row;
  reorderRows(draggedRowIndex, dropRowIndex);
  e.preventDefault();
});

 

Reorder the underlying data array based on where the row is dropped

This removes the dragged item from its original position, inserts it at the drop index, and keeps the same object reference.

function reorderRows(from, to) {
  if (from === to || from < 0 || to < 0) return;

  const item = items.splice(from, 1)[0];
  items.splice(to, 0, item);

  flex.collectionView.refresh();
}

 

Refresh the grid’s collection view so the UI updates

This forces the FlexGrid to re-read itemsSource and triggers a visual re-render.

flex.collectionView.refresh();

 

Code for context

<template>
  <wj-flex-grid
    ref="flex"
    :itemsSource="items"
    headersVisibility="Column"
    @initialized="onGridInitialized"
  />
</template>

<script>
import * as wjcGrid from '@mescius/wijmo.grid';
import { WjFlexGrid } from '@mescius/wijmo.vue2.grid';

export default {
  components: { WjFlexGrid },
  data() {
    return {
      items: getData(),
      flex: null
    };
  },
  methods: {
    onGridInitialized(grid) {
      this.flex = grid;

      // formatItem + drag logic goes here
    }
  }
};
</script>

 

With this Vue setup, every cell in a FlexGrid row can be dragged to reorder rows even when no row header is shown. The drag/drop behavior is implemented using standard DOM events and Wijmo’s hitTest method to map mouse positions to grid rows.

Happy coding!

Andrew Peterson

Technical Engagement Engineer