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

Getting item index in BindTo method

3 Answers 80 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Ian
Top achievements
Rank 1
Ian asked on 08 Oct 2013, 03:58 PM
I wanted to set the menu item selection based on a persisted item index but cannot see how I can access the menu item index from within the BindTo() section. I have worked around this in the code below by matching on the label but this is not ideal. Is it possible to compare the item index instead?  Thanks.

@(Html.Kendo().Menu()
      .Name("viewTypeMenu")
      .HighlightPath(false)
      .BindTo(Model.ViewTypeLabels, (item, value) =>
      {
          item.Text = value;
          item.Selected = (value == Model.SelectedViewTypeLabel);
      })
)

3 Answers, 1 is accepted

Sort by
0
Ignacio
Top achievements
Rank 1
answered on 09 Oct 2013, 07:36 PM
It depends on the type of "ViewTypeLabels".
If its simply a List<string> you can use IndexOf.

Example:
@(Html.Kendo().Menu()
      .Name("viewTypeMenu")
      .BindTo(Model.ViewTypeLabels, (kendoMenuItem, labelValue ) => {
          kendoMenuItem.Text = labelValue;
          kendoMenuItem.Selected = (Model.ViewTypeLabels.IndexOf(labelValue) == Model.PersistedIndex);
      })
)

0
Daniel
Telerik team
answered on 10 Oct 2013, 09:46 AM
Hi,

You could also use a separated parameter to track the index:

@{
    var index = 0;
}
 
@(Html.Kendo().Menu()
      .Name("viewTypeMenu")
      .HighlightPath(false)
      .BindTo(Model.ViewTypeLabels, (item, value) =>
      {
          item.Text = value;
          item.Selected = index++ == selectedIndex;
      })
)

 
Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Ian
Top achievements
Rank 1
answered on 10 Oct 2013, 03:33 PM
Thanks for your solutions Niko and Daniel. Cheers.
Tags
Menu
Asked by
Ian
Top achievements
Rank 1
Answers by
Ignacio
Top achievements
Rank 1
Daniel
Telerik team
Ian
Top achievements
Rank 1
Share this question
or