Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use client template expressions in ajax binding of a mvc kendo grid?

I have a two-layer hierarchical grid that I am moving from server side binding to using ajax. The ajax reads for both layers of data are working correctly however I am having difficulty using the ClientTemplate to render my columns based on conditional logic.

Below is the server-side binding version. I understand I have to use ClientTemplate and expressions #=# for the same effect but I am having two problems:

  1. How to increment variable 'i' for each row so I can use CheckBoxFor and like html helper methods?
  2. How to convert the @ to use the ClientTemplate expression. Note the conditional logic uses properties of the Model of the View and also properties of the bound element (MyViewModel) with the conditional logic using a mixture of properties from the Model

Converting this to an expression would be most helpful.

var i = -1;

@(Html.Kendo().Grid<MyViewModel>()
  .Name("grid")
  .Columns(columns =>
  {
    columns.Bound(c => c.Selected).Title("")
      .Template(
        @<text>
          @{i++;}
          @if (Model.Permissions.HasInsertAccess && item.Status == Status.Created)
          {
            <input type="hidden" name="MyViewModels.Index" value="@i" />
            @Html.CheckBoxFor(m => m.MyViewModels[i].Selected)
          }
          </text>);

          columns.Bound(c => c.Id)
            .Template(@<text>@Html.HiddenFor(m => m.MyViewModels[i].Id)@item.Id</text>)
like image 656
David Avatar asked Aug 29 '13 07:08

David


1 Answers

Please try with the below code snippet.

VIEW

@model MvcApplication1.Models.TestModels

<script type="text/javascript">
var rowNumber = 0;

function resetRowNumber(e) {
    rowNumber = 0;
}

function renderNumber(data) {
    return ++rowNumber;
}

function renderRecordNumber(data) {
    var page = parseInt($("#Grid").data("kendoGrid").dataSource.page()) - 1;
    var pagesize = $("#Grid").data("kendoGrid").dataSource.pageSize();
    return parseInt(rowNumber + (parseInt(page) * parseInt(pagesize)));
}

</script>
@(Html.Kendo().Grid<MvcApplication1.Models.TestModels>()
.Name("Grid")
.Columns(columns =>
{
    columns.Bound(p => p.ID);
    columns.Bound(p => p.Name);

    columns.Template(t => { }).Title("Row No").ClientTemplate("# if ( '" + @Model.Permissions.HasValue.ToString().ToLower() + "' == 'true') { #" +
        "<input type='text' name='MyViewModels.Index' value='#= renderNumber(data) #' /> " +
        "# } #");

})
.Pageable(x => x.PageSizes(new int[] { 10, 20, 30, 50 }).Refresh(true))
.Sortable()
.Filterable()
.DataSource(dataSource => dataSource
    .Ajax()
        .Read(read => read.Action("Grid_Read", "Home"))

)
    .Events(ev => ev.DataBound("resetRowNumber"))
)

CONTROLLER

public ActionResult Index()
{
        TestModels model = new TestModels();
        model.Permissions = true; //Please comment this line and check
        return View(model);
}

public ActionResult Grid_Read([DataSourceRequest] DataSourceRequest request)
{
        List<TestModels> models = new List<TestModels>();

        for (int i = 0; i < 50; i++)
        {
            TestModels t1 = new TestModels();
            t1.ID = i;
            t1.Name = "Name" + i;
            models.Add(t1);

        }

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

MODEL

public class TestModels
{
    [Display(Name = "ID")]
    public int ID { get; set; }

    [Display(Name = "Name")]
    public string Name { get; set; }

    public bool? Permissions { get; set; }
}

Please try with the above code snippet. Let me know if any concern.

like image 151
Jayesh Goyani Avatar answered Oct 15 '22 03:10

Jayesh Goyani