简体   繁体   中英

TYPO3 | Extbase / Fluid - How can I fill <f:form.select.optgroup> from a domain object / database?

Hi I'm struggling with the <f:form.select> I need to populate the values with option groups but according to the manual all I can find is how to manually create optgroup's.

Does anybody know a way to fill the select with values from the object / database?

This select works fine but doesn't create option groups obviously
<f:form.select options="{positionen}" optionLabelField="title" multiple="1" id="position" property="position" size="10" class="form-control select2-hs" />

Now how could I modify the view so that it'll be like this:

<select class="form-control select2-multiple select2-multiple-max3" name="position[]">
<optgroup label="Geschäftsführung / Management">
 <option value="1">Geschäftsführer KMU (Bereich Handel / Dienstleistung)</option>
<option value="2">Geschäftsführer Großunternehmen (Bereich Handel / Dienstleistung)</option>
<option value="3">Geschäftsführer KMU (Bereich Produktion)</option>
</optgroup>
<optgroup label="Steuern / Revision / Treuhand">
... 

The content of the object {positionen} looks like this: (Screenshot of fluid debug output)

You will need your own logic for grouping, either by preparing groups in your controller or using <f:groupedFor> :

<f:select property="position">
  <f:groupedFor
    each="{positions}"
    as="areaPositions"
    groupBy="area"
    groupKey="area"
  >

    <f:form.select.optgroup label="{area.title}">

      <f:for each="{areaPositions}" as="position">

        <f:form.select.option value="{position.uid}">
          {position.title}
        </f:form.select.option>

      </f:for>

    </f:form.select.optgroup>

  </f:groupedFor>
</f:select>

Here it is assumed that you have loaded all possible positions (eg from a PositionRepository ) and passed it to the template. Each position should have a related area (1:1) and each area should have a title . This way you can group positions by their area and use the area title as option group label.

Afterwards all positions for each area are rendered as select options assuming that each position has a title .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM