2013-11-28 49 views
2

是否有可能在GridFieldAddExistingAutocompleter上添加過濾器setFilterFunctionTreeDropdownFieldSilverstripe中的過濾GridFieldAddExistingAutocompleter

TreeDropdownField::create(
    "LinkID", 
    "Link to", 
    "SiteTree" 
)->setFilterFunction(create_function('$obj', 'return $obj->isPublished();')); 

這將使SiteTree對象時isPublished被稱爲是返回true列表。

有沒有辦法以類似的方式過濾GridField自動完成器?

在以下代碼$this->Pages()Pagehas_many關係。結果將僅列出自動填充下拉列表中的所有Page對象。

GridField::create(
    'Pages', 
    'Pages belonging to group', 
    $this->Pages(), 
    $config = GridFieldConfig::create()->addComponents(
     new GridFieldButtonRow('toolbar-header-right'), 
     new GridFieldToolbarHeader(), 
     new GridFieldSortableHeader(), 
     new GridFieldFilterHeader(), 
     new GridFieldDataColumns(), 
     new GridFieldDeleteAction(true), 
     new GridFieldDetailForm(), 
     new GridFieldAddExistingAutocompleter('toolbar-header-right') 
    ) 
) 

有沒有辦法可以過濾?

注:我想通過一些其他的檢查,不只是isPublished,我檢查,如果頁面的父ID是一樣的另一種關係的ID過濾

回答

2

有一個上GridFieldAddExistingAutocompleter稱爲setSearchList()方法使用它你可以設置要搜索的列表。

但是,我不知道這是完全實現,但你應該嘗試以下方法:

// lets assume you want all Pages that have no parent (root pages) 
$listToBeSearched = Page::get()->filter('ParentID', 0); 
$autoCompleteField = new GridFieldAddExistingAutocompleter('toolbar-header-right'); 
$autoCompleteField->setSearchList($listToBeSearched); 
$config = GridFieldConfig::create()->addComponents(
    new GridFieldButtonRow('toolbar-header-right'), 
    new GridFieldToolbarHeader(), 
    new GridFieldSortableHeader(), 
    new GridFieldFilterHeader(), 
    new GridFieldDataColumns(), 
    new GridFieldDeleteAction(true), 
    new GridFieldDetailForm(), 
    $autoCompleteField 
); 

,如果你想在你的問題就像一個功能來過濾,然後用filterByCallback()過濾列表:

// because GridFieldAddExistingAutocompleter only supports DataList at the moment 
// and filterByCallback() returns an ArrayList, we have to use a workaround 
// (which obviously yields a performance loss) 
$arrayList = Page::get()->filterByCallback(function($obj) { return $obj->isPublished(); }); 

// now that we have a list of pages that we want to search, 
// lets take the IDs of this list and create a DataList that filters for this IDs: 
$listToBeSearched = Page::get()->filter('ID', $arrayList->column('ID')); 
+0

正在嘗試這個,但是正在做'$ this-> Page()'而不是'Page :: get()'!明白了,謝謝! – danbroooks

+0

你可以粘貼完整的堆棧跟蹤(在某些粘貼網站上)? – Zauberfisch

+0

nvm,我知道這是爲什麼,問題是'filterByCallback'返回'ArrayList'。但'GridFieldAddExistingAutocompleter'僅適用於'DataList',因爲它使用了一些不受'ArrayList' – Zauberfisch