2012-06-21 200 views
2

我正在使用gxt的LiveGrid。我想添加filters。我在下面添加了幾行,但數據未被過濾。我在這裏錯過任何東西嗎?添加過濾器到LiveGrid?

GridFilters filters = new GridFilters(); 
filters.setLocal(true); 

StringFilter nameFilter = new StringFilter("column name"); 
filters.addFilter(nameFilter); 

filters.init(liveGrid); 
liveGrid.addPlugin(filters); 

回答

2

filter的javadoc:

要將過濾器添加到網格列,創建過濾器的具體子類的實例,傳遞給構造函數的ValueProvider爲列,然後添加過濾到GridFilters

您的代碼示例似乎過於重複,應該可能參數化。應該給StringFilter一個ValueProvider,用於你想要過濾的模型對象的屬性。以下是如何使用過濾器創建網格的簡單概述。

假設你有一個類用戶

public class User implements Serializable { 
    private String name; 
    private Integer id; 

    // Setters and getters 
} 

public interface UserProperties extends PropertyAccess<User> { 
    @Path("id") 
    ModelKeyProvider<User> key(); 

    ValueProvider<User, String> name(); 
} 

要創建一個網格,將顯示你的用戶,你會做如下

private static final UserProperties props = GWT.create(UserProperties.class); 
... 

// Create column config 
ColumnConfig<User, String> nameCol = new ColumnConfig<User, String>(props.name(), 200, "Name"); 

// Create column model 
List<ColumnConfig<User, ?>> l = new ArrayList<ColumnConfig<User, ?>>(); 
l.add(nameCol); 
ColumnModel<User> cm = new ColumnModel<User>(l); 

// Create User store 
ListStore<User> store = new ListStore<User>(props.key()); 

// Create your grid 
final LiveGridView<User> liveGridView = new LiveGridView<User>(); 
liveGridView.setForceFit(true); 

Grid<User> view = new Grid<User>(store, cm) { 
    @Override 
    protected void onAfterFirstAttach() { 
    super.onAfterFirstAttach(); 
    // Get grid data 
    } 
}; 

// Create a String filter for the column 
StringFilter<User> nameFilter = new StringFilter<User>(props.name()); 

// Create a GridFilters 
GridFilters<User> filters = new GridFilters<User>(); 
filters.initPlugin(grid); 
filters.setLocal(true); 
filters.addFilter(nameFilter); 
+0

好吧。 +1 :) –

+0

嗨感謝您的詳細回覆。我的代碼在正常的網格中運行良好。如果我嘗試使用活動網格,那麼它不是過濾。您的代碼是否適用於實時網格?謝謝! – user1016403

0

GridFilters過濾器=新GridFilters(); filters.setLocal(true);

StringFilter nameFilter = new StringFilter(「column name」); filters.addFilter(nameFilter);

filters.init(liveGrid); liveGrid.addPlugin(filters);

如果你想獲得過濾器應用後的數據,你需要覆蓋AbstractGridFilters的reload()方法。