Telerik MVC Grid,真正的 Ajax 自定义绑定

发布于 2024-11-28 06:44:57 字数 907 浏览 2 评论 0原文

我使用的网格看起来像这样:

 Html.Telerik().Grid(Model).Name("preciousGrid").
     ... bla bla bla..
     .ClientEvents(events => events.OnDataBinding("onDataBinding"))
     .Columns(columns =>
         {
           columns.Bound(o => o.Date);
           columns.Bound(o => o.Name);

是的,我完全忽略 .DataBinding 的东西来使用自定义 ajax 调用。为什么?我需要向服务器发送更多数据,而不是简单的 id。收集数据的唯一方法是遍历 DOM 元素。因此,Telerik 建议的所有方法都不适用于我的情况。

一切正常 - 在 onDataBinding 中,收集必要的数据并将其发送到服务器后,服务器返回结果,网格显示该数据。

但仍然有一个问题。分页不起作用。页脚显示类似的内容:

在此处输入图像描述

有什么想法吗?

UPD:哦...也许我应该向服务器发送寻呼信息并基于此返回结果?怎么做呢?你能给我看一个样品吗?

UPD2:默认情况下,GridCommand 命令不会将分页信息发送到服务器(如果我在 $.ajax 中省略它,并且仍然在操作方法中放置 GridCommand 参数,它会向控制器发送一些内容,但 PageSize 始终等于 10 (默认) value),并且 Page 始终为 1。所以我想我必须将这些值硬编码到 $.ajax 中,但我不知道如何在客户端上获取 PageSize 和 Page 值?

I'm using grid that looks like that:

 Html.Telerik().Grid(Model).Name("preciousGrid").
     ... bla bla bla..
     .ClientEvents(events => events.OnDataBinding("onDataBinding"))
     .Columns(columns =>
         {
           columns.Bound(o => o.Date);
           columns.Bound(o => o.Name);

Yes, I'm totally ignoring .DataBinding stuff to use custom ajax call. Why? I need to send to the server more data, rather than a simple id. And the only way to gather that data is by traversing DOM elements. So, none of the methods suggested by Telerik wouldn't work in my case.

Everything works - in onDataBinding, after the necessary data gathered and sent to the server, server returns results, grid displays that data.

But still there is a problem. Paging doesn't work. And the footer shows something like that:

enter image description here

Any ideas?

UPD: Oh... maybe I should send to the server paging info and return results based on that? How to do that? Can you show me a sample?

UPD2: GridCommand command doesn't send Paging info to the server by default(if I omit it in $.ajax and still put a GridCommand parameter in the action method it would send something to the controller, but PageSize is always equals 10 (default value), and Page is always 1. So I guess I have to hardcodedly include these values in $.ajax. But I don't know how can I get PageSize and Page values on the client?

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

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

发布评论

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

评论(2

感悟人生的甜 2024-12-05 06:44:57

如果您正在进行自定义数据绑定,则必须自己处理分页和排序,如下所示。 演示网站上的示例非常合理......

[GridAction(EnableCustomBinding = true)]
public ActionResult _Index(GridCommand command)
{
    IEnumerable<MyObject> data = GetData(command.Page - 1, command.PageSize);
    int count = GetDataCount();
    return View(new GridModel { Data = data, Total = count });
}

在您看来:

.DataBinding(dataBinding => dataBinding.Ajax().Select("_Index", "Subjects"))
.Pageable(p =>
{
    p.PageSize(Model.PageSize, Model.PageSizes);
    p.Style(GridPagerStyles.PageInput | GridPagerStyles.NextPreviousAndDropDown);
})

If you're doing custom data binding, you must handle the paging and sorting yourself, something like that shown below. The example on the demo site is pretty reasonable...

[GridAction(EnableCustomBinding = true)]
public ActionResult _Index(GridCommand command)
{
    IEnumerable<MyObject> data = GetData(command.Page - 1, command.PageSize);
    int count = GetDataCount();
    return View(new GridModel { Data = data, Total = count });
}

in the view you have:

.DataBinding(dataBinding => dataBinding.Ajax().Select("_Index", "Subjects"))
.Pageable(p =>
{
    p.PageSize(Model.PageSize, Model.PageSizes);
    p.Style(GridPagerStyles.PageInput | GridPagerStyles.NextPreviousAndDropDown);
})
丘比特射中我 2024-12-05 06:44:57

好吧...显然你可以做真正的自定义ajax绑定

你必须定义数据绑定虽然它不会被网格使用,但你需要它才能使网格可分页。

 .DataBinding(binding => binding.Ajax().Select("GetList","Home")) // Although I guess you can put whatever here
                        .ClientEvents(events => events.OnDataBinding("Grid_onDataBinding"))

接下来,在 Grid_onDataBinding javascript 函数中,您必须执行类似的操作

var grid = $('#ConflictsGrid').data('tGrid');
$.ajax({ 
        url: "GetList",
        contentType: 'application/json; charset=utf-8',
        type: "GET",
        data: { 
               page: JSON.stringify({currentPage: grid.currentPage, pageSize: grid.pageSize }),
               // any other data you want to send to the server 
         },
         success: function (data) {
             grid.dataBind(data); // Here, the data that server returns will be actually bound to the grid
         },

Ok... apparently you can do truly custom ajax binding

You have to define databinding although it's not gonna be used by the grid, but you need it in order to be able to make the grid Pageable.

 .DataBinding(binding => binding.Ajax().Select("GetList","Home")) // Although I guess you can put whatever here
                        .ClientEvents(events => events.OnDataBinding("Grid_onDataBinding"))

Next, in the Grid_onDataBinding javascript function you have to do something like that

var grid = $('#ConflictsGrid').data('tGrid');
$.ajax({ 
        url: "GetList",
        contentType: 'application/json; charset=utf-8',
        type: "GET",
        data: { 
               page: JSON.stringify({currentPage: grid.currentPage, pageSize: grid.pageSize }),
               // any other data you want to send to the server 
         },
         success: function (data) {
             grid.dataBind(data); // Here, the data that server returns will be actually bound to the grid
         },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文