简体   繁体   English

无法使用jquery打开kendo网格的编辑弹出窗口

[英]Cannot open the edit popup of kendo grid using jquery

I am implementing a context menu for my kendo grid on MVC page. 我正在为MVC页面上的kendo网格实现上下文菜单。 I am trying to call the edit button on my kendo grid using on click of the context menu. 我正在尝试使用单击上下文菜单调用我的kendo网格上的编辑按钮。 I have implemented event on my context menu and on the event written jquery code to call fire the click event of the edit button. 我已经在我的上下文菜单和事件编写的jquery代码上实现了事件来调用编辑按钮的click事件。 I do see the window popping up for a split second and closing. 我确实看到窗口弹出一瞬间关闭。 How do I get this working 我如何让这个工作

@(Html.Kendo().ContextMenu()
        .Name("menu")
        .Target("#GridTeam")
        .Filter("tr")
        .Orientation(ContextMenuOrientation.Vertical)
        .Animation(animation =>
        {
            animation.Open(open =>
            {
                open.Fade(FadeDirection.In);
                open.Duration(500);
            });
        })
         .Items(items =>
         {
             items.Add()
                 .Text("Edit");

             items.Add()
                  .Text("Delete");
         })

       .Events(e =>
       {
           e.Select("onEdit");

       })
         )


 function onEdit(e) {
            //Logic to be executed on Edit event
            $('a.k-grid-edit').click();

You can use addRow , editRow and removeRow . 您可以使用addRoweditRowremoveRow

Model 模型

public class ViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

View 视图

<script type="text/javascript">

    function onSelect(e) {
        var grid = $("#GridTeam").data("kendoGrid");

        switch ($(e.item).children(".k-link").text()) {
        case "Add":
            grid.addRow(e.target);
            break;
        case "Edit":
            grid.editRow(e.target);
            break;
        case "Delete":
            grid.removeRow(e.target);
            break;
        }
    }
</script>

@(Html.Kendo().Grid<ViewModel>()
        .Name("GridTeam")
        .Columns(columns =>
        {
            columns.Command(command => { command.Edit(); command.Destroy(); });
            columns.Bound(d => d.Name).Title("Name");
            columns.Bound(d => d.Description).Title("Description");
        })
        .Editable(editable => editable.Mode(GridEditMode.PopUp))
        .DataSource(dataSource => dataSource
            .Ajax()
            .Model(model => model.Id(d => d.Id))
            .Read(read => read.Action("Data_Read", "Home"))
            .Update(update => update.Action("Data_Update", "Home"))
            .Destroy(update => update.Action("Data_Destroy", "Home")))
)

@(Html.Kendo().ContextMenu()
        .Name("menu")
        .Target("#GridTeam")
        .Filter("tr")
        .Orientation(ContextMenuOrientation.Vertical)
        .Animation(animation =>
        {
            animation.Open(open =>
            {
                open.Fade(FadeDirection.In);
                open.Duration(500);
            });
        })
        .Items(items =>
        {
            items.Add().Text("Add");
            items.Add().Text("Edit");
            items.Add().Text("Delete");
        })
        .Events(e => e.Select("onSelect"))
)

Controller 调节器

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Data_Read([DataSourceRequest] DataSourceRequest request)
    {
        return Json(_models.ToDataSourceResult(request));
    }

    [HttpPost]
    public ActionResult Data_Update([DataSourceRequest] DataSourceRequest request, ViewModel viewModel)
    {
        var model = _models.FirstOrDefault(x => x.Id == viewModel.Id);
        if (model != null)
        {
            model.Name = viewModel.Name;
            model.Description = viewModel.Description;
        }
        return Json(ModelState.IsValid ? new object() : ModelState.ToDataSourceResult());
    }

    [HttpPost]
    public ActionResult Data_Destroy([DataSourceRequest] DataSourceRequest request, ViewModel viewModel)
    {
        _models.Remove(_models.First(x => x.Id == viewModel.Id));
        return Json(new[] {_models}.ToDataSourceResult(request, ModelState));
    }

    // Created as static to simulate data from database
    private static List<ViewModel> _models = new List<ViewModel>
    {
        new ViewModel {Id = 1, Name = "One", Description = "One Hundred"},
        new ViewModel {Id = 2, Name = "Two", Description = "Two Hundreds"},
        new ViewModel {Id = 3, Name = "Three", Description = "Three Hundreds"},
    };
}

Screen Shot 屏幕截图

在此输入图像描述

This should work. 这应该工作。 First you get your grid instance. 首先你得到你的网格实例。 Then from context menu event find which row was clicked on. 然后从上下文菜单事件中找到单击的行。 And then just put that row into edit mode. 然后将该行置于编辑模式。

function onEdit(e) {
    //Logic to be executed on Edit event

    var grid = $("#GridTeam").data("kendoGrid");
    var model = e.target;
    grid.editRow(model)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM