element-ui——添加数据

    <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>

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Southern Wind

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值