---
title: "Row Dragging to an External DropZone"
framework: javascript
version: "36.1.0"
---

# Row Dragging to an External DropZone

Row Dragging to an External DropZone is concerned with moving rows from the grid to different components within the same application. When using row drag with an external DropZone, the data is moved or copied around using the grid events; this is in contrast to standard [Drag & Drop](https://www.ag-grid.com/javascript-data-grid/drag-and-drop/) which uses browser events.

The Row Drag to an External DropZone uses the grid's internal Managed Row Dragging system combined with row selection to create a seamless data drag and drop experience.

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `addRowDropZone` | `Function` |  |  | Adds a drop zone outside of the grid where rows can be dropped. Module: [`RowDragModule`](https://www.ag-grid.com/javascript-data-grid/modules/). |
| `removeRowDropZone` | `Function` |  |  | Removes an external drop zone added by `addRowDropZone`. Module: [`RowDragModule`](https://www.ag-grid.com/javascript-data-grid/modules/). |

> **Note**
>
> If you read the [Managed Row Dragging](https://www.ag-grid.com/javascript-data-grid/row-dragging-managed/) section of the Row Dragging documentation you probably noticed that when you `sort`, `filter` and `rowGroup` the Grid, the managed Row Dragging stops working. The only exception to this rule is when you register external drop zones using `addRowDropZone`. In this case, you will be able to drag from one container to another, but will not be able to drag the rows within the grid.

## Adding and Removing Row Drop Targets

To allow dragging from the grid onto an outside element, or a different grid, call the `addRowDropZone` from the grid API. This will result in making the passed element or `Grid` a valid target when moving rows around. If you later wish to remove that drop zone use the `removeRowDropZone` method from the grid API.

```js
// define drop zone
const targetContainer = document.querySelector('.target-container');
const dropZoneParams = {
    getContainer: () => targetContainer,
    onDragStop: params => {
        // here we create an element for the target container
        const element = createElement(params.node.data);
        targetContainer.appendChild(element);
    }
};

// register drop zone with grid
gridApi.addRowDropZone(dropZoneParams);

// deregister the drop zone when no longer required
gridApi.removeRowDropZone(dropZoneParams);
```

In the example below, note the following:

- You can move rows inside the grid.
- You can move rows to the container on the right hand side.
- Toggle the checkbox to enable or disable [suppressMoveWhenRowDragging](https://www.ag-grid.com/javascript-data-grid/row-dragging-managed/#suppress-move-when-dragging)

#### Simple

```ts
import {
  ClientSideRowModelModule,
  ColDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  RowDragModule,
  RowDropZoneParams,
  RowStyleModule,
  TextFilterModule,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  TextFilterModule,
  RowDragModule,
  RowStyleModule,
  ClientSideRowModelModule,
]);

let rowIdSequence = 100;

const columnDefs: ColDef[] = [
  { field: "id", rowDrag: true },
  { field: "color" },
  { field: "value1" },
  { field: "value2" },
];

let gridApi: GridApi;

const gridOptions: GridOptions = {
  defaultColDef: {
    filter: true,
    flex: 1,
  },
  rowClassRules: {
    "red-row": 'data.color == "Red"',
    "green-row": 'data.color == "Green"',
    "blue-row": 'data.color == "Blue"',
  },
  rowData: createRowData(),
  rowDragManaged: true,
  columnDefs: columnDefs,
  onGridReady: (params) => {
    addDropZones(params);
    addCheckboxListener(params);
  },
};

function addCheckboxListener(params: GridReadyEvent) {
  const checkbox = document.querySelector("input[type=checkbox]")! as any;

  checkbox.addEventListener("change", () => {
    params.api.setGridOption("suppressMoveWhenRowDragging", checkbox.checked);
  });
}

function createRowData() {
  const data: any[] = [];
  [
    "Red",
    "Green",
    "Blue",
    "Red",
    "Green",
    "Blue",
    "Red",
    "Green",
    "Blue",
  ].forEach((color) => {
    const newDataItem = {
      id: rowIdSequence++,
      color: color,
      value1: Math.floor(window.agRandom() * 100),
      value2: Math.floor(window.agRandom() * 100),
    };
    data.push(newDataItem);
  });
  return data;
}

function createTile(data: any) {
  const el = document.createElement("div");

  el.classList.add("tile");
  el.classList.add(data.color.toLowerCase());
  el.innerHTML =
    '<div class="id">' +
    data.id +
    "</div>" +
    '<div class="value">' +
    data.value1 +
    "</div>" +
    '<div class="value">' +
    data.value2 +
    "</div>";

  return el;
}

function addDropZones(params: GridReadyEvent) {
  const tileContainer = document.querySelector(".tile-container") as any;
  const dropZone: RowDropZoneParams = {
    getContainer: () => {
      return tileContainer as any;
    },
    onDragStop: (params) => {
      const tile = createTile(params.node.data);
      tileContainer.appendChild(tile);
    },
  };

  params.api.addRowDropZone(dropZone);
}

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;

gridApi = createGrid(gridDiv, gridOptions);
```

[Live example: Simple](https://www.ag-grid.com/examples/row-dragging-to-external-dropzone/simple/typescript/)

## Dragging Between Grids

It is possible to use a generic `DropZone` to Drag and Drop rows from one grid to another. However, this approach will treat the target grid as a generic `HTMLElement` and adding the rows should be handled by the `onDragStop` callback. If you wish the grid to manage the Drag and Drop across grids and also handle where the record should be dropped, take a look at [Row Dragging - Grid to Grid](https://www.ag-grid.com/javascript-data-grid/row-dragging-to-grid/)

In the example below, note the following:

- Rows can be dragged from one grid to the other grid. When the row is received, it is **not** removed from the first grid. This is the choice of the example. The example could equally have removed from the other grid.
- If the row is already present in the grid, it will not be added twice, and a message is logged in the developer console. This happens because the grid will not allow duplicated IDs.
- Rows can be removed from both grids by dragging the row to the 'Trash' drop zone.
- New rows can be created by clicking on the red, green and blue buttons.

#### Two Grids

```ts
import {
  ClientSideRowModelApiModule,
  ClientSideRowModelModule,
  ColDef,
  GetRowIdParams,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  RowApiModule,
  RowDragModule,
  RowDropZoneParams,
  RowStyleModule,
  TextFilterModule,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ClientSideRowModelApiModule,
  RowApiModule,
  TextFilterModule,
  RowDragModule,
  RowStyleModule,
  ClientSideRowModelModule,
]);

let rowIdSequence = 100;

const leftColumnDefs: ColDef[] = [
  { field: "id", rowDrag: true },
  { field: "color" },
  { field: "value1" },
  { field: "value2" },
];

const rightColumnDefs: ColDef[] = [
  { field: "id", rowDrag: true },
  { field: "color" },
  { field: "value1" },
  { field: "value2" },
];
let leftApi: GridApi;
const leftGridOptions: GridOptions = {
  defaultColDef: {
    flex: 1,
    minWidth: 100,
    filter: true,
  },
  rowClassRules: {
    "red-row": 'data.color == "Red"',
    "green-row": 'data.color == "Green"',
    "blue-row": 'data.color == "Blue"',
  },
  getRowId: (params: GetRowIdParams) => {
    return String(params.data.id);
  },
  rowData: createLeftRowData(),
  rowDragManaged: true,
  suppressMoveWhenRowDragging: true,
  columnDefs: leftColumnDefs,
  onGridReady: (params) => {
    addBinZone(params);
    addGridDropZone(params, "Right");
  },
};
let rightApi: GridApi;
const rightGridOptions: GridOptions = {
  defaultColDef: {
    flex: 1,
    minWidth: 100,
    filter: true,
  },
  rowClassRules: {
    "red-row": 'data.color == "Red"',
    "green-row": 'data.color == "Green"',
    "blue-row": 'data.color == "Blue"',
  },
  getRowId: (params: GetRowIdParams) => {
    return String(params.data.id);
  },
  rowData: [],
  rowDragManaged: true,
  suppressMoveWhenRowDragging: true,
  columnDefs: rightColumnDefs,
  onGridReady: (params) => {
    addBinZone(params);
    addGridDropZone(params, "Left");
  },
};

function createLeftRowData() {
  return ["Red", "Green", "Blue"].map(function (color) {
    return createDataItem(color);
  });
}

function createDataItem(color: string) {
  return {
    id: rowIdSequence++,
    color: color,
    value1: Math.floor(window.agRandom() * 100),
    value2: Math.floor(window.agRandom() * 100),
  };
}

function addRecordToGrid(side: string, data: any) {
  // if data missing or data has no it, do nothing
  if (!data || data.id == null) {
    return;
  }

  let api = side === "left" ? leftApi : rightApi,
    // do nothing if row is already in the grid, otherwise we would have duplicates
    rowAlreadyInGrid = !!api!.getRowNode(data.id),
    transaction;

  if (rowAlreadyInGrid) {
    console.log("not adding row to avoid duplicates in the grid");
    return;
  }

  transaction = {
    add: [data],
  };

  api!.applyTransaction(transaction);
}

function onFactoryButtonClick(e: any) {
  const button = e.currentTarget,
    buttonColor = button.getAttribute("data-color"),
    side = button.getAttribute("data-side"),
    data = createDataItem(buttonColor);

  addRecordToGrid(side, data);
}

function binDrop(data: any) {
  // if data missing or data has no id, do nothing
  if (!data || data.id == null) {
    return;
  }

  const transaction = {
    remove: [data],
  };

  [leftApi, rightApi].forEach((api) => {
    const rowsInGrid = !!api!.getRowNode(data.id);

    if (rowsInGrid) {
      api!.applyTransaction(transaction);
    }
  });
}

function addBinZone(params: GridReadyEvent) {
  const eBin = document.querySelector(".bin") as any,
    icon = eBin.querySelector("i"),
    dropZone: RowDropZoneParams = {
      getContainer: () => {
        return eBin;
      },
      onDragEnter: () => {
        eBin.style.color = "blue";
        icon.style.transform = "scale(1.5)";
      },
      onDragLeave: () => {
        eBin.style.removeProperty("color");
        icon.style.transform = "scale(1)";
      },
      onDragStop: (dragStopParams) => {
        binDrop(dragStopParams.node.data);
        eBin.style.removeProperty("color");
        icon.style.transform = "scale(1)";
      },
    };

  params.api.addRowDropZone(dropZone);
}

function addGridDropZone(params: GridReadyEvent, side: string) {
  const grid = document.querySelector<HTMLElement>("#e" + side + "Grid")!,
    dropZone: RowDropZoneParams = {
      getContainer: () => {
        return grid;
      },
      onDragStop: (params) => {
        addRecordToGrid(side.toLowerCase(), params.node.data);
      },
    };

  params.api.addRowDropZone(dropZone);
}

function loadGrid(side: string) {
  const grid = document.querySelector<HTMLElement>("#e" + side + "Grid")!;
  if (side === "Left") {
    leftApi = createGrid(grid, leftGridOptions);
  } else {
    rightApi = createGrid(grid, rightGridOptions);
  }
}

const buttons = document.querySelectorAll("button.factory");

for (let i = 0; i < buttons.length; i++) {
  buttons[i].addEventListener("click", onFactoryButtonClick);
}

loadGrid("Left");
loadGrid("Right");
```

[Live example: Two Grids](https://www.ag-grid.com/examples/row-dragging-to-external-dropzone/two-grids/typescript/)
