Clipboard

21 Dec 202213 minutes to read

The clipboard provides an option to copy selected rows or cells data into the clipboard.

The following list of keyboard shortcuts is supported in the Tree Grid to copy selected rows or cells data into the clipboard.

Interaction keys Description
Ctrl + C Copy selected rows or cells data into clipboard.
Ctrl + Shift + H Copy selected rows or cells data with header into clipboard.
<ejs-treegrid id="TreeGrid" dataSource="ViewBag.dataSource" height="350" childMapping="Children" treeColumnIndex="1">
    <e-treegrid-selectionsettings type="Multiple" mode="Row"></e-treegrid-selectionsettings>
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="70"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="200"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText=" Start Date" textAlign="Right" format="yMd" type="date" width="80"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80"></e-treegrid-column>
        <e-treegrid-column field="Progress" headerText="Progress" textAlign="Right" width="80"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
public IActionResult Index()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;
    return View();
}

Copy to clipboard by external buttons

To copy selected rows or cells data into the clipboard with help of external buttons, you need to invoke the copy method.

<ejs-button id="copy" content="Copy"></ejs-button>

<ejs-button id="copyHeader" content="Copy Header"></ejs-button>

<ejs-treegrid id="TreeGrid" dataSource="ViewBag.dataSource" height="350" childMapping="Children" treeColumnIndex="1">
    <e-treegrid-selectionsettings type="Multiple" mode="Row"></e-treegrid-selectionsettings>
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="70"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="200"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText=" Start Date" textAlign="Right" format="yMd" type="date" width="80"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80"></e-treegrid-column>
        <e-treegrid-column field="Progress" headerText="Progress" textAlign="Right" width="80"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>

<script>

    document.getElementById("copy").addEventListener("click", function () {
        var treegrid = document.getElementById("TreeGrid").ej2_instances[0];
        treegrid.copy();
    })
    document.getElementById("copyHeader").addEventListener("click", function () {
        var treegrid = document.getElementById("TreeGrid").ej2_instances[0];
        treegrid.copy(true);
    })

</script>
public IActionResult Index()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;
    return View();
}

Copy Hierarchy Modes

Tree Grid provides support for a set of copy modes with copyHierarchyMode property.
The below are the type of filter mode available in TreeGrid.

  • Parent : This is the default copy hierarchy mode in Tree Grid. Clipboard value have the selected records with its parent records, if the selected records not have any parent record then the selected record will be in clipboard.

  • Child : Clipboard value have the selected records with its child record, if the selected records do not have any child record then the selected records will be in clipboard.

  • Both : Clipboard value have the selected records with its both parent and child record. If the selected records do not have any parent and child record then the selected records alone in clipboard.

  • None : Only the Selected records will be in clipboard.

@{
    List<object> toolbar = new List<object>();
    toolbar.Add(new { text = "Copy", tooltipText = "Copy", prefixIcon = "e-copy", id = "copy" });
    toolbar.Add(new { text = "Copy With Header", tooltipText = "Copy With Header", prefixIcon = "e-copy", id = "copyHeader" });
}

<ejs-dropdownlist id="mode" dataSource="@ViewBag.dropdata" placeholder="Select a Mode" change="onModeChange" index="0" popupHeight="220px">
    <e-dropdownlist-fields text="mode" value="id"></e-dropdownlist-fields>
</ejs-dropdownlist>

<ejs-treegrid id="TreeGrid" dataSource="ViewBag.dataSource" allowPaging="true" childMapping="Children" treeColumnIndex="1" toolbarClick="toolbarClick" toolbar="toolbar">
    <e-treegrid-pagesettings pageSize="10" ></e-treegrid-pagesettings>
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="70"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="200"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText=" Start Date" textAlign="Right" format="yMd" type="date" width="80"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80"></e-treegrid-column>
        <e-treegrid-column field="Progress" headerText="Progress" textAlign="Right" width="80"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>

<script>
        function toolbarClick(args) {
            if (this.getSelectedRecords().length > 0) {
                var withHeader = false;
                if (args.item.id === 'copyHeader') {
                    withHeader = true;
                }
                this.copy(withHeader);
            } else {
                var dialogObj = document.getElementById('alert_dialog').ej2_instances[0];
                dialogObj.show();
            }
        }       
        function onModeChange(e) {
            var treegrid = document.getElementById("TreeGrid").ej2_instances[0];
            var mode = e.value;
            treegrid.copyHierarchyMode = mode;
        }
</script>
public IActionResult Index()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;           
    List<Object> dropData = new List<object>() {
        new { id = "Parent", mode = "Parent" },
        new { id = "Child", mode = "Child" },
        new { id = "Both", mode = "Both" },
        new { id = "None", mode = "None" }
    };
    ViewBag.dropdata = dropData;
    return View();
}

AutoFill

AutoFill Feature allows you to copy the data of selected cells and paste it to another cells by just dragging the autofill icon of the selected cells up to required cells. This feature is enabled by defining enableAutoFill property as true.

<ejs-treegrid id="TreeGrid" dataSource="ViewBag.dataSource" height="350" enableAutoFill="true" childMapping="Children" treeColumnIndex="1" toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })">
    <e-treegrid-selectionsettings type="Multiple" mode="Cell" cellSelectionMode="Box"></e-treegrid-selectionsettings>
    <e-treegrid-editsettings allowAdding="true" allowEditing="true" allowDeleting="true" mode="Batch"></e-treegrid-editsettings>
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" isPrimaryKey="true" textAlign="Right" width="70"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="200"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText=" Start Date" textAlign="Right" format="yMd" type="date" width="80"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80"></e-treegrid-column>
        <e-treegrid-column field="Progress" headerText="Progress" textAlign="Right" width="80"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
public IActionResult Index()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;
    return View();
}

NOTE

  • If enableAutoFill is set to true, then the autofill icon will be displayed on cell selection to copy cells.

    * It requires the selection mode to be Cell, cellSelectionMode to be Box and also Batch Editing should be enabled.

Limitations of AutoFill

  • Since the string values are not parsed to number and date type, so when the selected string type cells are dragged to number type cells then it will display as NaN. For date type cells, when the selected string type cells are dragged to date type cells then it will display as an empty cell.
  • Linear series and the sequential data generations are not supported in this autofill feature.

Paste

You can able to copy the content of a cell or a group of cells by selecting the cells and pressing Ctrl + C shortcut key and paste it to another set of cells by selecting the cells and pressing Ctrl + V shortcut key.

<ejs-treegrid id="TreeGrid" dataSource="ViewBag.dataSource" height="350" childMapping="Children" treeColumnIndex="1" toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })">
    <e-treegrid-selectionsettings type="Multiple" mode="Cell" cellSelectionMode="Box"></e-treegrid-selectionsettings>
    <e-treegrid-editsettings allowAdding="true" allowEditing="true" allowDeleting="true" mode="Batch"></e-treegrid-editsettings>
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" isPrimaryKey="true" textAlign="Right" width="70"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="200"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText=" Start Date" textAlign="Right" format="yMd" type="date" width="80"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80"></e-treegrid-column>
        <e-treegrid-column field="Progress" headerText="Progress" textAlign="Right" width="80"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
public IActionResult Index()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;
    return View();
}

NOTE

To perform paste functionality, it requires the selection mode to be Cell, cellSelectionMode to be Box and also Batch Editing should be enabled.

Limitations of Paste Functionality

  • Since the string values are not parsed to number and date type, so when the copied string type cells are pasted to number type cells then it will display as NaN. For date type cells, when the copied string format cells are pasted to date type cells then it will display as an empty cell.

NOTE

You can refer to our ASP.NET Core Tree Grid feature tour page for its groundbreaking feature representations. You can also explore our ASP.NET Core Tree Grid example ASP.NET Core Tree Grid example to knows how to present and manipulate data.