Get/Set selected row woes

3 posts, 0 answers
  1. Randy Hompesch
    Randy Hompesch avatar
    148 posts
    Member since:
    Nov 2008

    Posted 23 Apr 2019 Link to this post

     

    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. Eyup
    Admin
    Eyup avatar
    4074 posts

    Posted 23 Apr 2019 Link to this post

    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.
  3. Marianne
    Marianne avatar
    5 posts
    Member since:
    Mar 2020

    Posted 24 Apr 2020 in reply to Eyup Link to this post

    it's a year later but your code example helped me with one problem. thank you
Back to Top