---
title: "Row Dragging to an External DropZone"
framework: angular
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/angular-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/angular-data-grid/modules/). |
| `removeRowDropZone` | `Function` |  |  | Removes an external drop zone added by `addRowDropZone`. Module: [`RowDragModule`](https://www.ag-grid.com/angular-data-grid/modules/). |

> **Note**
>
> If you read the [Managed Row Dragging](https://www.ag-grid.com/angular-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/angular-data-grid/row-dragging-managed/#suppress-move-when-dragging)

#### Simple

```ts
import { Component } from "@angular/core";
import { AgGridAngular } from "ag-grid-angular";
import "./style.css";
import {
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  RowClassRules,
  RowDragModule,
  RowDropZoneParams,
  RowStyleModule,
  TextFilterModule,
  enableDevValidations,
} from "ag-grid-community";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

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

@Component({
  selector: "my-app",
  standalone: true,
  imports: [AgGridAngular],
  template: `<div class="example-wrapper">
    <div class="toolbar">
      <label
        ><input type="checkbox" /> Enable suppressMoveWhenRowDragging</label
      >
    </div>
    <div class="drop-containers">
      <div class="grid-wrapper">
        <ag-grid-angular
          style="width: 100%; height: 100%;"
          [columnDefs]="columnDefs"
          [defaultColDef]="defaultColDef"
          [rowClassRules]="rowClassRules"
          [rowData]="rowData"
          [rowDragManaged]="true"
          (gridReady)="onGridReady($event)"
        />
      </div>
      <div class="drop-col">
        <span id="eDropTarget" class="drop-target">==&gt; Drop to here</span>
        <div class="tile-container"></div>
      </div>
    </div>
  </div> `,
})
export class AppComponent {
  columnDefs: ColDef[] = [
    { field: "id", rowDrag: true },
    { field: "color" },
    { field: "value1" },
    { field: "value2" },
  ];
  defaultColDef: ColDef = {
    filter: true,
    flex: 1,
  };
  rowClassRules: RowClassRules = {
    "red-row": 'data.color == "Red"',
    "green-row": 'data.color == "Green"',
    "blue-row": 'data.color == "Blue"',
  };
  rowData: any[] | null = createRowData();

  onGridReady(params: GridReadyEvent) {
    addDropZones(params);
    addCheckboxListener(params);
  }
}

let rowIdSequence = 100;
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);
}
```

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

## 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/angular-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 { Component, ViewChild } from "@angular/core";

import { AgGridAngular } from "ag-grid-angular";
import type {
  ColDef,
  GetRowIdParams,
  GridApi,
  GridReadyEvent,
  RowDropZoneParams,
} from "ag-grid-community";
import {
  ClientSideRowModelApiModule,
  ClientSideRowModelModule,
  ModuleRegistry,
  RowApiModule,
  RowDragModule,
  RowStyleModule,
  TextFilterModule,
  enableDevValidations,
} from "ag-grid-community";

import "./styles.css";

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

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

@Component({
  standalone: true,
  imports: [AgGridAngular],
  selector: "my-app",
  template: `
    <div class="example-wrapper">
      <div class="inner-col">
        <div class="toolbar">
          <button
            class="factory factory-red"
            data-color="Red"
            data-side="left"
            (click)="onFactoryButtonClick($event)"
          >
            <i class="far fa-plus-square"></i>Add Red
          </button>
          <button
            class="factory factory-green"
            data-color="Green"
            data-side="left"
            (click)="onFactoryButtonClick($event)"
          >
            <i class="far fa-plus-square"></i>Add Green
          </button>
          <button
            class="factory factory-blue"
            data-color="Blue"
            data-side="left"
            (click)="onFactoryButtonClick($event)"
          >
            <i class="far fa-plus-square"></i>Add Blue
          </button>
        </div>
        <div style="height: 100%;" class="inner-col" #eLeftGrid>
          <ag-grid-angular
            style="height: 100%;"
            [defaultColDef]="defaultColDef"
            [getRowId]="getRowId"
            [rowClassRules]="rowClassRules"
            [rowDragManaged]="true"
            [suppressMoveWhenRowDragging]="true"
            [rowData]="leftRowData"
            [columnDefs]="columns"
            (gridReady)="onGridReady($event, 'Left')"
          />
        </div>
      </div>

      <div class="inner-col vertical-toolbar">
        <span class="bin" #eBin>
          <i class="far fa-trash-alt fa-3x" #eBinIcon></i>
        </span>
      </div>

      <div class="inner-col">
        <div class="toolbar">
          <button
            class="factory factory-red"
            data-color="Red"
            data-side="right"
            (click)="onFactoryButtonClick($event)"
          >
            <i class="far fa-plus-square"></i>Add Red
          </button>
          <button
            class="factory factory-green"
            data-color="Green"
            data-side="right"
            (click)="onFactoryButtonClick($event)"
          >
            <i class="far fa-plus-square"></i>Add Green
          </button>
          <button
            class="factory factory-blue"
            data-color="Blue"
            data-side="right"
            (click)="onFactoryButtonClick($event)"
          >
            <i class="far fa-plus-square"></i>Add Blue
          </button>
        </div>
        <div style="height: 100%;" class="inner-col" #eRightGrid>
          <ag-grid-angular
            style="height: 100%;"
            [defaultColDef]="defaultColDef"
            [getRowId]="getRowId"
            [rowClassRules]="rowClassRules"
            [rowDragManaged]="true"
            [suppressMoveWhenRowDragging]="true"
            [rowData]="rightRowData"
            [columnDefs]="columns"
            (gridReady)="onGridReady($event, 'Right')"
          />
        </div>
      </div>
    </div>
  `,
})
export class AppComponent {
  leftRowData: any[] = [];
  rightRowData: any[] = [];
  leftApi!: GridApi;
  rightApi!: GridApi;

  rowClassRules = {
    "red-row": 'data.color == "Red"',
    "green-row": 'data.color == "Green"',
    "blue-row": 'data.color == "Blue"',
  };

  defaultColDef: ColDef = {
    flex: 1,
    minWidth: 100,
    filter: true,
  };

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

  @ViewChild("eLeftGrid") eLeftGrid: any;
  @ViewChild("eRightGrid") eRightGrid: any;
  @ViewChild("eBin") eBin: any;
  @ViewChild("eBinIcon") eBinIcon: any;

  constructor() {
    this.leftRowData = createLeftRowData();
  }

  getRowId = (params: GetRowIdParams) => {
    return String(params.data.id);
  };

  onGridReady(params: GridReadyEvent, side: string) {
    const api = params.api;
    if (side === "Left") {
      this.leftApi = api;
    } else {
      this.rightApi = api;
    }

    this.addBinZone(api);
    this.addGridDropZone(side, api);
  }

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

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

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

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

    api.applyTransaction(transaction);
  }

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

    this.addRecordToGrid(side, data);
  }

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

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

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

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

  addBinZone(api: GridApi) {
    const dropZone: RowDropZoneParams = {
      getContainer: () => this.eBinIcon.nativeElement,
      onDragEnter: () => {
        this.eBin.nativeElement.style.color = "blue";
        this.eBinIcon.nativeElement.style.transform = "scale(1.5)";
      },
      onDragLeave: () => {
        this.eBin.nativeElement.style.removeProperty("color");
        this.eBinIcon.nativeElement.style.transform = "scale(1)";
      },
      onDragStop: (params) => {
        this.binDrop(params.node.data);
        this.eBin.nativeElement.style.removeProperty("color");
        this.eBinIcon.nativeElement.style.transform = "scale(1)";
      },
    };

    api.addRowDropZone(dropZone);
  }

  addGridDropZone(side: string, api: GridApi) {
    const dropSide = side === "Left" ? "Right" : "Left";
    const dropZone: RowDropZoneParams = {
      getContainer: () =>
        dropSide === "Left"
          ? this.eLeftGrid.nativeElement
          : this.eRightGrid.nativeElement,
      onDragStop: (dragParams) =>
        this.addRecordToGrid(dropSide.toLowerCase(), dragParams.node.data),
    };

    api.addRowDropZone(dropZone);
  }
}

let rowIdSequence = 100;

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

  return obj;
}

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

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