Grid edition regression issue on limiting number of rows

2 Answers 23 Views
Grid
Laurent
Top achievements
Rank 2
Iron
Iron
Laurent asked on 28 Nov 2023, 04:10 PM

Hi team,

I need to limit the number of entries in an editable grid.

Consider the following dojo:

https://dojo.telerik.com/eDeDEtAM

Number of rows is limited to 2. Works fine before 2023-R1-SP1 and fails since.

I tried to find a workaround without success. I tried beforeEdit event but it fails aborting creation of a new row...

It's a regression issue, please help!

Best regards,

Laurent.

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Zornitsa
Telerik team
answered on 06 Dec 2023, 10:22 AM

Hello Laurent,

Indeed, it seems that the suggested approach using the cancelChanges() method causes undesired behavior in some other cases like the ones you described in your reply. 

What I would suggest for an alternative approach, aside from the one you have implemented, is configuring your own custom button in the Grid toolbar, that would be responsible for adding new rows:

toolbar: [
     { name: 'add-row-custom', template: kendo.template($('#template').html()) }
]

 

<script type="x-kendo/template" id="template">
    <button class="k-button k-button-md k-rounded-md k-button-solid k-button-solid-base k-toolbar-button k-grid-add k-toolbar-tool" role="button">Add new record</button>
</script>

Then, you could implement your previous approach for preventing the addition of new rows based on the desired condition, and use the addRow() method if the condition is not valid:

$("#grid").find(".k-grid-add").on("click", function(e) {
          if (grid.dataSource.total() >= 2) {					
            console.log("reach max number of lines");
            return false;
          } else {
            grid.addRow();
          }
});

 

Below I am linking a Dojo example, in which you can observe the suggested approach:

I hope that the proposed suggestion would be suitable for your scenario.

Please let me know if you have any additional questions.

Regards,
Zornitsa
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources
Laurent
Top achievements
Rank 2
Iron
Iron
commented on 06 Dec 2023, 10:36 AM

Hi Zornitsa,

Thank you for this response and I think this is a good workaround.

Maybe in the future we could have a new Grid event like "beforeAdd" that may be prevented.

Thank you for all.

Best regards,

Laurent.

0
Zornitsa
Telerik team
answered on 01 Dec 2023, 01:21 PM

Hi Laurent,

Since the 2023 R1 SP1 Kendo release, the toolbar of the Grid has been substituted with the Kendo UI Toolbar component. You may observe this information in our Kendo UI Release History, also linked below:

Due to this change, the internal attachment of the click handlers to the Toolbar buttons has also been modified, which is the reason why the behavior you described occurs. 

In the previous versions, the event was attached to an element in the Grid that contains the 'k-grid-add' class, so when you click on any element with this class, a new row would be added, e.g.:

$("#grid").on("click", ".k-grid-add", executeAddNewRow);
However, currently, in the case of the Toolbar component, the event handler is attached to the specific element/action button, not to any element with the above class:
$(commandButton).on("click", executeAddNewRow);

This means that, in the current case, your approach would not be able to prevent the execution of the handler for adding a new row to the Grid anymore.

Nevertheless, in order to overcome the experienced issue, I have prepared an alternative approach for you to achieve the desired scenario:

beforeEdit: function(e) {
    if (grid.dataSource.total() - 1 >= 2) {
      e.preventDefault();
      e.sender.cancelChanges();
    }
}

As you can see in the above code snippet, I am preventing the default action of the beforeEdit event that you have also mentioned in your reply, and then using the cancelChanges() method of the Grid to cancel any pending changes in the data source. 

Please note that since the approach with this event basically creates a new record and then removes it, the grid.dataSource.total() method would return the number of current records + the one record that is being created at the moment. That is the reason why I have also modified the if condition in the respective way.

Below I am sharing the provided Dojo example, demonstrating the desired behavior using the suggested approach:

Please observe the above information and let me know how that works for your scenario.

Regards,
Zornitsa
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources
Laurent
Top achievements
Rank 2
Iron
Iron
commented on 01 Dec 2023, 02:44 PM

Hi Zornitsa,

Thank you for your prompt response.

Unfortunately doing like that conducts to some weird behavior as soon as you add the "destroy" command.

Please consider the dojo: https://dojo.telerik.com/OsevOBAP in which I've only added the "destroy" command.

Then:

1- Run

2- Delete last line

3- Create a new one

4- Add new record --> will delete line you've just created !

Some other weird behavior can be observed if you also play with "edit" buttons.

I'm afraid cancelChanges() is doing more than expected.

 

For the moment, the only workaround I found is as follows:

beforeEdit: function(e) {
    if (e.model.isNew()) {
    if (e.sender.dataSource.total() > 2) {
      e.preventDefault();
      e.sender.removeRow(e.sender.tbody.find("tr").eq(0)); // remove extra line
    }
    }
  },

 

Best regards,

Laurent.

Tags
Grid
Asked by
Laurent
Top achievements
Rank 2
Iron
Iron
Answers by
Zornitsa
Telerik team
Share this question
or