2012-07-27 68 views
4

我有一個Telerik MVC Grid使用ajax來獲取數據,我想控制它何時加載。手動綁定Telerik MVC Grid 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")) 
     ) 

默認情況下,該代碼工作正常。加載頁面後,網格會自動發送一個發送請求到服務器以執行指定的操作,並使用返回的數據加載它自己。

我想要的是具有相同行爲但加載頁面時不加載數據的同一個網格;我想要在用戶單擊按鈕或任何其他操作時加載網格。

我發現了一些有趣的帖子,指出如何手動刷新網格,但沒有人指定如何防止網格的初始綁定。

回答

0

我認爲你可以爲網格的DataBinding或DataBound事件添加一個處理程序,並檢查它是否是頁面的第一次加載。如果是這樣,請使用e.preventDefault()以避免調用服務器。

其他選項可能是刪除了ajax從電網聲明綁定,並使用dataBind method

5

這小片適用於我自己來做。添加數據綁定客戶端事件(你已經有了,顯然):

.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()時,網格存在於數組中,並且可以正常綁定。