How to duplicate a selected row and add it underneath the selected row

Hello!

I have a grid with batch editing that looks like this:
------------------------------------------------------------
<ejs-grid id="Grid"
                      dataSource="ViewBag.DataSource"
                      allowPaging="true"
                      allowFiltering="true"
                      allowExcelExport="true"
                      allowPdfExport="true"
                      allowReordering="true"
                      allowResizing="false"
                      allowSorting="true"
                      allowTextWrap="true"
                      allowSelection="true"
                      batchAdd="duplicateSelectedRow"
                      gridLines="Both"
                      queryCellInfo="queryCellInfo"
                      showColumnChooser="true"
                      toolbar="@(new List<object> {"Add", "Update", "Cancel", "Delete", "Print", "PdfExport", "ExcelExport", "Search", "ColumnChooser"})">
                <e-grid-filterSettings type="Excel" columns="filterColumns" />
                <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Batch" showDeleteConfirmDialog="true" />
                <e-data-manager json="ViewBag.DataSource" adaptor="RemoteSaveAdaptor" crudUrl="/MyDashboard/StatusUpdateBatchAddUpdate"></e-data-manager>
                <e-grid-columns>
                    <e-grid-column field="ReporterObjectiveStatusUpdateFullDetail.GridId" headerText="GridId" isPrimaryKey="true" visible="false" allowEditing="false" showInColumnChooser="false"></e-grid-column>
                    <e-grid-column field="ReporterObjectiveStatusUpdateFullDetail.ObjectiveId" headerText="ObjectiveId" visible="false" allowEditing="false" showInColumnChooser="false"></e-grid-column>
                    <e-grid-column field="ReporterObjectiveStatusUpdateFullDetail.StatusUpdateId" headerText="StatusUpdateId" visible="false" allowEditing="false" showInColumnChooser="false"></e-grid-column>
                    <e-grid-column field="ReporterObjectiveStatusUpdateFullDetail.StatusUpdateFiscalYearId" headerText="FiscalYearId" visible="false" allowEditing="false" showInColumnChooser="false"></e-grid-column>
                    <e-grid-column field="ReporterObjectiveStatusUpdateFullDetail.StatusUpdateReportingPeriodId" headerText="ReportingPeriodId" visible="false" allowEditing="false" showInColumnChooser="false"></e-grid-column>
                    <e-grid-column field="ReporterObjectiveStatusUpdateFullDetail.StatusUpdateWorkflowStatusId" headerText="StatusUpdateWorkflowStatusId" visible="false" allowEditing="false" showInColumnChooser="false"></e-grid-column>
                    <e-grid-column field="ReporterObjectiveStatusUpdateFullDetail.DivisionDescription" headerText="Division" allowEditing="false" width="5"></e-grid-column>
                    <e-grid-column field="ReporterObjectiveStatusUpdateFullDetail.AspectDescription" headerText="Focus Area" headerTemplate="#focusAreaHeader" allowEditing="false" width="5"></e-grid-column>
                    <e-grid-column field="ReporterObjectiveStatusUpdateFullDetail.ObjectiveDescription" headerText="Objective" headerTemplate="#objectiveHeader" allowEditing="false" width="15"></e-grid-column>
                    <e-grid-column field="ReporterObjectiveStatusUpdateFullDetail.StatusUpdateFiscalYearDescription" headerText="FY" allowEditing="false" width="5"></e-grid-column>
                    <e-grid-column field="ReporterObjectiveStatusUpdateFullDetail.StatusUpdateReportingPeriodDescription" headerText="Reporting Period" editType="dropdownedit" defaultValue="@currentReportingPeriodDescription" edit="new {@params = reportingPeriodDropDownList }" width="5"></e-grid-column>
                    <e-grid-column field="ReporterObjectiveStatusUpdateFullDetail.StatusUpdateStatusDescription" headerText="Status" editType="dropdownedit" edit="new {@params = statusDropDownList }" width="5"></e-grid-column>
                    <e-grid-column field="ReporterObjectiveStatusUpdateFullDetail.StatusUpdateComment" headerText="Comment" width="10"></e-grid-column>
                    <e-grid-column field="Champions" headerText="Champion(s)" visible="@ViewBag.Area == AreaTypes.Environmental" showInColumnChooser="@ViewBag.Area == AreaTypes.Environmental" allowEditing="false" width="5"></e-grid-column>
                    <e-grid-column field="Reporters" headerText="Reporter(s)" allowEditing="false" width="5"></e-grid-column>
                </e-grid-columns>
            </ejs-grid>
---------------------------------------------------------


I would like an "Add" button that does the following:
- if a row is not selected, pop up a message that says no row has been selected for this operation (just like the "Delete" button)
- if a row is selected
-- copy the data in the selected row
-- create a row underneath the selected row
-- fill the new row with some of the copied data

I have been trying to figure it out with batchAdd, but I am not having success so far.

Thanks!


8 Replies 1 reply marked as answer

JG Jessica Goodrich February 5, 2021 01:01 AM UTC

I made a little bit of progress by using beforeBatchAdd instead of batchAdd and using the following js:
function checkForSelectedRow(args) {
            args.cancel = true;
            var gridObj = document.getElementById("Grid").ej2_instances[0];
            var selectedRow = gridObj.getSelectedRecords()[0];
            if (selectedRow === undefined) {
                bootbox.alert({
                    title: 'No record selected to copy from',
                    message: 'You must select a row that you would like to duplicate in order to add a new row. The row you select will be used to determine which Action/Objective you are creating a Status Update for and will be used to populate the Division, Focus Area, Objective/Action, FY, and Reporter(s) fields. <br><br>The Reporting Period, Status, and Comment fields will be editable.',
                    centerVertical: true
                });               
            }
            else {
                duplicateSelectedRow(selectedRow)
            }
        }

        function duplicateSelectedRow(selectedRow) {
            bootbox.alert({
                title: 'Not currently working :)',
                message: 'When this works, a row will be added underneath the row you selected, and it will be populated with data for this objective: <br><br>' + selectedRow.ReporterObjectiveStatusUpdateFullDetail.ObjectiveDescription,
                centerVertical: true
            }); 
            console.log(selectedRow);

        }


RL Ryan Lim February 5, 2021 03:42 PM UTC

Can anyone tell me, how to develop a theme like apksclub in ASP.NET Core or MVC?


JG Jessica Goodrich February 5, 2021 08:10 PM UTC

Ryan Lim - you should make your question its own thread. That has nothing to do with this thread...


JG Jessica Goodrich February 9, 2021 07:45 PM UTC

Any ideas on this? Haven't heard back yet...


JG Jessica Goodrich February 10, 2021 12:30 AM UTC

Ok, I have made some more progress. My grid is using beforeBatchAdd=checkForSelectedRow

That function looks like this:

function checkForSelectedRow(args) {
            var grid = document.getElementById("Grid").ej2_instances[0];
            var selectedRow = grid.getSelectedRecords()[0];
            if (selectedRow === undefined) {
                args.cancel = true;
                bootbox.alert({
                    title: 'No record selected to copy from',
                    message: 'You must select a row that you would like to duplicate in order to add a new row. ' +
                        'The row you select will be used to determine which Action/ Objective you are creating a Status Update for and will populate the Division, Focus Area, Objective / Action, and FY fields. <br> <br>' +
                        'The Reporting Period, Status, and Comment fields will be editable. <br><br>' +
                        'The new row will appear at the top of the grid. After updating your changes, it will be sorted.',
                    centerVertical: true
                });               
            }
            else {
                args.defaultData["ReporterObjectiveStatusUpdateFullDetail"]["ObjectiveId"] = selectedRow["ReporterObjectiveStatusUpdateFullDetail"]["ObjectiveId"];
                args.defaultData["ReporterObjectiveStatusUpdateFullDetail"]["FiscalYearSortOrder"] = selectedRow["ReporterObjectiveStatusUpdateFullDetail"]["FiscalYearSortOrder"];
                args.defaultData["ReporterObjectiveStatusUpdateFullDetail"]["StatusUpdateFiscalYearId"] = selectedRow["ReporterObjectiveStatusUpdateFullDetail"]["StatusUpdateFiscalYearId"];
                args.defaultData["ReporterObjectiveStatusUpdateFullDetail"]["DivisionDescription"] = selectedRow["ReporterObjectiveStatusUpdateFullDetail"]["DivisionDescription"];
                args.defaultData["ReporterObjectiveStatusUpdateFullDetail"]["AspectDescription"] = selectedRow["ReporterObjectiveStatusUpdateFullDetail"]["AspectDescription"];
                args.defaultData["ReporterObjectiveStatusUpdateFullDetail"]["ObjectiveDescription"] = selectedRow["ReporterObjectiveStatusUpdateFullDetail"]["ObjectiveDescription"];
                args.defaultData["ReporterObjectiveStatusUpdateFullDetail"]["StatusUpdateFiscalYearDescription"] = selectedRow["ReporterObjectiveStatusUpdateFullDetail"]["StatusUpdateFiscalYearDescription"];
                args.defaultData["Champions"] = selectedRow["Champions"];
                args.defaultData["Reporters"] = selectedRow["Reporters"];
                args.defaultData["ReportingPeriods"] = selectedRow["ReportingPeriods"];
                args.defaultData["Statuses"] = selectedRow["Statuses"];
            }
        }



So all that is missing is adding the new row underneath the selected row instead of at the top of the grid. Is this possible? I have tried args.index = x with no luck.


SM Shalini Maragathavel Syncfusion Team February 10, 2021 12:48 PM UTC

Hi Jessica, 

Sorry for the late reply. 

By default in EJ2 Grid editing the newly added row element will be added in the DOM virtually (i.e) which will not reflected to the gridRows. When we save the newly added record then only it will be reflected to the gridRows, so it is not feasible to insert the newly added row element under the selected row. 

Regards, 
Shalini M 


Marked as answer

JG Jessica Goodrich February 11, 2021 08:03 PM UTC

Thanks for letting me know!


SM Shalini Maragathavel Syncfusion Team February 12, 2021 12:57 PM UTC

Hi Jessica, 

Thanks for your update. 

We are glad that the provided solution resolved your query. Please get back to us if you need further assistance. 

Regards, 
Shalini M. 
 


Loader.
Up arrow icon