【问题标题】:OCTOBERCMS Dropdown options dependent on selected value on other dropdownOCTOBERCMS 下拉选项取决于其他下拉列表中的选定值
【发布时间】:2017-07-29 18:31:39
【问题描述】:

我被这个问题困住了,经过一段时间搜索示例后,我无法弄清楚如何解决。

这两个下拉选项是依赖于它们的值的表格。

我在 fields.yaml 文件中有一个具有以下结构的“区域”值表(嵌套简单的树工作正常):

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

我在 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

在“区域”模型中,我有:

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

在我的“模数”模型中

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

我有其他模型与以前的字段有关系,两个下拉字段在没有任何过滤器的情况下工作正常,在故障排除字段(模数)中我找不到基于“区域”下拉列表的值进行过滤的方法 I在 fields.yaml 中有以下内容。

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

area
  type: dropdown
  tab: Geral

在我定义了下拉列表的模型 PHP 文件中,我有:

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

这对我来说似乎是合乎逻辑的(也许不是),我也尝试过使用 DB 以及更多其他方法。我尝试使用dd() 来查看是否可以从第一个下拉列表中获取值,但无济于事。如果我尝试过滤这些值,则根本不会出现任何值(空值除外)。

有什么帮助吗???

TIA

JL

【问题讨论】:

  • Ty 进行编辑,对他们来说更清楚......不幸的是,我要正确发布代码是一场战斗:(。

标签: php dropdown octobercms octobercms-backend


【解决方案1】:

数据集作为第二个参数传递以获取“getOptions”方法。以下是一种可行的替代方法:

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

您可能还想尝试访问area_id 值:

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

area->id 值或效率较低(可能需要异常处理):

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

【讨论】:

  • 谢谢塞缪尔...我在其中两个片段之前尝试过,除了带参数的那个...也尝试过那个但没有用...当然我错过了一些非常简单的东西.我尝试对 $this->area_id 和其他变量进行转储,但是当我更改第一个下拉列表的值时没有得到任何值。
  • 尝试使用 Debugbar 插件转储或记录 $data 值。 $data 属性将包含所有字段。我用作`$data['area']
  • 还需要在yaml文件中定义dependsOn属性
【解决方案2】:

我通过以下步骤解决了该下拉列表和其他具有相同目标的问题:

  • 我上面有“关系”小部件。将它们更改为“下拉”小部件。
  • 定义了“取决于”字段。
  • 将“预设”字段定义为上述字段。我认为这是没有在任何地方记录的问题解决方案的缺失链接,我在尝试/错误的基础上到达那里(可能将其添加到 10 月份的文档中很有价值)。
  • 使用我的问题末尾的代码段或 Samuel 答案的第二个代码段过滤选项。

这解决了我的问题。

谢谢大家。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 2014-07-26
相关资源
最近更新 更多