Enable editing in single click

11 Jan 20236 minutes to read

Normal Editing

You can make a row editable on a single click with Normal mode of editing in Grid, by using the startEdit and endEdit methods.

Bind the mouseup event for Grid and in the event handler call the startEdit and endEdit, based on the clicked target element.

<ejs-grid id="Grid" dataSource="ViewBag.DataSource" allowPaging="true" toolbar="@(new List<string>() {"Add", "Edit", "Delete", "Update", "Cancel"})" load="load">
    <e-grid-editSettings allowAdding="true" allowEditing="true" allowDeleting="true" mode="Normal"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="100" isPrimaryKey="true"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" width="120"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" width="120" format="C2"></e-grid-column>
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>
    
<script>
    function load(args) {
        var instance = document.getElementById('Grid').ej2_instances[0];
        instance.element.addEventListener('mouseup', function (e) {
            if (e.target.classList.contains("e-rowcell")) {
                if (instance.isEdit)
                    instance.endEdit();
                var index = parseInt(e.target.getAttribute("Index"));
                instance.selectRow(index);
                instance.startEdit();
            };
        });
    }        
</script>
public IActionResult Index()
    {
        var Order = OrderDetails.GetAllRecords();
        ViewBag.DataSource = Order;
        return View();
    }

Open dropdown edit popup on single click

You can open the default dropdown edit popup with single click edit by focusing the dropdown element and calling the EJ2 dropdown list’s showPopup method in the Grid’s actionComplete event. In this demo we have used a global flag variable in the mouseup event to ensure if the dropdown column is the clicked edit target.

<ejs-grid id="Grid" actionComplete="onActionComplete" dataSource="ViewBag.DataSource" allowPaging="true" toolbar="@(new List<string>() {"Add", "Edit", "Delete", "Update", "Cancel"})" load="load">
    <e-grid-editSettings allowAdding="true" allowEditing="true" allowDeleting="true" mode="Normal"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="100" isPrimaryKey="true"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" width="120"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" width="120" format="C2"></e-grid-column>
        <e-grid-column field="ShipCountry" headerText="Ship Country" editType="dropdownedit" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>
    
<script>
    var isDropdown = false;
    
    function load(args) {
        var instance = document.getElementById('Grid').ej2_instances[0];
        instance.element.addEventListener('mouseup', function (e) {
            if (e.target.classList.contains("e-rowcell")) {
              if (instance.isEdit)
                  instance.endEdit();
              var rowInfo = instance.getRowInfo(e.target);
              if (rowInfo.column.field === "ShipCountry")
                  isDropdown = true;
              instance.selectRow(rowInfo.rowIndex);
              instance.startEdit();
            }
        });
    }  

    function onActionComplete(args) {
        if (args.requestType =="beginEdit" && isDropdown) {
            isDropdown = false;
            var dropdownObj = args.form.querySelector('.e-dropdownlist').ej2_instances[0];
            dropdownObj.element.focus();
            dropdownObj.showPopup();
        }
    }      
</script>
public IActionResult Index()
    {
        var Order = OrderDetails.GetAllRecords();
        ViewBag.DataSource = Order;
        return View();
    }