Telerik mvc 网格和自定义命令

发布于 2024-11-26 16:02:05 字数 1911 浏览 0 评论 0原文

使用 Telerik mvc grid 和 ajax,让我有些头疼。 我试图插入带有一些简单链接的列,以执行与删除命令相同的行为(因为我只是不想要默认命令列)。 但我失败了。 默认的删除命令效果很好:删除记录并刷新网格。 我的自定义链接,只删除记录,但网格不刷新。

这是我的代码。也许我错过了一件简单的事情。

视图:

@model  Benner.Saude.Mapeamento.Especialidade[]


@(Html.Telerik().Grid(Model)
    .Name("Grid")
    .DataKeys(keys => keys.Add(c => c.Handle))         
    .DataBinding(dataBinding => dataBinding
        .Ajax()
        .OperationMode(GridOperationMode.Client)
        .Select("AjaxPesquisar", "Especialidade")
        .Update("AjaxAtualizar", "Especialidade")
        .Delete("Delete", "Especialidade"))
    .HtmlAttributes(new { @class = "grid-padrao" })
    .ClientEvents(events => events
        .OnDataBound("atualizarCss")
    )
    .Columns(columns =>
    {
        .ClientTemplate("<text><a href='/Especialidade/Delete/33' class='formatacao delete-link' image='delete'/></text>")
        .Width(20).Title("Commands");  ***this does not works ***


        columns.Bound("Descricao").Title("Descrição");
        columns.Bound("Handle").Title("Código");
        columns.Command(commands =>
        {
            commands.Delete().ButtonType(GridButtonType.BareImage); ***this works***

        }).Width(70);

    })        
    .Pageable()
    .Sortable()

    )

控制器:

    [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
    [GridAction]
    public ActionResult Delete(int id)
    {
        cadastro.ExcluirEspecialidade(Session["token"].ToString(), id);
        Especialidade[] especialidades = consulta.PesquisarEspecialidades(Session["token"].ToString());
        return View(new GridModel(especialidades));
    }

JavaScript:

$("a.delete-link").click(function (event) {
    var link = $(this)[0];

    if (confirm("Confirm delete?")) {
        $.post(link.href);
    }

    return false;
});

Using telerik mvc grid with ajax, give me some headaches.
I am trying to insert a column with some simple links, to perform the same behaviors that Delete command does (because I just don't want the default command column).
But I am fail.
The default Delete command works great: delete the record and refresh the grid.
My custom link, only delete the record, but the grid is not refreshed.

Here is my code. Maybe I am missing a simple thing.

View:

@model  Benner.Saude.Mapeamento.Especialidade[]


@(Html.Telerik().Grid(Model)
    .Name("Grid")
    .DataKeys(keys => keys.Add(c => c.Handle))         
    .DataBinding(dataBinding => dataBinding
        .Ajax()
        .OperationMode(GridOperationMode.Client)
        .Select("AjaxPesquisar", "Especialidade")
        .Update("AjaxAtualizar", "Especialidade")
        .Delete("Delete", "Especialidade"))
    .HtmlAttributes(new { @class = "grid-padrao" })
    .ClientEvents(events => events
        .OnDataBound("atualizarCss")
    )
    .Columns(columns =>
    {
        .ClientTemplate("<text><a href='/Especialidade/Delete/33' class='formatacao delete-link' image='delete'/></text>")
        .Width(20).Title("Commands");  ***this does not works ***


        columns.Bound("Descricao").Title("Descrição");
        columns.Bound("Handle").Title("Código");
        columns.Command(commands =>
        {
            commands.Delete().ButtonType(GridButtonType.BareImage); ***this works***

        }).Width(70);

    })        
    .Pageable()
    .Sortable()

    )

Controller:

    [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
    [GridAction]
    public ActionResult Delete(int id)
    {
        cadastro.ExcluirEspecialidade(Session["token"].ToString(), id);
        Especialidade[] especialidades = consulta.PesquisarEspecialidades(Session["token"].ToString());
        return View(new GridModel(especialidades));
    }

Javascript:

$("a.delete-link").click(function (event) {
    var link = $(this)[0];

    if (confirm("Confirm delete?")) {
        $.post(link.href);
    }

    return false;
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无畏 2024-12-03 16:02:05

您必须获取网格对象并在ajax回调中调用重新绑定,例如

$("a.delete-link").click(function (event) {
    var link = $(this)[0];

    if (confirm("Confirm delete?")) {
        $.post(link.href, function(data)
          {
             var $grid = $("#Grid").data("tGrid");
             $grid.rebind();
          });
    }

    return false;
});

$("#Grid")中“Grid”是页面上网格控件的名称。其余的是语法

you have to get grid object and call rebind in ajax callback like

$("a.delete-link").click(function (event) {
    var link = $(this)[0];

    if (confirm("Confirm delete?")) {
        $.post(link.href, function(data)
          {
             var $grid = $("#Grid").data("tGrid");
             $grid.rebind();
          });
    }

    return false;
});

In $("#Grid") "Grid" is name of your grid control on the page. rest is syntax

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文