Laravel nova-基于其他字段的自定义字段设置值,并且仍可编辑

时间:2019-07-16 09:14:42

标签: laravel vue.js laravel-nova

laravel 5.8 新星2.0.0

我有两个基于第一个字段的字段,我想设置第二个字段的值,取决于用户使用设置的值,还是可以插入一个新字段并提交表单。

我要添加自定义字段,例如

CustomField::make('Field 2', 'field_2')->dependsOn('field_1'),

现在在CustomFielddependsOn()方法中,我也可以在Vue> FormField中访问dependsOn字段名称,即field_1。vue我可以以{{1} }在模板中,在JS部分中是{{ field.dependsOnField }},但是如何获得console.log(this.field.dependsOnField);的值,以及如何在更改时获得新的值,我是Vue JS的新手,但是我尝试了< / p>

field_1

export default { mounted() { this.registerDependencyWatchers(this.$root) }, methods: { registerDependencyWatchers(root) { //console.log('working 0'); console.log(this.field.dependsOnField); root.$children.forEach(component => { if (this.componentIsDependency(component)) { // console.log('working 1'); console.log(component.$attrs); component.$watch('value', async (value) => { console.log('working 2'); // this.dependencyValues[component.field.attribute] = value; // this.supplier = value; // var attr = (await Nova.request().post("/custom-field/nova-component/custom-field/supplier", { // dependKey: component.field.attribute, // dependValue: value // })).data; // this.supplier = attr.supplier; this.registerDependencyWatchers(component) }); } }); }, componentIsDependency(component) { console.log(component); console.log(component.$attrs); console.log(component.field); if(component.field === undefined) { return false; } for (let dependency of this.field.dependsOnField) { console.log(dependency); if(component.field.attribute === dependency.field) { return true; } } return false; }, } } `componentIsDependency` is always returning false as `component.field` is undefined and if I am returing true there `component.$watch('value', async (value) => {` is not running, I coded it by taking reference from [NovaDependencyConatiner][1] package. 类似于

Console.log(component)

1 个答案:

答案 0 :(得分:0)

我在 VueComponent 中创建了一个方法:

findFieldByAttribute (attribute) {
    return this.$parent.$children.find(brotherField =>
        brotherField.field.attribute === attribute,
    );
}

用法:

const field1 = this.findFieldByAttribute('field_1');
const field1Value = field1.value;