ricastro
ricastro

Reputation: 422

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

I'm currently testing the Kendo UI MVC Extensions Beta. I'm trying to implement a double click - edit but I don't know how I can get the rowId.

JavaScript:

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

View:

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

I can detect the double click with this function, but how do I get the id?

Upvotes: 2

Views: 16986

Answers (3)

abuumar
abuumar

Reputation: 81

To open edit mode with double click you need to register the double click event with the grid like this:

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

Upvotes: 0

ricastro
ricastro

Reputation: 422

I achieved what i was looking for with this js

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

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

});

Upvotes: 0

Igorrious
Igorrious

Reputation: 1618

I've done this example with client side api and an equivalent with the MVC extensions.

Create a grid div, to create a grid at run time.

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

Created a row template so that I could give the element an id tag.

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

Initialize the grid and bind data.

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

--EDIT--

I've also gone ahead and created an MVC extensions example, the approach is the same via the template route.

Model class:

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

View code:

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

Controller:

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

Hope this helps!

Upvotes: 6

Related Questions