是否可以向 GridFieldAddExistingAutocompleter
添加类似于 TreeDropdownField
上的 setFilterFunction
的过滤器?
TreeDropdownField::create(
"LinkID",
"Link to",
"SiteTree"
)->setFilterFunction(create_function( '$obj', 'return $obj->isPublished();' ));
这将给出一个 SiteTree
对象列表,在调用 isPublished
时返回 true。
有没有办法以类似的方式过滤 GridField 自动完成器?
在下面的代码中,$this->Pages()
是 Page
的 has_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 相同
最佳答案
有一个方法叫做setSearchList()
上GridFieldAddExistingAutocompleter
您可以使用它来设置要搜索的列表。
但是,我不确定这是否已完全实现,但您应该尝试以下操作:
// 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'));
关于silverstripe - Silverstripe GridFieldAddExistingAutocompleter 中的过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20268308/