MVC的Telerik格Ajax和手动绑定绑定、MVC、Telerik、Ajax

2023-09-10 19:20:42 作者:啃着面包去追梦

我有一个MVC的Telerik网格使用Ajax获取数据,我想控制什么时候会被加载。

I have a Telerik MVC Grid using ajax to get data and I want to control when it will be loaded.

下面是code。在我看来:

Here is the code in my view:

@(Html.Telerik().Grid<ViewModels.Reports.UserActionLoggingDetailViewModel>()
          .Name("UserActionLoggingFollowedGrid")
          .DataBinding(dataBinding => dataBinding.Ajax().Select("SelectUserActionLogging", "Report", new { userTeamId = Model.UserTeamId, startDate = Model.StartDate, endDate = Model.EndDate }).OperationMode(GridOperationMode.Client))
          .Columns(columns =>
                      {
                         columns.Bound(x => x.FullName).Hidden();
                         columns.Bound(x => x.ActionName);
                         columns.Bound(x => x.ActionCount);
                      })
          .Pageable(page => page.PageSize(20))
          .Sortable()
          .Groupable(grouping => grouping.Groups(groups => groups.Add(c => c.FullName)).Visible(false))
          .Filterable()
          .Localizable("fr-FR")
          .HtmlAttributes(new { @class = "grid-style static-grid-style" })
          .ClientEvents(e => e.OnError("Grid_onServerError").OnDataBinding("Grid_onDataBinding").OnDataBound("Grid_onDataBound"))
        )

默认情况下,code正常工作。当加载页面时,网格自动发送POST请求到服务器指定的操作并加载本身带有返回的DATAS。

By default, this code works correctly. When the page is loaded, the grid send automatically a post request to the server for the specified action and load itself with the returned datas.

我要的是同一个网格,相同的行为,但没有加载数据时,页面加载;我想网格加载,当用户点击一个按钮或任何其它操作。

What I want is the same grid with the same behavior but without loading data when the page is loaded; I want the grid to be loaded when the user click a button or any other actions.

我发现了一些有趣的职位说明如何手动刷新网格,但没有一个指定$如何为p $ pvent网格的初始绑定。

I found some interesting posts indicating how to manually refresh the grid but no one specified how to prevent the initial bind of the grid.

推荐答案

这小片对我的作品。添加数据绑定客户端事件(你已经有了,很明显):

This little piece works for me. Add a data binding client event (which you already have, apparently):

.ClientEvents(events => events
    .OnDataBinding("preventAjaxSelectOnPageLoad"))

然后创建添加此javascript:

Then create add this javascript:

<script type="text/javascript">

    var gridsToBind = [];

    function preventAjaxSelectOnPageLoad(e)
    {
        if ($.inArray(e.target.id, gridsToBind) == -1)
        {
            e.preventDefault();
            gridsToBind.push(e.target.id);
        }
    }
</script>

基本上,你创建一个数组来跟踪需要绑定网格。页面加载时,阵列将是空的 - 在这一点上,它取消了数据绑定事件并添加网格阵列。然后在下一次ajaxRequest()被调用,网格存在阵列中,并且可以正常地结合

Basically, you create an array to keep track of the grids that need to bind. When the page loads, the array will be empty - at that point, it cancels the data binding event and adds the grid to the array. Then the next time ajaxRequest() is called, the grid exists in the array and can be bound normally.