silverstripe - Silverstripe GridFieldAddExistingAutocompleter 中的过滤

标签 silverstripe

是否可以向 GridFieldAddExistingAutocompleter 添加类似于 TreeDropdownField 上的 setFilterFunction 的过滤器?

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 相同

最佳答案

有一个方法叫做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/

相关文章:

上传图像 SilverStripe 时出现 PHP 错误 'Filesize is zero bytes'

phpunit - 找不到 Silverstripe 4 SapphireTest 类

php - 如何生成自定义 CSV 导出?

SilverStripe ORM where 关系子句及其计数

css - silverstripe 在后端更改 optgroup 颜色

php - 为 SilverStripe 创建自定义 CSV 导入器

silverstripe - 我可以动态更改 SilverStripe 主题吗?

youtube - Silverstripe 3:覆盖TinyMCE样式

php - SilverStripe SMTP 身份验证代替 PHP mail() 函数

linux - Apache2 用户 (www-data) 对 www-data 拥有的文件夹的权限被拒绝