This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

jlongo
jlongo

Hi all

I'm stuck with this problem and i can't figure out how to solve after some time searching for a example.

The two dropdowns options are table dependent on their values.

I have the one table with 'area' values (nested with simple tree working ok) with the following structure on fields.yaml file:

  fields:
id:
    label: Número
    oc.commentPosition: ''
    span: auto
    disabled: 1
    type: number
area_id:
    label: 'Parente de'
    oc.commentPosition: ''
    emptyOption: 'Sem valor'
    span: auto
    type: dropdown
area:
    label: Área
    oc.commentPosition: ''
    span: full
    required: 1
    type: text

Also i have other table 'modulos' values with the following structure on fields.yaml:

   fields:
modulo:
    label: Módulo
    oc.commentPosition: ''
    span: auto
    required: 1
    type: text
area:
    label: Área
    oc.commentPosition: ''
    nameFrom: area
    emptyOption: 'Sem valor'
    span: auto
    descriptionFrom: id
    type: relation

On model 'Area' i have

 ... 
 public $hasMany = [
    'modulos' =>['JML\Gkb\Models\Modulos']
];

On model 'Modulos' i have

 ....
 public $belongsTo = [
    'area' => '\JML\Gkb\Models\Area'
];

I have other model that have relations with previous fields and two dropdown fields working ok without any filter, and the trobleshoting field (modulos) where i can't find a way to filter based on the values of 'Area' dropdown i have the following on fields.yaml

....
modulo_id:
        label: mod
        oc.commentPosition: ''
        emptyOption: 'Sem valor'
        span: auto
        required: 1
        dependsOn:
            - area
        type: dropdown
        tab: Geral

On models php file where i have the dropdowns i have:

public function getModuloIdOptions(){
    return Modulos::where('area_id', '=', $this->area)->lists('modulo', 'id');
}

that to me seems logical (maybe not) and i tried with DB also and many more other. I tryed with dump() to see if i can get the values from first dropdown with no avail...

Any help out there ???

TIA

JL

Last updated

jlongo
jlongo

I solved the problem with that dropdown and others with the same objective with the following steps:

  • I have the the 'Relation' widget on them. Changed them to 'Dropdown' widget.
  • Defined the 'Depends on' field.
  • Defined the 'Preset' field to the one above. I think that is the missing link to the problem solution not documented anywhere and i get there on try/error basis (may be it is valuable to add this to the October documentation).
    • Filter options with the snipet code on the end of my question . That solved my problem.

Thanks all.

JL

hasil
hasil

The dataset is passed as the second argument to get the "getOptions" method. Here is an alternative approach that may work:

public function getModuloIdOptions($value, $data) {
    return Modulos::where('area_id', '=', array_get($data, 'area'))->lists('modulo', 'id');
}

You may also want to try accessing the area_id value:

public function getModuloIdOptions(){
    return Modulos::where('area_id', '=', $this->area_id)->lists('modulo', 'id');
}

Or less efficiently the area->id value (may require exception handling):

public function getModuloIdOptions(){
    return Modulos::where('area_id', '=', $this->area->id)->lists('modulo', 'id');
}

Reference: https://stackoverflow.com/questions/42684057/octobercms-dropdown-options-dependent-on-selected-value-on-other-dropdown

Last updated

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.