This is a migrated thread and some comments may be shown as answers.

Get/Set selected row woes

2 Answers 426 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Randy Hompesch
Top achievements
Rank 1
Randy Hompesch asked on 23 Apr 2019, 09:54 AM

 

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)
        )
    )

2 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 24 Apr 2019, 12:17 AM
Hello Randy,

I've already replied to your query in your support ticket. I suggest that we continue our conversation in the mentioned thread.

Meanwhile, I will also paste the response here so that it can be helpful to other developers as well:

You can achieve this requirement using the following approach:
function OnRunReport(event) {
    var button = $(event.target);
    var row = button.parents("tr").first();
  
    var index = row.index();
    var value = this.dataItem(row).OrderID;
  
    alert(index + " - " + value);
}

You don't need to select the item in this case and you don't even need the index.

I hope this will prove helpful.

Regards,
Author nickname
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
 


Regards,
Eyup
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Marianne
Top achievements
Rank 1
Iron
Iron
answered on 24 Apr 2020, 10:09 PM
it's a year later but your code example helped me with one problem. thank you
Tags
Grid
Asked by
Randy Hompesch
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Marianne
Top achievements
Rank 1
Iron
Iron
Share this question
or