Author Topic: Is there a function/method that I can use only on newly added rows?  (Read 55 times)

omerix

  • Pro Enterprise
  • Full Member
  • *
  • Posts: 155
    • View Profile
Hello,

I can perform various operations when adding rows using the addRow method. However, sometimes I use copy-paste to add multiple rows, and in this case, the addRow method is not triggered.

I tried using the beforePaste event, but it executes for all rows, including the existing ones that are not newly added.

What I need is an event that triggers only when a new row is added to the grid, whether through addRow or paste functionality. Is there something like a beforeRowAdd event or any alternative to achieve this?



This function "GridNew()" assigns default values to new rows.
I use it to add new rows and set default values for them, but it does not work for rows added via paste.

The goal is to make it work for rows added through paste as well.


Code: [Select]
window.GridNew = function () {
 
    function applyDefaultValues(rowData) {
        var defaultValues = { type: 1, code: 'newCode',quantity:1 };
        return Object.assign({}, defaultValues, rowData);
    }

    // Adding a new row
    var newRowData = applyDefaultValues({}); // Apply default values

    if (localStorage.getItem('qpRowBottom') == 1) {
        // Adding a new row at the bottom
        var pm = grid.option("pageModel");
        grid.option("pageModel", { type: 'local', curPage: 1, rPP: pm.rPP, rPPOptions: pm.rPPOptions });
        var rowIndx = grid.addRow({
            newRow: newRowData,
            track: true,
            history: false,
            checkEditable: false,
            refresh: true
        });
        console.log("New row added: Bottom");
        grid.goToPage({ rowIndx: rowIndx });
        grid.editFirstCellInRow({ rowIndx: rowIndx });
    } else {
        // Adding a new row at the top
        var rowIndx = grid.addRow({
            rowIndxPage: 0,
            newRow: newRowData,
            track: true,
            history: false,
            checkEditable: false,
            refresh: true
        });
        console.log("New row added: Top");
        grid.editFirstCellInRow({ rowIndx: rowIndx });
    }


};




Thank you for your help!







« Last Edit: January 16, 2025, 07:27:38 pm by omerix »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6344
    • View Profile
Re: Is there a function/method that I can use only on newly added rows?
« Reply #1 on: January 16, 2025, 07:50:47 pm »
beforeValidate and change are universal events which fire irrespective of the origin.

https://paramquery.com/pro/api#event-beforeValidate

https://paramquery.com/pro/api#event-change

ui.addList length can be checked in the event listener for newly added rows.

omerix

  • Pro Enterprise
  • Full Member
  • *
  • Posts: 155
    • View Profile
Re: Is there a function/method that I can use only on newly added rows?
« Reply #2 on: January 16, 2025, 08:37:03 pm »
I just discovered rowTemplate and managed to use it to set default values ​​for rows added with paste. I even removed the default value assignment from addRow as it was no longer needed.

It seems that rowTemplate does not affect my existing rows because I set the default values ​​to '' or 0 in the dataModel.

I will still take a look at beforeValidate and change things as you suggested. Thanks for the suggestion!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6344
    • View Profile
Re: Is there a function/method that I can use only on newly added rows?
« Reply #3 on: January 16, 2025, 09:24:33 pm »
If your only goal is to set default values for newly added rows, then rowTemplate is the right fit.
« Last Edit: January 17, 2025, 06:23:22 am by paramvir »