3

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

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

我有一个带有“区域”值的表(嵌套在简单的树上可以正常工作),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']
];

我有其他模型与以前的字段有关系,两个下拉字段在没有任何过滤器的情况下工作正常,在故障排除字段(模数)中我找不到基于“区域”下拉列表的值进行过滤的方法我有以下在 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

杰伦

4

2 回答 2

5

数据集作为第二个参数传递以获取“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');
}
于 2017-03-09T00:13:21.597 回答
1

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

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

这解决了我的问题。

谢谢大家。

于 2017-03-09T10:24:32.543 回答