vue 實(shí)現(xiàn)動(dòng)態(tài)表單點(diǎn)擊新增 增加一行輸入框

點(diǎn)擊增加后會(huì)新增一行,點(diǎn)擊每行后面的刪除圖標(biāo)則會(huì)刪除該行,新增按鈕只會(huì)出現(xiàn)在最后一行

<el-col :span="12" class="mb20">
                    <el-form-item
                        :label="index == 0 ? '選擇原材料' : ''"
                        v-for="(item, index) in form.productItemList"
                        :key="index"
                        :prop="'domains.' + index + '.value'"
                    >
                        <el-select v-model="item.name" filterable clearable placeholder="請(qǐng)輸入原材料" @change="changeValue" @clear="clearValue">
                            <el-option v-for="item in productAll" :key="item.value" :label="item.name" :value="item.id" />
                        </el-select>
                        <el-input v-model="item.num" placeholder="請(qǐng)輸入數(shù)量" style="position: absolute; left: 300px; width: 160px"></el-input>
                        <el-button
                            v-if="index !== 0"
                            class="mt-2"
                            type="danger"
                            style="position: absolute; left: 545px; bottom: 0px"
                            @click.prevent="removeDomain(domain)"
                            >刪除</el-button
                        >
                    </el-form-item>
                    <el-form-item>
                        <el-button @click="addDomain" type="primary" style="position: absolute; left: 475px; bottom: 18px">添加</el-button>
                    </el-form-item>
                </el-col>

定義方法

// 添加原材料
const addDomain = () => {
    form.productItemList.push({
        name: '',
        num: '',
    });
};

// 刪除原材料
const removeDomain = (item: any) => {
    const index = form.productItemList.indexOf(item);
    if (index === -1) {
        form.productItemList.splice(index, 1);
    }
};

定義表單提交數(shù)據(jù)

// 提交表單數(shù)據(jù)
const form = reactive({
    productItemList: [
        {
            name: '',
            num: '',
        },
    ],
});
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容