Call Function on DataBound of Every Instance of Widget

2 Answers 31 Views
Grid
Lee
Top achievements
Rank 2
Bronze
Bronze
Bronze
Lee asked on 24 Jun 2025, 05:43 PM
I have a lot of projects with kendo grids in them. As a matter of practice, I always call the same function on the dataBound event. Instead of adding this every time I initialize a widget, I would like to set it as a default so that any time I initialize a kendo grid, it is already set. I don't want to give up the ability to add additional functions to specific grids though. Is this possible? 

2 Answers, 1 is accepted

Sort by
1
Nikolay
Telerik team
answered on 27 Jun 2025, 01:41 PM

Hi Lee,

A possible approach would be if you instantiate the Grids with a JSON literal of options, save a base form of that literal and .extend() it with particulars for each instance. This base literal will have the databound handler.

var baseGridConfig = {
            dataBound: function(e) {
                // Common functionality for all grids
                console.log('Default dataBound executed for:', e.sender.element[0].id);
            },
            sortable: true,
            pageable: {
                pageSize: 5,
                buttonCount: 5
            },
            height: 300,
            scrollable: true
        };

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

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.

Lee
Top achievements
Rank 2
Bronze
Bronze
Bronze
commented on 27 Jun 2025, 01:58 PM

Thanks. This gives me a start. I'm still going to figure out how to merge the default dataBound event with the custom one so that both fire but I think I can get inventive there.
0
Rizwan
Top achievements
Rank 1
Iron
answered on 12 Aug 2025, 03:06 PM | edited on 16 Aug 2025, 03:35 AM

Yes, it's possible to set a default dataBound function for all Kendo Grids without losing the ability to add specific functions to individual grids.

You can achieve this by extending the Kendo UI Grid's default behavior. Here's how you can do it:

  1. Create a custom default function that you want to be called on the dataBound event of all Kendo Grids.

  2. Override the dataBound event globally by using Kendo's kendo.ui.Grid.fn to hook into the grid's event lifecycle.

Here's an example solution:


// Define the default function you want to call on dataBound
function defaultDataBound(e) {
    // Your default function logic here
    console.log('Default dataBound function called');
}

// Extend Kendo Grid's default dataBound event
var originalGrid = kendo.ui.Grid.fn;

kendo.ui.Grid.fn = $.extend(true, {}, originalGrid, {
    events: $.extend({}, originalGrid.events, {
        dataBound: "dataBound"
    }),
    dataBound: function(e) {
        defaultDataBound(e); // Call the default function
        if (originalGrid.fn.dataBound) {
            originalGrid.fn.dataBound.call(this, e); // Call the grid's specific dataBound function
        }
    }
});

// Initialize a Kendo Grid, it will automatically call the default function on dataBound
$("#grid").kendoGrid({
    dataSource: data,
    columns: columns,
    dataBound: function(e) {
        // Additional logic specific to this grid
        console.log('Specific dataBound logic');
    }
});

  • The defaultDataBound function is called whenever the dataBound event is triggered for any grid.

  • You can still add a dataBound function specific to an individual grid. The global function will be called first, followed by any grid-specific function.

This solution ensures you have a default behavior without losing flexibility for individual grid customizations.

Thanks & Regards
Team XtraSaaS

Tags
Grid
Asked by
Lee
Top achievements
Rank 2
Bronze
Bronze
Bronze
Answers by
Nikolay
Telerik team
Rizwan
Top achievements
Rank 1
Iron
Share this question
or