<top-bar @submitAction="submitAction"></top-bar>
methods:{
// 添加——转TopBar.vue页面
submitAction(rowData){
this.$api.addList({rowData}).then(res=>{
this.tableData.unshift(res.data)
})
},
}
TopBar.vue页面
submitForm() {
this.$refs.form.validate(valid => {
if (valid) {
this.$emit('submitAction', this.form)
this.dialogFormVisible = false
} else {
console.log('error submit!!');
return false;
}
});
},
完整页面
<template>
<div class="top-bar">
<el-input
placeholder="请输入内容,按回车键搜索..."
clearable
@clear="getListdata"
@keydown.enter.native="handleSearch"
v-model="inputValue"
>
<template slot="append" class="search-first">
<el-button class="search" @click="handleSearch">搜索</el-button>
</template>
</el-input>
<el-button type="primary" @click="dialogFormVisible = true"
>添加商品</el-button
>
<!-- 弹出层 -->
<el-dialog
title="添加用户信息"
:visible.sync="dialogFormVisible"
append-to-body
width="30%"
>
<el-form :model="form" ref="form" :rules="rules">
<el-form-item label="姓名:" prop="name" :label-width="formLabelWidth">
<el-input v-model="form.name" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="地址:" prop="address" :label-width="formLabelWidth">
<el-input v-model="form.address" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="爱好:" prop="likes" :label-width="formLabelWidth">
<el-input v-model="form.likes" autocomplete="off"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取 消</el-button>
<el-button type="primary" @click="submitForm">确 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
export default {
data() {
return {
inputValue: "",
dialogTableVisible: false,
dialogFormVisible: false,
form: {
name: "",
address: "",
likes: "",
},
formLabelWidth: "120px",
rules: {
name: { required: true, message: '请输入姓名', trigger: 'blur' },
address: { required: true, message: '请输入地址', trigger: 'blur' },
likes: { required: true, message: '请输入爱好', trigger: 'blur' }
}
};
},
methods: {
// 传一个函数submitAction把this.form传给父组件
submitForm() {
this.$refs.form.validate(valid => {
if (valid) {
this.$emit('submitAction', this.form)
this.dialogFormVisible = false
} else {
console.log('error submit!!');
return false;
}
});
},
handleSearch() {
this.$emit("handleSearch", this.inputValue);
},
getListdata() {
this.$emit("getdataList");
},
},
};
</script>
<style lang="less" scoped>
.top-bar {
margin-bottom: 5px;
.el-input {
width: 300px;
.search-first {
cursor: pointer;
margin-right: 10px;
}
.search {
color: #000;
}
}
.el-button {
height: 40px;
}
}
/deep/.v-modal {
z-index: -1 !important;
}
/deep/.el-input__inner {
width: 300px;
}
</style>
|