patricknelson / silverstripe-gridfieldlimititems
这是一个简单的组件,可以自动限制在GridField中显示的项目最大数量(包括修改实际关系)。
0.2.1
2016-12-17 12:16 UTC
Requires
- silverstripe/cms: ^3.1
- silverstripe/framework: ^3.1
This package is auto-updated.
Last update: 2024-09-23 09:18:24 UTC
README
这是一个简单的组件,可以自动限制在GridField中显示的项目最大数量(包括修改实际关系)。
重要:这作为一个集中化的方法来维护实际的硬限制数量,对has_many
和many_many
关系非常有用。因此,这将修改这些关系,并且(目前)不会简单地限制在网格字段中显示的项目数量。
安装
- 运行
composer require patricknelson/silverstripe-gridfieldlimititems
- 运行
sake dev/build
示例用法
快速开始
使用GridFieldConfig_LimitedRelationEditor
开始管理关系,如下所示
// Setup a new relation editor with an upper hard limit of 10 items. Items past this amount will be automatically // removed by GridFieldLimitItems (setup in this relation editor). $gridConfig = GridFieldConfig_LimitedRelationEditor::create(10); $gridField = new GridField('RelationName', 'Relation Title', $this->MyRelation()->sort('Sort'), $gridConfig); $fields->addFieldToTab('Root.main', $gridField);
您还可以设置额外的配置选项(大多数选项都已包括)
// Setup a brand new limiter... $limiter = new GridFieldLimitItems(10); // Limit to max of 10. $gridConfig->addComponent($limiter); // ... or get from an existing configuration. $limiter = $gridConfig->getComponentByType('GridFieldLimitItems'); // Setup the limiter with a few extra options. $limiter->setNoteBelow(); // Ensure note displays below grid field instead of on top (default). $limiter->setRemoveFromTop(true); // Removes excess items from the top of the list instead of the bottom (default). // You can even setup a callbacks to prevent any manipulation from taking place under certain circumstances. $limiter->onBeforeManipulate(function(GridField $grid, SS_List $list) use($disableLimiting) { // Will prevent manipulation if you return false from this closure, otherwise operates as normal. if ($disableLimiting) return false; }); // Or do something after list manipulation takes place, like so... $limiter->onBeforeManipulate(function(GridField $grid, SS_List $list) { SS_Log::log(print_r($list->map()), SS_Log::DEBUG); });
警告:由于这将作为一个组件修改关系,最好确保该关系本身没有被其他组件(如分页器)修改。如果您正在使用标准的GridFieldConfig_RelationEditor
,您需要移除该组件。例如
// Setup a new relation editor. $gridConfig = GridFieldConfig_RelationEditor::create(); // Since GridFieldLimitItems is not yet compatible with GridFieldPaginator, remove that now. $gridConfig->removeComponentsByType("GridFieldPaginator"); $gridConfig->removeComponentsByType("GridFieldPageCount"); // Now we can add our GridFieldLimitItems component. $limiter = new GridFieldLimitItems(10); // Limit to max of 10. $gridConfig->addComponent($limiter); // ... continue below with adding your new GridField instance with this $gridConfig...
已知问题
- 这可能会因为运行顺序不当而出现与问题有关的问题,例如在排序字段且新排序的字段尚未正确初始化的情况下(例如,从0开始但应在修改之前设置为11)。在这种情况下,您应该确保将您的排序组件添加到网格配置中,在组件之前,以便首先处理排序,然后由本组件执行修剪。这仅在使用内置的
GridFieldConfig_RelationEditor
时才会成为问题,它已经确保了它将在最后处理关系。
待办事项
- 设置自定义注释的能力。
- 设置修改返回列表的能力,而不实际影响关系(即只读,完全不写入数据库)。
- 编写单元测试以覆盖当前功能。