Hello,
I am encountering a problem with the grid control and PersistSelection. After a user selects rows across the pages of the grid, there's a javascript function (ProcessGrid()) that builds an array of the selected rows and passes it as a parameter to an AJAX call on a controller method for processing. This process works correctly when a user manually selects the rows through clicking on the control. However, if I set the selected rows programatically by handling the DataBound event in Javascript, only the rows selected on the current page are picked up when I call ProcessGrid(). Note that after DataBound is handled, the correct rows are selected across every page.
Am I making a mistake somewhere when setting the selected rows in the DataBound event handler?
Sample grid:
@(Html.Kendo().Grid<
SampleApp.Models.CompanyViewModel
>()
.Name("grdCompanies")
.Columns(col=> {
col.Select().Width(50);
col.Bound(b=>b.CompanyName);
col.Bound(b=>b.CompanyType);
})
.ToolBar(tb => {
tb.Search();
})
.Search(search=> { search.Field(p=>p.CompanyName); })
.Pageable()
.Sortable()
.PersistSelection()
.Events(e=>e.DataBound("onDataBound_grdCompanies"))
.DataSource(ds => ds
.Ajax()
.Model(model=>model.Id(p=>p.CompanyID))
.Read(read=>read.Action("Company_Read","Sample"))))
DataBound event handler:
// set select flag on companies in progress
function
onDataBound_grdCompanies(e) {
var
m_grid =
this
;
var
m_dataSource = m_grid.dataSource;
// loop through each record
$.each(m_grid.items(),
function
(index, item) {
var
m_uid = $(item).data(
"uid"
);
var
m_dataItem = m_dataSource.getByUid(m_uid);
if
(m_dataItem.IsSelectedCompany ==
true
) {
m_grid.select($(item));
}
});
}
ProcessGrid()
function
ProcessGrid() {
var
m_ViewModelID =
'@Model.ViewModelID'
;
var
m_url =
'@Url.Action("Company_Save", "Sample")'
;
var
m_grid = $(
"#grdCompanies"
).data(
"kendoGrid"
);
var
m_SelectedCompanies = [];
m_grid.select().each(
function
() {
m_SelectedCompanies.push(m_grid.dataItem(
this
));
});
$.ajax({
type:
'POST'
,
url: m_url,
cache:
false
,
data: JSON.stringify({
pViewModelID: m_ViewModelID,
pCompanies: m_SelectedCompanies,
}),
dataType:
"json"
,
contentType:
'application/json;'
,
success:
function
(result) {
if
(result.indexOf(
"ERROR:"
) !== -1) {
// some code
}
}
});
}