vue组件开发-弹窗选择记录

组件需求

点击输入框, 弹窗显示表格,然后选择记录,
结果: 组件的绑定的值为对应主键, 但是显示值为对应名称。

效果

在这里插入图片描述

组件代码

<template>
  <el-input @click="dialogVisible = true" v-model="showName">
  </el-input>
  <el-dialog
      v-model="dialogVisible"
      title="请选择"
      width="80%"
      :before-close="handleClose"
      @open="beforeOpen"
  >
    <el-table :data="tableData" border class="table" v-loading="loading"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column :prop="label" label="名称" align="center"/>
    </el-table>
    <el-pagination
        class="pagination"
        v-model:currentPage="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    >
    </el-pagination>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleOk"
        >确定</el-button
        >
      </span>
    </template>
  </el-dialog>
</template>

<script>
import request from "../util/request";

export default {
  name: "Naruto-Select",
  data() {
    return {
      tableData: [],
      loading: false,
      dialogVisible: false,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      selection: [],
      showName: ''
    }
  },
  props: {
    id: {
      type: String,
      required: false
    },
    baseUrl: {
      type: String,
      required: true
    },
    label: {
      type: String,
      required: true
    }
  },
  emits: ['update:id'],
  watch: {
    id: {
      immediate: true,
      handler(val, oldVal) {
        if (!val) {
          this.showName = '';
          return;
        }
        if (val && val !== oldVal) {
          this.getById(val);
        }
      }
    }
  },
  methods: {
    getById(id) {
      let params = {id}
      request.get(`/api/${this.baseUrl}/get`, {params}).then((res) => {
        if (res.success) {
          this.showName = res.data[this.label];
        }
      })
    },
    handleOk() {
      if (this.selection.length !== 1) {
        this.$message.warning(`请选择单条记录`);
        return;
      }
      this.showName = this.selection[0][this.label];
      this.$emit(`update:id`, this.selection[0].id);
      this.handleClose();
    },
    handleSelectionChange(val) {
      this.selection = val;
    },
    beforeOpen() {
      this.get();
    },
    get() {
      let params = {
        pageNo: this.currentPage,
        pageSize: this.pageSize
      }
      this.loading = true
      request.get(`/api/${this.baseUrl}/list`, {params}).then((res) => {
        this.loading = false
        if (res.success) {
          this.total = res.data.total;
          this.tableData = res.data.records;
        }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.get();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.get();
    },
    handleClose() {
      this.dialogVisible = false;
    }
  }
}
</script>

<style scoped lang="less">
.pagination {
  padding-top: 10px;
  text-align: right;
}
</style>

import mySelect from './components/Naruto-Select.vue'

<my-select v-model:id="form.parentId" label="menuName" baseUrl="/service-admin/platform_menu"></my-select>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue组件中调用组件可以通过以下步骤实现: 1. 首先,创建一个组件,可以在组件的`template`中定义的内容和样式。 ```vue <template> <div class="modal"> <div class="modal-content"> <!-- 内容 --> <slot></slot> </div> </div> </template> <script> export default { name: 'Modal', // ...组件的其他配置 } </script> <style> .modal { /* 样式 */ } .modal-content { /* 内容样式 */ } </style> ``` 2. 在需要使用的父组件中,引入并注册组件。 ```vue <template> <div> <!-- 点击按钮触发 --> <button @click="openModal">打开</button> <!-- 使用组件 --> <Modal v-if="showModal"> <!-- 内容 --> <h2>这是一个</h2> <p>欢迎使用组件!</p> <button @click="closeModal">关闭</button> </Modal> </div> </template> <script> import Modal from './Modal.vue'; export default { name: 'ParentComponent', components: { Modal, }, data() { return { showModal: false, }; }, methods: { openModal() { this.showModal = true; }, closeModal() { this.showModal = false; }, }, } </script> ``` 在上述代码中,点击按钮 `打开` 会触发 `openModal` 方法,将 `showModal` 属性设置为 `true`,从而显示组件组件在父组件中使用时,可以通过插槽(slot)传递的内容和样式。 这样,当点击按钮时,组件会显示,并且可以通过中的按钮或其他交互元素来关闭

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值