Author Topic: Add new row with values from existing row  (Read 4207 times)

kshipra

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 94
    • View Profile
Add new row with values from existing row
« on: April 16, 2018, 06:03:23 am »
I am adding new rows by selecting existing rows. The problem is first time it creates empty blank row but then it adds new rows from existing data rows and works properly.

I do not understand why it is creating blank row for first time? Need help.

Code: [Select]
var arr = $grid.pqGrid("selection", { type: 'row', method: 'getSelection' });
           // console.log(arr);
            if (arr && arr.length > 0) {

                var colModel = $grid.pqGrid("getColModel");

                for (var index = 0; index < arr.length; index++) {
                   // var foo = arr[index].rowData;
                   /* console.log("arr[index].rowData");
                    console.log(arr[index].rowData);
                    console.log(foo);*/

                    // var clone = Object.assign({}, foo);

                    var clone = {};
                    for (var i = 0; i < colModel.length; i++)
                    {
                        var column = colModel[i];
                        //console.log(column.dataIndx);
                        if (column.dataIndx === "Id") {
                            console.log("Id match");
                            continue;
                        } else {
                            clone[column.dataIndx] = arr[index].rowData[column.dataIndx];

                            console.log(clone[column.dataIndx]);
                        }
                    }
                    clone.pq_rowcls = 'yellow';
                    clone.Deprecated = false;
                    clone.IsTemplate = false;
                    clone.Exclude = false;
                   

                    console.log("clone");
                    console.log(clone);

                    delete clone.Id;

                    console.log("remove Id from clone");
                   
                    var addIndx = $grid.pqGrid("addRow", {rowIndx: index, rowData: clone, checkEditable: true });
                   
                    $grid.pqGrid("setSelection", null);
                    $grid.pqGrid("refresh");
                }
            }

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Add new row with values from existing row
« Reply #1 on: April 16, 2018, 09:58:54 am »
Please share a jsfiddle so that I can check the issue faced by you.

From the source code, these 2 lines

Code: [Select]
$grid.pqGrid("setSelection", null);
$grid.pqGrid("refresh");

supposed to be outside the 2 loops.

kshipra

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 94
    • View Profile
Re: Add new row with values from existing row
« Reply #2 on: April 16, 2018, 11:53:25 am »
I figured out that the blank row is getting inserted because editable is returning false. 

Code: [Select]
editable: function (ui) {
                           
                            if (ui.column.title === "Id") {
                                return false;
                            }

                            if (ui.column.dataIndx === "Name") {
                                return ui.rowData.Id === undefined || ui.rowData.Id === '' || ui.rowData.Id <= 0 ? true : false;
                            }

                            if (ui.column.title === "BriefDescription" || ui.column.title === "ValidationComments" || ui.column.title === ValidationClassification")
                            {
                                return true;
                            }

                            if (ui.rowData != undefined)
                            {
                                if (ui.rowData.Id <= 0 || ui.rowData.Id == undefined || ui.rowData.Id === "") {
                                    return true;
                                }

                               return ui.rowData.IsEditable && item.IsEditable;
                            }

                            return true;
                        }

So ideally I want to turn off checkeditable: false when creating new row. If I do that, Id is still getting initialized with Id: "" even though i am removing Id property before assigning to the addRow

delete clone.Id;

var addIndx = $grid.pqGrid("addRow", {rowIndx: index, rowData: clone, checkEditable: false });
                   
so the addRow is getting treated as update instead of add and I see dirty cell red flag at top left corner of cell
« Last Edit: April 16, 2018, 12:01:02 pm by kshipra »

kshipra

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 94
    • View Profile
Re: Add new row with values from existing row
« Reply #3 on: April 16, 2018, 05:16:45 pm »
I am still having issue where I cannot do addRow.

If I turn off checkeditable: false when creating new row.  Id is still getting initialized with Id: "" even though i am removing Id property before assigning to the addRow

delete clone.Id;

var addIndx = $grid.pqGrid("addRow", {rowIndx: index, rowData: clone, checkEditable: false });
                   
so the addRow is getting treated as update instead of add and I see dirty cell red flag at top left corner of cell. Any suggestions?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Add new row with values from existing row
« Reply #4 on: April 17, 2018, 11:52:18 am »
Kindly share a jsfiddle.