我有一个 Telerik MVC 网格,使用 ajax 来获取数据,我想控制何时加载数据。
这是我认为的代码:
@(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"))
)
默认情况下,此代码可以正常工作。加载页面时,网格会自动向服务器发送一个 post 请求以执行指定的操作,并使用返回的数据加载自身。
我想要的是具有相同行为的相同网格,但在加载页面时不加载数据;我希望当用户单击按钮或任何其他操作时加载网格。
我发现一些有趣的帖子指示如何手动刷新网格,但没有人指定如何防止网格的初始绑定。
这个小作品对我有用。添加数据绑定客户端事件(显然您已经拥有):
.ClientEvents(events => events
.OnDataBinding("preventAjaxSelectOnPageLoad"))
然后创建添加此 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()时,该网格就存在于数组中,可以正常绑定。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)