t_plusplus
t_plusplus

Reputation: 4209

Selecting a dataItem in a Kendo UI Grid

I have KendoUI Grid in an MVC4 application, as shown here:

@(Html.Kendo().Grid<ProjectSystem.Web.Models.ProjectModel>()
      .Name("Grid")
      .Editable(editable =>
          {
              if(User.IsInRole(Constants.Admin)) 
              { 
                editable.Mode(GridEditMode.InCell);
              }
              else
              {
                  editable.Enabled(false);
              }
          })
      .Sortable(sortable => sortable.AllowUnsort(false))
      .Navigatable()
      .ToolBar(toolbar =>
          {
              if (User.IsInRole(Constants.Admin))
              {
                  toolbar.Create();
                  toolbar.Save();
              }
          })
          .Selectable(s => s.Mode(GridSelectionMode.Single).Type(GridSelectionType.Row))
          .Columns(columns =>
      {
          columns.Bound(p => p.ProjectId);
              columns.Bound(p => p.Name);
              columns.Bound(p => p.Address);
              columns.Bound(p => p.Postcode);
              columns.Bound(p => p.Contact);
              columns.Bound(p => p.Files);
              columns.Bound(p => p.Link).ClientTemplate("<a href='#=Link#' target='_blank'>#=Files!=null && Files.length > 0  ? 'Dropbox' : ''  #</a>");
              columns.Bound(p => p.ProjectStatus);
              columns.Bound(p => p.Active).ClientTemplate("<input type='checkbox' class='chkboxActive' #= Active ? checked='checked' : '' # ></input>");
              columns.Bound(p => p.Unused).ClientTemplate("<input type='checkbox' class='chkboxUnused' #= Unused ? checked='checked' : '' # ></input>");
              columns.Bound(p => p.IsSMS).ClientTemplate("<input type='checkbox' class='chkboxIsSMS' #= IsSMS ? checked='checked' : '' # ></input>");
              columns.Bound(p => p.MaterialLink).Title("").ClientTemplate("<a href='/Material/index?projectId=#=ProjectId#'>#=GetText()#</a>");
          })
      .DataSource(dataSource => dataSource.Ajax()
                                          .Model(model => model.Id(p => p.ProjectId))
                                          .Batch(true)
                                          .Events(events => events.Error("error"))
                                          .Events(events => events.RequestEnd("onRequestEnd"))
                                          .Create(create => create.Action("ProjectCreate", "Project"))
                                          .Update(update => update.Action("ProjectUpdate", "Project"))
                                          .Read(read => read.Action("ProjectRead", "Project").Data("ExtraData"))
      )
      )

The ProjectStatus column is an Enum, which has a UIHint called StatusColumn; both shwon here:

The Enum:

public enum ProjectStatus
{
    Open = 1,
    Closed = 2
}

The View Model:

[UIHint("StatusColumn")]
[Display(Name = "Status")]
public ProjectStatus ProjectStatus { get; set; }

The UIHint (Partial View):

@(Html.Kendo().DropDownList().Name("ProjectStatus")
.BindTo(new List<DropDownListItem>
            {
                new DropDownListItem{Text = "", Value = "0"},
                new DropDownListItem{Text = "Open", Value = "1"},
                new DropDownListItem{Text = "Closed", Value = "2"}
            }
    ).Events(e => e.Select("saveProjectStatus")))

*The Problem: *

I am actually trying to get the ProjectId value (a column in the above-shown grid), when the saveProjectStatus event is fired on the dropdown UI hint shown above.

I managed to get the selected value as below:

function saveProjectStatus(e) {
    debugger;
    var grid = e.sender;

    var row = grid.select();
    var currentDataItem = grid.dataItem(this.select());
    var selectedValue = currentDataItem.Text;

     //     var data = grid.dataItem("tr:eq(1)");
    //   var dataItem = grid.dataItem($(this).closest('tr'));
    // var uid = currentDataItem.uid; //.Name;

    //  var dataContext = grid.dataSource.getByUid(uid);

    //      var parentRow = e.wrapper.closest("tr");

    //var uid = $(e)parent().parent().attr('data-uid');
    //var dataContext = grid.dataSource.getByUid(uid);
}

But I couldn't do the same for the ProjectId! I have had several attempts (all commented out above) to solve this in the past hour but no success.

I could also be happy with hiding the row in DOM instead of getting the ProjectId, if that is also possible.

Please help if you can.

Many thanks in advance.

Upvotes: 1

Views: 9324

Answers (1)

CSharper
CSharper

Reputation: 5590

It looks like your grid is set up to be single selectable, You can access values of the selected grid row like this.

$('#Grid').click(function () {
    var gview = $(this).data("kendoGrid");
    var selectedItem = gview.dataItem(gview.select());
    var ProjectId = selectedItem.ProjectId;       
})

edit: is the dropdown in each row? and you want an event to fire on the the dropdown change and grab the Id of the row?

Upvotes: 3

Related Questions