簡體   English   中英

如何通過雙擊從Kendo網格獲取行ID

[英]How to get the row ID from a Kendo Grid with a double click

我目前正在測試Kendo UI MVC擴展Beta。 我正在嘗試實現雙擊-編輯,但是我不知道如何獲取rowId。

JavaScript的:

$('#GridPedidos table tr').live('dblclick', function () {
    alert(' grid dbl clicked');
});

視圖:

@(Html.Kendo().Grid(Model) _
.Name("GridPedidos") _
    .Columns(Sub(column)
                 column.Bound(Function(item) item.idPedidoDocumentacao).Width("5%")
                 column.Bound(Function(item) item.descEstadoPedidoDoc).Width("25%")
                 column.Bound(Function(item) item.descTipoPedidoDoc).Width("25%")
                 column.Bound(Function(item) item.data).Width("25%").Format("{0:dd-MM-yyyy}")
                 column.Command(Function(item) item.Destroy()).Width("10%")
             End Sub) _
    .DataSource(Sub(ds)
                    ds.Ajax().ServerOperation(False).Read(Sub(s)
                                                              s.Action("GetListaGrid", "listaPedidos")
                                                          End Sub).Create(Sub(s)
                                                                              s.Action("detalhePedido", "Pedidos")
                                                                          End Sub).Model(Sub(m)
                                                                                             m.Id(Function(p) p.idPedidoDocumentacao)
                                                                                         End Sub).Destroy(Sub(d)
                                                                                                              d.Action("apagaPedido", "listaPedidos")
                                                                                                          End Sub)
                End Sub) _
    .Selectable()
)

我可以使用此功能檢測到雙擊,但是如何獲取ID?

我已經使用客戶端api和MVC擴展的等效項完成了此示例。

創建一個網格div,以在運行時創建一個網格。

<div id="grid" style="width: 400px;"></div>

創建一個行模板,以便我可以給該元素一個id標記。

<script id="rowTemplate" type="text/x-kendo-tmpl">
  <tr>
      <td id="EmployeeId">
        ${ EmployeeID }
      </td>
      <td>
        ${ FirstName }
      </td>
      <td>
        ${ LastName }
      </td>
  </tr>
</script>

初始化網格並綁定數據。

<script>
  $(document).ready(function () {
      $("#grid").kendoGrid({
          columns: [
              "EmployeeID"
              ,{
                  field: "FirstName",
                  title: "First Name"
              },{
                  field: "LastName",
                  title: "Last Name"
              }
          ],
          dataSource: {
              data: [
                  {
                      EmployeeID: 0,
                      FirstName: "Joe",
                      LastName: "Smith"
                  }, {
                      EmployeeID: 1,
                      FirstName: "Jane",
                      LastName: "Smith"
                  }
              ],
              schema: {
                  model: {
                      id: "EmployeeID",
                      fields: {
                          EmployeeID: {type: "number" },
                          FirstName: { type: "string" },
                          LastName: { type: "string" }
                      }
                  }
              },
              pageSize: 10
          },
          scrollable: {
              virtual: true
          },
          sortable: true,
          pageable: true,
          rowTemplate: kendo.template($("#rowTemplate").html())
      });

      //Add a double click event that will return the text in the EmployeeId column.
      $('#grid table tr').dblclick(function () {
          alert($(this).find("td[id=EmployeeId]")[0].innerText);
      });
  });
</script>

- 編輯 -

我還繼續創建了一個MVC擴展示例,該方法通過模板路由是相同的。

型號類別:

public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }
}

查看代碼:

<script type="text/javascript">
    function OnDataBound() {
        $('#OtherGrid table tr').dblclick(function () {
                alert($(this).find("span[id=EmployeeId]")[0].innerText);
        });
    }
</script>


@(Html.Kendo().Grid<Employee>()
     .Name("OtherGrid")
     .Columns(columns =>
     {
         columns.Bound(p => p.EmployeeId).ClientTemplate("<span id=\"EmployeeId\">#: EmployeeId #</span>");
         columns.Bound(p => p.Name);
     })
     .DataSource(dataSource => dataSource
         .Ajax() // Specify that the data source is of ajax type
         .Read(read => read.Action("GetEmployees", "Home")) // Specify the action method and controller name
     )
     .Events(e => e.DataBound("OnDataBound"))
)

控制器:

public ActionResult GetEmployees([DataSourceRequest]DataSourceRequest request)
{
    List<Employee> list = new List<Employee>();
    Employee employee = new Employee() { EmployeeId = 1, Name = "John Smith" };
    list.Add(employee);
    employee = new Employee() { EmployeeId = 2, Name = "Ted Teller" };
    list.Add(employee);

    return Json(list.ToDataSourceResult(request));
}

希望這可以幫助!

我用這個js實現了我想要的

$('#GridPedidos table tr').live('dblclick', function () {
var grid = $("#GridPedidos").data("kendoGrid");
var dItem = grid.dataItem($(this));

if (dItem != null) {
    detalhePedido(dItem.id);
}

});

要通過雙擊打開編輯模式,您需要在網格中注冊雙擊事件,如下所示:

var grid = $("#grid").data("kendoGrid");
grid.element.delegate("tbody>tr", "dblclick", function () {
    grid.editRow($(this));
});

暫無
暫無

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

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