admin管理员组

文章数量:1415664

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 mented 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.

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 mented 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.

Share Improve this question edited Jan 27, 2014 at 15:28 t_plusplus asked Jan 27, 2014 at 15:20 t_plusplust_plusplus 4,2195 gold badges47 silver badges61 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

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?

本文标签: javascriptSelecting a dataItem in a Kendo UI GridStack Overflow