JLongo
JLongo

Reputation: 49

OCTOBERCMS Dropdown options dependent on selected value on other dropdown

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

I also have another table 'modulos' values with the following structure in 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

In the 'Area' model I have:

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

In the 'Modulos' model 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 troubleshoting field (modulos) where I can't find a way to filter based on the values of 'Area' dropdown I have the following in fields.yaml.

....
modulo_id:
  label: mod
  oc.commentPosition: ''
  emptyOption: 'Sem valor'
  span: auto
  required: 1
  dependsOn:

area
  type: dropdown
  tab: Geral

In my models PHP file where I have the dropdowns defined, 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 tried with dd() to see if I can get the values from the first dropdown to no avail. If I try to filter the values, no value appears at all (except an empty value).

Any help out there ???

TIA

JL

Upvotes: 3

Views: 3993

Answers (2)

JLongo
JLongo

Reputation: 49

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 or the second snipet of Samuel answer.

That solved my problem.

Thanks all.

Upvotes: 1

Samuel Georges
Samuel Georges

Reputation: 973

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');
}

Upvotes: 5

Related Questions