on click event not firing after sort

3 Answers 47 Views
Grid
Benjamin
Top achievements
Rank 2
Iron
Iron
Veteran
Benjamin asked on 04 Nov 2024, 07:00 AM

my onclick event not firing after grid sort.

grid columns markup


columns: [
   
                    {
                        field: "", title: "", width: "150px", attributes: { "class": "ps-0" }, template: function(dataItem) {
                            var actionHtmlContent = '';
                            if (dataItem && dataItem.canEdit) {

                                actionHtmlContent += '<a class="btn btn-link"  href="' + contactDetailLink + '/1/' + dataItem.id + '">';
                                actionHtmlContent += '<em class="material-icons material-edit" style="color:#0D4EA2;"></em>';
                                actionHtmlContent += '</a>';
                            }
                            if (dataItem && dataItem.canDelete) {

                                actionHtmlContent += '<a class="btn btn-link delete-button" title=" " aria-label=" " href="?AppContactID=' + encodeURIComponent(dataItem.id) + ' &handler=Delete">';
                                actionHtmlContent += '<em class="material-icons material-delete" style="color:#0D4EA2;"></em>';
                                actionHtmlContent += '</a>';
                            }
                            return actionHtmlContent;
                        }
                    },
       
    { field: "salutation", title: "Salutation", width: "130px" },
    { field: "fullName", title: "Full Name", width: "200px" },

 

code to attach onclick


$(".delete-button").on("click", function () {
    return confirm('Are you sure you want to proceed?');
});

 

my js tag has nounce, thus i can't add the event listener to the html tag itself.

 

after i sort the grid, when user click on the .delete-button the confirm popup is not showing

3 Answers, 1 is accepted

Sort by
0
Accepted
Nikolay
Telerik team
answered on 06 Nov 2024, 03:34 PM

Hello Benjamin,

The reason for the lost click handler is that the Grid content is re-rendered and the $.on("click") is lost. You need to re-attach it in the sort Grid  event handler or define it with onclick in the template:

template: function(dataItem) {
            return '<a onclick="customClick()" class="btn btn-link delete-button" title=" " aria-label=" " &handler=Delete">Delete</a>';
          }
...
      function customClick() {
        return confirm('Are you sure you want to proceed?');
      }

Dojo demo: https://dojo.telerik.com/QoexeqjA

Regards,
Nikolay
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Ashwani
Top achievements
Rank 1
Iron
answered on 07 Nov 2024, 12:20 PM
Another Possible answer is available in this dojo: https://dojo.telerik.com/IpxBxIHk
Here I used databound(), additionally this provides csp complaint code as we should not use 'onclick' event to avoid csp issues.
Nikolay
Telerik team
commented on 12 Nov 2024, 08:52 AM

Hi Ashwani,

Thank you for sharing this approach with the community. It is also a valid one.

Regards,

Nikolay

0
Alex
Top achievements
Rank 1
Iron
answered on 24 Dec 2024, 06:46 AM | edited on 24 Dec 2024, 06:48 AM
I like that both Nikolay and Ashwani provided solutions. It's helpful to see different approaches. heardle
Tags
Grid
Asked by
Benjamin
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Nikolay
Telerik team
Ashwani
Top achievements
Rank 1
Iron
Alex
Top achievements
Rank 1
Iron
Share this question
or