通过javascript

时间:2015-10-20 09:16:19

标签: kendo-ui kendo-grid

我想通过内联编辑支持向kendo Grid添加行。我需要在初始化网格并加载数据后执行此操作。

我试过了:

$(document).ready(function () {

    gridAdd(); // no work

});

function gridAdd(){
     $(".k-grid-add").click()
}

当我在Grid中的DataBound事件中调用gridAdd()时,网格冻结和数据不会加载。执行此功能的正确事件是什么?

网格代码在gridAdd函数的代码之前:

                    @(Html.Kendo().Grid<TT.Web.Models.ViewModel.WorkViewModel>()
                      .Name("gridAdd")
                      .Events(events => events.Edit("gridEdit").DataBound("databoundinitAdd").Save("gridAddSaveChanges"))
                      .Columns(columns =>
                      {
                      columns.Template(x => x.Id)
                          .HeaderTemplate("<input id='mastercheckbox' type='checkbox'/>")
                          .ClientTemplate("#if(Status == 0 || Status == 3) {#  <input type='checkbox' name='checkedRecords' value='#= Id #' class='checkboxGroups'/> #} #")
                          .Width(30)
                          .HtmlAttributes(new { style = "text-align:center" })
                          .HeaderHtmlAttributes(new { style = "text-align:center" });
                      columns.Template(@<text></text>).Title("Status").ClientTemplate("#if(Status == 0) {# <i class='fa fa-bolt icc'></i> #}#" + "#if(Status == 1) {# <i class='fa fa-paper-plane icc'></i> #}#" + "#if(Status == 2) {# <i class='fa fa-check-square-o icc'></i> #}#" + "#if(Status == 3) {# <a class='popoverEl' data-toggle='popover' data-trigger='hover' tabindex='0' title='Důvod zamítnutí' data-content='#=RejectionReason#' ><i class='fa fa-ban icc popoverEl' ></i></a> #}#").HtmlAttributes(new { @class = "icc" });
                      columns.Bound(work => work.Date).EditorTemplateName("WorkDate").Title(@L("Date"));
                      columns.Bound(work => work.Project).ClientTemplate("#=Project.Name#").Width(250).Title(@L("ProjectNameP")); // .ClientTemplate("#=Project.Name#")
                      columns.Bound(work => work.Spp).ClientTemplate("#=Spp.Code#").Width(100);
                      columns.Bound(work => work.Operation).ClientTemplate("#=Operation.Code#").Width(100).Title(@L("TypeOfOperation"));
                      columns.Bound(work => work.Hours).Title(@L("Hours"));
                      //columns.Command(command => { command.Edit(); command.Destroy(); }).Width(172);

                      columns.Template(@<text></text>).ClientTemplate(
                             "<div class='btn-group'>" +
                             "#if(Status == 0 || Status == 3 ) {# <a class='btn btn-success btn-sm k-button-icontext k-grid-edit'><i class='fa fa-pencil'></i></a>#}#" +
                             "#if(Status == 0 || Status == 3 ) {# <a class='btn btn-sm btn-primary sendToApprove'  data-href='" + Url.Action("WorkToApprove", "Home") + "/#=Id#'><i class='fa fa-paper-plane'></i></a>#}#"
                           + "#if(Status == 0 ) {# <a data-href='" + Url.Action("WorkDelete", "Home") + "/#=Id#' class='btn btn-sm btn-danger delete-work-item' ><i class='fa fa-times'></i></a>#}#"
                           + "</div>"
                             ).Width(130);
                      })
                      .ToolBar(toolbar =>
                      {
                          toolbar.Create().Text(@L("AddRecord"));
                          //toolbar.Save();
                      })
                      .Editable(editable => editable.Mode(GridEditMode.InLine))
                      .DataSource(dataSource => dataSource
                        .Ajax()
                        .Batch(false)
                        .Events(events => events.RequestEnd("OnRequestEnd_TopLinePriceGrid"))
                        .PageSize(20)
                        //.Events(events => events.Error("error_handler"))
                        .Model(model =>
                        {
                            model.Id(p => p.Id);
                            model.Field(p => p.Operation).DefaultValue(ViewData["defaultOperation"] as TT.Web.Models.ViewModel.OperationViewModel);
                            model.Field(p => p.Spp).DefaultValue(ViewData["defaultSpp"] as TT.Web.Models.ViewModel.SppViewModel);
                            model.Field(p => p.Project).DefaultValue(ViewData["defaultProject"] as TT.Web.Models.ViewModel.ProjectViewModel);
                        })
                        .Read(read => read.Action("WorkRead", "Home").Data("currentWeekInfo")) // Přidádo HTTP parametr s vybranným týdnem
                        .Create(update => update.Action("EditingInline_Create", "Home").Data("currentWeekInfo"))
                        .Update(update => update.Action("EditingInline_Update", "Home").Data("currentWeekInfo"))
                        .Destroy(update => update.Action("EditingInline_Destroy", "Home").Data("currentWeekInfo"))
                       )
                      .Pageable() // Enable paging
                      .Sortable() // Enable sorting
                )

谢谢!

在数据绑定事件中

编辑未解决:,但存在问题。 GridAdd导致数据绑定事件,因此它反复调用并且更加明确冻结。

setTimeout(function(){
    $(".k-grid-add").click()
})

1 个答案:

答案 0 :(得分:0)

基本上,最好的方法是获取Grid客户端对象,然后使用客户端API添加行。请查看以下相关文档: