美文网首页
vue+ts循环条件渲染的子组件 如何通过父组件调用子组件函数

vue+ts循环条件渲染的子组件 如何通过父组件调用子组件函数

作者: 阡陌昏晨 | 来源:发表于2023-07-12 10:08 被阅读0次
<template>
    <div class="rule-form">
        <a-row class="rule-form-item">
            <a-col :span="2" class="rule-form__center rule-form__header"
                >选择</a-col
            >
            <a-col :span="4" class="rule-form__center rule-form__header"
                >规则代码</a-col
            >
            <a-col :span="4" class="rule-form__center rule-form__header"
                >名称</a-col
            >
            <a-col :span="14" class="rule-form__center rule-form__header"
                >条件</a-col
            >
        </a-row>
        <!-- <div v-for="(condition, index) in conditions" :key="index"> -->
        <!-- <div class="rule-form-item rule-form__center rule-form__header" v-if="config.name">{{ config.name }}</div> -->
        <div>
            <a-row
                v-for="field in conditions"
                :key="field.code"
                class="rule-form-item"
                type="flex"
                align="middle"
            >
                <a-col :span="2" class="rule-form__center">
                    <a-checkbox
                        :checked="field.select"
                        @change="toggleCondition(field)"
                        :disabled="disabled"
                    ></a-checkbox>
                </a-col>
                <a-col :span="2" class="rule-form__center">
                    {{ field.code }}
                </a-col>
                <a-col :span="4" class="rule-form__center">
                    {{ field.name }}
                </a-col>
                <a-col :span="16">
                    <template v-if="field.factorType == 'Dictionary'">
                        <!-- {{field.options}} -->
                        <!-- <a-checkbox-group
                            :options="field.options"
                            v-model="fields[field.code]"
                            :disabled="disabled || !field.select"
                        ></a-checkbox-group> -->
                        <dictionary-select
                            :value="fields[field.code]"
                            :operatorId="field.operatorId"
                            :type="field.factorType"
                            :disabled="disabled || !field.select"
                            :optionsList="field.options"
                            @textChange="textChange(field, $event)"
                            @selectChange="selectChange(field, $event)"
                        ></dictionary-select>
                    </template>
                    <template
                        v-else-if="
                            field.factorType == 'String' ||
                                field.factorType == 'Number'
                        "
                    >
                        <text-field
                            :value="fields[field.code]"
                            :operatorId="field.operatorId"
                            :type="field.factorType"
                            :disabled="disabled || !field.select"
                            @textChange="textChange(field, $event)"
                        ></text-field>
                    </template>
                    <!-- <template v-else-if="field.factorType == 'Number'"> -->
                    <!-- <range-field
                            v-model="fields[field.code]"
                            :disabled="disabled || !field.select"
                        ></range-field> -->
                    <!-- </template> -->
                    <template v-else-if="field.factorType == 'AreaCode'">
                        <area-select
                            ref="areaSelect"
                            v-model="fields[field.code]"
                            :disabled="disabled || !field.select"
                        ></area-select>
                    </template>
                    <template v-else-if="field.factorType == 'CustomAreaCode'">
                        <custom-select
                            ref="customSelect"
                            v-model="fields[field.code]"
                            :disabled="disabled || !field.select"
                        ></custom-select>
                    </template>
                </a-col>
            </a-row>
        </div>
    </div>
</template>

<script lang="ts">
import { Vue, Component, Prop, Watch, Emit } from 'vue-property-decorator';

import factorUtil from '@/utils/mapper/factors';

import AreaSelect from './components/AreaSelect.vue';
import CustomSelect from './components/CustomSelect.vue';
import TextField from './components/Text.vue';
import RangeField from './components/RangeField.vue';
import DictionarySelect from './components/DictionarySelect.vue';

@Component({
    components: {
        TextField,
        RangeField,
        AreaSelect,
        CustomSelect,
        DictionarySelect,
    },
})
export default class extends Vue {
    @Prop(Array) private readonly conditions!: any[];
    @Prop({ default: false }) private readonly disabled!: boolean;
    private fields: any = {};

    // private created() {
    //     this.onConditionChange(this.conditions, []);
    // }

    private mounted() {
        console.log('customSelect ruleForm mounted');
    }

    openShow() {
        this.$nextTick(() => {
            console.log('ruleForm openShow');
            const operatorObj = this.$store.state.common.operatorObj;
            this.conditions.forEach(condition => {
                if (
                    !condition.operatorId &&
                    operatorObj[condition.factorType]
                ) {
                    const operators =
                        operatorObj[condition.factorType].operatorList;

                    if (operators.length === 1) {
                        condition.operatorId = operators[0].id;
                    } else {
                        condition.operatorId = '';
                    }
                }

                const factorInfo = factorUtil.getFactorInfo(
                    condition.operatorId
                );
                const component = (factorInfo as any).component;
                let value: any = condition.value;

                if (
                    ['Dictionary', 'CustomAreaCode', 'AreaCode'].includes(
                        condition.factorType
                    )
                ) {
                    value =
                        (condition.value && condition.value.split(',')) || [];
                }
                if ('CustomAreaCode' === condition.factorType) {
                    this.showCustomSelectChild();
                }
                // if ('AreaCode' === condition.factorType) {
                //     this.showAreaSelectChild();
                // }

                this.$set(this.fields, condition.code, value);
            });
        });
    }

    private showCustomSelectChild() {
        const custSelect = this.$refs.customSelect[0] as CustomSelect;
        console.log('customSelect showChild', custSelect);
        custSelect.showOpen();
    }

    private toggleCondition(field: any) {
        field.select = !field.select;
    }

    private textChange(field: { code: string; operatorId: string }, data: any) {
        this.fields[field.code] = data.value;
        field.operatorId = data.operatorId;
    }
    private selectChange(field: any, data: any) {
        console.log(field, 'field');
        console.log(data, 'data');
        this.fields[field.code] = data.value;
    }
}
</script>

以上是全部代码
下面是核心
private showCustomSelectChild() {
const custSelect = this.$refs.customSelect[0] as CustomSelect;
console.log('customSelect showChild', custSelect);
custSelect.showOpen();
}

相关文章

  • react里面父子组件通讯

    一、父组件 二、子组件 三、父组件通过props向子组件传递参数,子组件通过调用父组件的回调函数callback并...

  • vue子父组件通信

    子父组件传递数据 父组件===》子组件。父组件通过props向子组件中传递数据和改变数据的函数,通过在子组件中调用...

  • react父子组件通信

    父组件通过props 给子组件传递数据,子组件则是通过调用父组件传给它的函数给父组件传递数据。

  • 2019-10-09(总结)Ant-design-pro开发常用

    onRef父子组件关联 子组件调用父组件的方法 注意:父组件传递的函数名和子组件通过this.props接收的函数...

  • 【Vue3 从入门到实战 进阶式掌握完整知识体系】014-探索组

    5、父子组件如何通过事件进行通信 子组件调用的方法让父组件处理 子组件调用父组件的方法来改变父组件的数据。子组件无...

  • 2017.11.23

    React如何在父组件里面调用子组件的方法在父组件中引用子组件时,给子组件定义ref='name',父组件通过th...

  • vue ref 获取子组件属性值

    父引入、注册组件并调用组件 引入、注册 调用组件 调用子组件的函数 调用子组件的属性

  • Vue项目=====组件之间通信的几种方式

    一、父组件给子组件传递数据 props 二、子组件给父组件传递数据通过$emit函数调用的方式 三、兄弟组件(任何...

  • React组件间数据的传递

    1.通过父组件传递数据给子组件: 2.子组件如何改变父组件的值 然后在子组件中调用父组件传递过来的方法

  • React项目组件间通讯方式简介

    一、父组件给子组件传递数据 props 二、子组件给父组件传递数据 通过函数调用的方式 三、兄弟组件传递数据 pu...

网友评论

      本文标题:vue+ts循环条件渲染的子组件 如何通过父组件调用子组件函数

      本文链接:https://www.haomeiwen.com/subject/ycxpudtx.html