ParamQuery grid support forum

General Category => Help for ParamQuery Pro => Topic started by: kshipra on April 16, 2018, 06:03:23 am

Title: Add new row with values from existing row
Post by: kshipra 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");
                }
            }
Title: Re: Add new row with values from existing row
Post by: paramvir 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.
Title: Re: Add new row with values from existing row
Post by: kshipra 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
Title: Re: Add new row with values from existing row
Post by: kshipra 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?
Title: Re: Add new row with values from existing row
Post by: paramvir on April 17, 2018, 11:52:18 am
Kindly share a jsfiddle.