簡體   English   中英

在Kendo UI Grid中選擇dataItem

[英]Selecting a dataItem in a Kendo UI Grid

我在MVC4應用程序中有KendoUI Grid,如下所示:

@(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"))
      )
      )

ProjectStatus列是一個Enum,它有一個名為StatusColumn的UIHint; 在這里:

恩歐姆:

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

視圖模型:

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

UIHint(部分視圖):

@(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")))

* 問題:*

我實際上嘗試獲取ProjectId值(上面顯示的網格中的一列),當在上面顯示的下拉UI提示上觸發saveProjectStatus事件時。

我設法得到如下所選的值:

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

但我不能為ProjectId做同樣的事情! 我曾經多次嘗試(上面都注釋掉了)在過去一小時內解決了這個問題但沒有成功。

我也很高興在DOM中隱藏行而不是獲取ProjectId,如果這也是可能的話。

如果可以的話請幫忙。

提前謝謝了。

看起來您的網格設置為單一可選,您可以像這樣訪問所選網格行的值。

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

編輯:是每行的下拉列表嗎? 並且您希望在下拉列表更改時觸發事件並獲取行的ID?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM