Hello,
In my JavaScript file I bind to serveral events of the grid like:
var
grid = $(
"#grdStandort"
).data(
"kendoGrid"
);
grid.bind(
"change"
,
function
() {
loadTabstripData(tabstrip.select().text());
});
tabstrip.bind(
"show"
,
function
() {
loadTabstripData(tabstrip.select().text());
});
this is working fine but now I want to bind to the "read" Event to set my parameters instead of using the .Data() property of the mvc grid (I don't want to set javascript in my grid Definition only want to bind/hook into Event from my external javscript file)
is there a event which I can bind to to set the parameters for the datasource?
robert
12 Answers, 1 is accepted
As documented in the following help topic, passing additional data is possible only by setting the event handler for the Data method:
Regards,
Konstantin Dikov
Telerik by Progress
We would like to hold our grid definition (MVC Razor) clean and want to bind/hook into event only in the seperate javascript (*.js) files...
robert
You can try the following:
grid.dataSource.transport.options.read.data = dataFunctionName;
The same could be applied for the update/destroy and create.
Hope this helps.
Regards,
Konstantin Dikov
Telerik by Progress
Hi,
Ok, but to which Event should I bind to?
grid.bind(
"?"
,
function
() {
grid.dataSource.transport.options.read.data = dataFunctionName;
});
You do not have to bind to any event, because the "dataFunctionName" from the example will be called before the Read action and you can pass the additional data within the function:
grid.dataSource.transport.options.read.data = dataFunctionName;
function dataFunctionName(e) {
//your code
}
Regards,
Konstantin Dikov
Telerik by Progress
Ok, but if i use this i have to set it before every refresh (Reload) - right?
and I also have to set Autobind(false) to avoid read on page load...
robert
You can set it only once and it should fire every time. However, the AutoBind will be mandatory if you need to pass the data for the initial load and this applies to every event handler bound manually, because those handlers will be attached after the initialization of the widget.
Regards,
Konstantin Dikov
Telerik by Progress
Hi Konstantin,
if I set this on initialization javascript and like this:
grdStandort.dataSource.transport.options.read.data = dataFiltergrdStandort();
and in the dataFiltergrdStandort function I refer to a textbox and then if a refresh the grid with the paging bar refresh then it seems that the function is not called again with the new value but with the last/cached one?
for me it looks like to set this with the statement above is not the same as in the .Data() of t´he read action in grid definition...
robert
function
dataFiltergrdStandort() {
var
filterid = 0;
if
($(
"#toolStandorteFilter_Alle"
).data(
"button"
).options.selected ===
true
)
filterid = 0;
if
($(
"#toolStandorteFilter_Active"
).data(
"button"
).options.selected ===
true
)
filterid = 1;
if
($(
"#toolStandorteFilter_Geloeschte"
).data(
"button"
).options.selected ===
true
)
filterid = 2;
return
{
mitgliedid: mitgliedid,
filterid: filterid,
textfilter: $(
"#txtStandortsuche"
).val()
};
}
if I set it in the requestStart it works - or is this not a good solution?
grdStandort.dataSource.bind(
"requestStart"
,
function
(e) {
grdStandort.dataSource.transport.options.read.data = dataFiltergrdStandort();
});
In my tests the approach that I have suggested fires the event every time before the call to the Read action, but please note that you need to set only the reference to the function:
grdStandort.dataSource.transport.options.read.data = dataFiltergrdStandort;
In your last post you are just calling the function on requestStart and you are setting the initial object to the read.data, which is why the value is always the same.
Hope this helps.
Regards,
Konstantin Dikov
Telerik by Progress
Hi Konstantin,
this works now perfect with the Kendo mvc grid - now I wanted to do the same approach with the Kendo mvc Treeview (Ajax loading) but this doesn't work as expected because the treeview sends the "id" on child loading and this id is not given to the controller but there are two calls one with the id and one with my parameter "mitgliedid"
what I'm missing?
robert
This approach will not work for the TreeView and you will have to define the function in the Data method of the action directly in the DataSource:
@(Html.Kendo().TreeView()
.Name("treeview")
.DataTextField("Name")
.DataSource(dataSource => dataSource
.Read(read => read
.Action("Employees", "TreeView").Data("getData")
)
)
)
Regards,
Konstantin Dikov
Telerik by Progress