This should be easy, but appears not.
I have a grid with a custom command:
columns.Command(c => c.Custom("Run Report").Click("OnRunReport")).Width(140);
selectable is set as
.Selectable(s => s.Mode(GridSelectionMode.Single).Type(GridSelectionType.Row))
My OnRunReport needs to get the selected row index and set it as the selected row. This needs to happen before I call:
// this next line only works if .Selectable() is set.
var idx = $("#grid").data("kendoGrid").select().index()
because otherwise idx is -1. If I click on the desired row before clicking on the Run Report button, idx is correctly set.
How to force the selection of the row in the click event of the custom command (In my case OnRunReport)???
The ultimate goal it to get the value of a particular cell for the selected row to be able to pass it to a report.
Any help would be great!
Thanks ... Ed
function OnRunReport(arg)
{
// this next line only works if .Selectable() is set.
var idx = $("#grid").data("kendoGrid").select().index();
var certid = this.dataSource._data[idx].id;
window.location.replace = "/CertificateReport/" + certid;
}
@(Html.Kendo().Grid<
IndexModel.CertificateModel
>()
.Name("grid")
.ToolBar(t =>
{
if (User.IsInRole("Admin"))
{
t.Create().Text("Add New"); t.Save().Text("Save Changes");
}
})
.HtmlAttributes(new { style = "height: 650px;" })
.Editable(e =>
{
if (User.IsInRole("Admin"))
{
e.Mode(GridEditMode.InCell);
}
else
{
e.Enabled(false);
}
})
.Columns(columns =>
{
columns.Bound(c => c.CertId).Width(100).Visible(false);
columns.ForeignKey(c => c.Crop.CropId, (System.Collections.IEnumerable)ViewData["crops"], "CropId", "CommonName")
.Title("Crop").Width(150);
columns.ForeignKey(c => c.Trait.TraitId, (System.Collections.IEnumerable)ViewData["traits"], "TraitId", "TraitDesc").Title("Trait").Width(150);
columns.Bound(c => c.UserFullName).ClientTemplate("#=UserFullName#").Width(150);
columns.ForeignKey(c => c.OriginCountry.CountryId, (System.Collections.IEnumerable)ViewData["originCountries"], "CountryId", "CountryName").Title("Origin Country").Width(150).HeaderHtmlAttributes(new { style = "overflow: visible; white-space: normal;" });
columns.ForeignKey(c => c.IssuingCountry.CountryId, (System.Collections.IEnumerable)ViewData["issuingCountries"], "CountryId", "CountryName").Title("Issuing Country").Width(150).HeaderHtmlAttributes(new { style = "overflow: visible; white-space: normal;" });
columns.Bound(c => c.YearIssued).Width(110)
.HtmlAttributes(new { style = "text-align:right; " })
.HeaderHtmlAttributes(new { style = "overflow: visible; white-space: normal" });
columns.Bound(c => c.ExpirationDate).Width(175).Format("{0:d}").HtmlAttributes(new { style = "text-align:right; " })
.HeaderHtmlAttributes(new { style = "overflow: visible; white-space: normal" }); ;
columns.Command(c =>
{
c.Destroy().Text("Delete");
}).Visible(User.IsInRole("Admin")).Width(100);
//columns.Command(c => c.Custom("Upload").Click("OnShowUpload")).Visible(User.IsInRole("Admin")).Width(100);
columns.Command(c => c.Custom("Run Report").Click("OnRunReport")).Width(140);
}).HtmlAttributes(new { style = "margin-left:3px" })
.Events(ge => ge.Change("OnChange"))
.Resizable(resize => resize.Columns(true))
.Selectable(s => s.Mode(GridSelectionMode.Single).Type(GridSelectionType.Row))
.Scrollable()
.Groupable()
.Filterable()
.Sortable()
.Pageable() //p => { p.PageSizes(true); })
.DataSource(ds =>
ds.Ajax()
.Batch(true)
.Events(ev => ev.Error("errorHandler"))
.ServerOperation(false)
.Read(r => r.Url("/Certificates?handler=CertsRead").Data("forgeryToken"))
.Update(u => u.Url("/Certificates?handler=CertsUpdate").Data("forgeryToken"))
.Create(c => c.Url("/Certificates?handler=CertsCreate").Data("forgeryToken"))
.Destroy(d => d.Url("/Certificates?handler=CertsDestroy").Data("forgeryToken"))
.Model(m =>
{
m.Id(c => c.CertId);
m.Field(f => f.CertId).Editable(false);
m.Field(f => f.Crop).DefaultValue(ViewData["defaultCrop"] as Crop);
m.Field(f => f.Trait).DefaultValue(ViewData["defaultTrait"] as Trait).Editable(User.IsInRole("Admin"));
m.Field(f => f.UserFullName).DefaultValue((string)ViewData["defaultUserFullName"]).Editable(false);
m.Field(f => f.IssuingCountry).DefaultValue(ViewData["defaultIssuingCountry"] as Country).Editable(User.IsInRole("Admin"));
m.Field(f => f.OriginCountry).DefaultValue(ViewData["defaultOriginCountry"] as Country).Editable(User.IsInRole("Admin"));
m.Field(f => f.YearIssued).DefaultValue((int)ViewData["defaultYearIssued"]).Editable(User.IsInRole("Admin"));
m.Field(f => f.ExpirationDate).DefaultValue((DateTime)ViewData["defaultExpiryDate"]).Editable(User.IsInRole("Admin"));
})
.PageSize(10)
)
)