【问题标题】:SelectionGroup not adding extra attributesSelectionGroup 不添加额外的属性
【发布时间】:2016-08-10 01:37:15
【问题描述】:

我有 PHP 代码:

SelectionGroup::create(
//...
SelectionGroup_Item::create(/*...*/),
SelectionGroup_Item::create(/*...*/),
//...
)
->addExtraClass("some-extra-class")
->setAttribute('ng-change','log(myModel)')
->setAttribute('ng-model','myModel')

呈现的 html 看起来像:

...
<ul class="SelectionGroup field CompositeField selectiongroup some-extra-class nolabel">
    ...
</ul>
...

我的额外类被添加了,为什么我的额外属性没有被添加?


SelectionGroupFieldList 的一部分,其他Fields 允许设置属性,SelectionGroup_Item 中的Fields 可以设置其属性,例如:

FieldList::create([
  HiddenField::create(...)->setAttribute("does","this work"),
  SelectionGroup::create(
    //...
    SelectionGroup_Item::create('name',
      FieldGroup::create(null,[
        HiddenField::create(...)->setAttribute("maybe","it does")
      ])
    ),
    SelectionGroup_Item::create(...),
    //...
  )
  ->addExtraClass("some-extra-class")
  ->setAttribute('ng-change','log(myModel)')
  ->setAttribute('ng-model','myModel')
])

呈现以下 HTML:

...
<input type="hidden" ... does="this work" />
<ul class="SelectionGroup field CompositeField selectiongroup some-extra-class nolabel">
  ...
  <input type="hidden" ... maybe="it does" />
  ...
</ul>
...

【问题讨论】:

  • 你是哪个版本的SS?
  • @RobbieAverill 3.4(稳定)

标签: php silverstripe


【解决方案1】:

在 SilverStripe 3.4 中,$AttributesHTML 变量不会在 SelectionGroup_Item 使用的默认模板中调用。

SelectionGroup_Item 使用CompositeField 模板(因为它是extends CompositeField,并且它没有在框架中设置自己的模板)。

框架中当前的CompositeField模板在开始标签中不包含$AttributesHTML

<$Tag class="CompositeField $extraClass <% if ColumnCount %>multicolumn<% end_if %>">
    <% if $Tag == 'fieldset' && $Legend %>
        <legend>$Legend</legend>
    <% end_if %>

    <% loop $FieldList %>
        <% if $ColumnCount %>
            <div class="column-{$ColumnCount} $FirstLast">
                $Field
            </div>
        <% else %>
            $Field
        <% end_if %>
    <% end_loop %>
</$Tag>

我们可以创建自己的SelectionGroup_Item 模板或CompositeField 来添加$AttributesHTML 变量。

为此,我们在mysite/templates/includes 目录中创建一个SelectionGroup_Item.ss 文件。

mysite/templates/includes/SelectionGroup_Item.ss

<$Tag $AttributesHTML class="CompositeField $extraClass <% if ColumnCount %>multicolumn<% end_if %>">
    <% if $Tag == 'fieldset' && $Legend %>
        <legend>$Legend</legend>
    <% end_if %>

    <% loop $FieldList %>
        <% if $ColumnCount %>
            <div class="column-{$ColumnCount} $FirstLast">
                $Field
            </div>
        <% else %>
            $Field
        <% end_if %>
    <% end_loop %>
</$Tag>

创建此模板后,我们需要在页面 URL 中调用 ?flush=all 以便系统清除其缓存并找到此新模板。

【讨论】:

  • 那太可惜了,我以为这会默认实现......我想很难在任何地方实现所有东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-13
  • 2021-06-19
  • 2015-06-13
  • 1970-01-01
  • 2019-04-18
  • 2020-12-08
  • 2014-02-14
相关资源
最近更新 更多