【问题标题】:Show only parent_id names on octobercms backend form在 octobercms 后端表单上仅显示 parent_id 名称
【发布时间】:2018-07-13 09:18:35
【问题描述】:

我使用嵌套树的位置并使用“父”字段来选择父位置(我使用重新排序记录创建)。但我需要“父”字段来显示只有父母,只有 parent_id 等于 NULL 的位置。

关于位置模型 fields.yaml 我有:

 fields:
    name:
        label: Name
        oc.commentPosition: ''
        span: left
        required: 1
        type: text
    slug:
        label: Slug
        oc.commentPosition: ''
        span: right
        required: 1
        preset:
            field: name
            type: slug
        type: text
    parent:
        label: Parent Location
        oc.commentPosition: ''
        nameFrom: name
        descriptionFrom: description
        span: left
        type: relation
        placeholder: 'select parent location'
    description:
        label: Description
        size: large
        oc.commentPosition: ''
        span: right
        type: textarea

但现在我得到了后端字段“父”上的所有位置。有没有办法只获取父位置?

表架构:

参赛作品:

            $table->engine = 'InnoDB';
            $table->increments('id')->unsigned();
            $table->integer('user_id')->nullable()->unsigned();
            $table->string('name')->nullable();
            $table->string('slug');
            $table->text('content')->nullable();
            $table->integer('location_id')->nullable();
            $table->timestamp('published_at')->nullable();
            $table->boolean('published')->default(0);
            $table->timestamp('created_at')->nullable();
            $table->timestamp('updated_at')->nullable();

地点:

        $table->engine = 'InnoDB';
        $table->increments('id')->unsigned();
        $table->string('name')->nullable();
        $table->string('slug')->nullable();
        $table->text('description')->nullable();
        $table->integer('parent_id')->nullable()->unsigned();
        $table->integer('nest_left')->nullable();
        $table->integer('nest_right')->nullable();
        $table->integer('nest_depth')->nullable();
        $table->timestamp('created_at')->nullable();
        $table->timestamp('updated_at')->nullable();

【问题讨论】:

  • 表示您想显示drop-down 的记录,其parent_id IS NULL 并且不关心关系数据意味着它是一对一关系对吗?
  • 在入口模型上我使用belongsTo,在位置模型上我使用hasMany

标签: octobercms octobercms-plugins octobercms-backend


【解决方案1】:

嗯,我不确定我们是否可以通过 relation widget 传递 条件 并生成 filtered list from database,而不是我建议手动使用 drop-down 方式来获得更多控制。

为此,请将configuration 中的关系字段parent 更改为parent_id(因为这将是您的数据库字段)

新配置如下所示

parent_id:
    label: Parent Location
    oc.commentPosition: ''
    span: left
    type: dropdown
    placeholder: 'select parent location'

现在在您的位置模型中,您需要提供源代码

public function getParentIdOptions(){
    return \Locations::->whereNull('parent_id')->lists('name', 'id');
}

这将返回 array of location (id, name key => value pair) 其中parent_id IS NULL 或您基于您的自定义条件。

如果您遇到任何问题,请尝试此方法,请发表评论。

【讨论】:

  • 我尝试了下拉方法,但结果很奇怪。当我尝试创建一个位置并选择 parent_id 字段时,我得到了正确的 parent_id 名称,但是当我保存位置并重新打开它时,下拉列表中没有选择 parent_id。至少没有显示出来。
  • 如果可能的话,您能否为两者共享您的表架构
  • 哦,我明白了,因为下次您访问记录时,可能是该记录的 parent_id IS NULL 条件可能是下拉,你能检查一下并确认我,以便我考虑解决方案
  • 对于下拉菜单,如果活动项目不存在,那么如果占位符不存在,它将首先显示大多数项目,我们认为在这种情况下它的选定值,我猜
  • 好吧,问题是我使用了return Location::whereNull('parent_id')->lists('name'); 而不是return Location::whereNull('parent_id')->lists('name', 'id');。现在一切正常,但是当我打开父位置时,我收到占位符消息,没关系,但我偷可以选择父母。正确的方法应该是在 parent_id 字段上获得零结果。
猜你喜欢
  • 2020-10-28
  • 2021-01-29
  • 2016-07-29
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多