2011-11-28 77 views
5

我對我的jqGrid網格使用了一個過濾器,並且數據在組中,默認情況下處於摺疊狀態。如果用戶打開一個組或2個(展開組),然後執行過濾器,網格重新加載數據,正確過濾數據,但是然後我失去了用戶打開的組的擴展狀態。有沒有辦法沒有它,當做一個過濾器切換回摺疊的默認狀態?jqGrid過濾分組狀態

在此先感謝。

回答

2

我覺得你的問題很有趣。所以,從我+1。我做了一個演示,演示如何實現您的需求。

實現的主要思想與the answer中的一樣。我建議只保持數組expandedGroups中擴展組的狀態。我使用jqGrid 4.0.0中添加的onClickGroup回調函數(請參閱here)。在loadComplete回調內部,我嘗試展開數組expandedGroups中的所有項目。

該實現的優點是在分頁,排序和過濾期間擴展狀態不會消失。

演示你可以看到here。下面從演示代碼:

var $grid = $("#list"), expandedGroups = []; 

$grid.jqGrid({ 
    // ... some jqGrid parameters 
    grouping: true, 
    groupingView: { 
     groupField: ['name'], 
     groupCollapse: true, 
     groupDataSorted: true 
    }, 
    onClickGroup: function (hid, collapsed) { 
     var idPrefix = this.id + "ghead_", id, groupItem, i; 
     if (hid.length > idPrefix.length && hid.substr(0, idPrefix.length) === idPrefix) { 
      id = hid.substr(idPrefix.length); 
      groupItem = this.p.groupingView.sortnames[0][id]; 
      if (typeof (groupItem) !== "undefined") { 
       i = $.inArray(expandedGroups[i], groups); 
       if (!collapsed && i < 0) { 
        expandedGroups.push(groupItem); 
       } else if (collapsed && i >= 0) { 
        expandedGroups.splice(i, 1); // remove groupItem from the list 
       } 
      } 
     } 
    }, 
    loadComplete: function() { 
     var $this = $(this), i, l, index, groups = this.p.groupingView.sortnames[0]; 
     for (i = 0, l = expandedGroups.length; i < l; i++) { 
      index = groups.indexOf(expandedGroups[i]); 
      if (i >= 0) { 
       $this.jqGrid('groupingToggle', this.id + 'ghead_' + index); 
      } 
     } 
    } 
}); 
$grid.jqGrid('navGrid', '#pager', {add: false, edit: false, del: false}, {}, {}, {}, 
    {multipleSearch: true, multipleGroup: true, closeOnEscape: true, showQuery: true, 
     closeAfterSearch: true}); 

修訂:jqGrid的的分組服務器在許多地方,因爲我原來的答覆變化。修改後的演示可以找到here。使用的代碼中最重要的部分可以在下面看到

grouping: true, 
groupingView: { 
    groupField: ["invdate"], 
    groupCollapse: true, 
    groupDataSorted: true 
}, 
onClickGroup: function (hid, collapsed) { 
    var idPrefix = this.id + "ghead_", i, groupid, 
     $this = $(this), 
     groups = $(this).jqGrid("getGridParam", "groupingView").groups, 
     l = groups.length; 

    if (!inOnClickGroup) { 
     inOnClickGroup = true; // set to skip recursion 
     for (i = 0; i < l; i++) { 
      groupid = idPrefix + groups[i].idx + "_" + i; 
      if (groupid !== hid) { 
       $this.jqGrid("groupingToggle", groupid); 
      } 
     } 
     inOnClickGroup = false; 
    } 
} 

變量inOnClickGroup在外部範圍內定義。

+0

非常感謝你,這將花費一點時間來工作,但我的經驗是有限的這個核心。非常直接,但是當每個人都利用這些特色時,它會幫助社區。謝謝你節省我的時間,我希望以同樣的方式幫助別人。 –

+0

@ D-S:不客氣! – Oleg

+0

偉大的問題和偉大的答案! – FastTrack