el-select框,切换选项时弹出提示框

本文介绍如何在Element UI的el-select组件中实现选择项变更前的确认操作,通过监听v-model变化和使用visible-change事件,确保在选项改变前能够进行二次确认,以避免意外的数据修改。

table中的el-select选择框,当切换选项时弹出提示框,点击确认切换选项,取消则不改变数据

在这里插入图片描述
需求:如图,状态选择框切换选项时,弹出提示框;点击确定,发送请求更改状态;点击取消,选择框显示原来选项。

思路:由于element-ui的el-select选择后只能添加change事件,但是change事件一旦触发,选择框中的值已经改变。正常的方法是监听select中的v-model绑定的值(定义一个初始值,)。如下代码:

<template>
  <div class="approvalStage">
    <el-select v-model="storeLocation" placeholder="请选择">
      <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
    </el-select>
    <el-dialog
      :visible.sync="changeStoreVisible"
      class="taskDialog delete-shortcut-dialog"
      width="420px"
      :modal="false"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <template slot="title">
        <span style="font-size:16px;font-weight:400;">
          <i
            class="iconfont icon-warning"
            style="font-size:20px;color:rgba(23,155,255,1);margin-right:5px;"
          ></i>是否改变选项值
        </span>
      </template>
      <p class="tips-text" style="height: 38px;">
        <span style="color:red;font-size:14px;">改变选项值,</span>是否继续?
      </p>
      <div slot="footer" class="dialog-footer">
        <el-button @click="changeStoreCancle">取 消</el-button>
        <el-button type="primary" @click="changeStoreForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      options: [
        {
          value: '选项1',
          label: '黄金糕'
        },
        {
          value: '选项2',
          label: '双皮奶'
        },
        {
          value: '选项3',
          label: '蚵仔煎'
        },
        {
          value: '选项4',
          label: '龙须面'
        },
        {
          value: '选项5',
          label: '北京烤鸭'
        }
      ],
      storeLocation: '初始值',
      changeStoreVisible: false,
      beforeStorageValue: '', // 中介值
      afterStorageValue: ''
    }
  },
  watch: {
    storeLocation: {
      immediate: true,
      handler(val, old) {
        console.log('val:', val, 'old:', old)
        if (this.beforeStorageValue) {
          console.log(
            'val:',
            val,
            'old:',
            old,
            'this.beforeStorageValue',
            this.beforeStorageValue
          )
          if (val !== this.beforeStorageValue) {
            this.changeStoreVisible = true
          }
        }
      }
    }
  },
  mounted() {
    this.beforeStorageValue = this.storeLocation
  },
  methods: {
    changeStoreCancle() {
      this.storeLocation = this.beforeStorageValue
      this.changeStoreVisible = false
    },
    changeStoreForm() {
      console.log(this.storeLocation)
      this.beforeStorageValue = this.storeLocation // 确定更改后使中介值等于目前框内的值,这样下次确定改变取消的话,可以回到现在的值
      this.changeStoreVisible = false
    }
  }

}
</script>
<style lang="scss" scoped>
  .approvalStage{
    background-color: #fff;
    padding: 20px;
    padding-bottom: 40px;
    border-radius: 4px;
  }
</style>

此次项目里的选项是在表格内后台数据渲染而来,v-model无法设定一个初始值;但select提供了visible-change事件,官方文档描述:下拉框出现/隐藏时触发 出现则为 true,隐藏则为 false;利用此事件在点击下拉框,出现下拉框时把原来现实的值储存(baseValue)。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
select框值改变时提示框:
在这里插入图片描述

<template> <!-- 顶部搜索区域 --> <div class="filter-container"> <el-form :inline="true" :model="searchForm"> <!-- 问卷标题搜索 --> <el-form-item label="问卷标题"> <el-input v-model="searchForm.dcWjTitle" placeholder="输入问卷标题" clearable /> </el-form-item> <!-- 被测评人ID搜索 --> <el-form-item label="被测评人ID"> <el-input v-model="searchForm.dcid" placeholder="输入被测评人ID" clearable /> </el-form-item> <!-- 部门选择 --> <el-form-item label="人员部门"> <el-select v-model="searchForm.dcDept" placeholder="选择部门" clearable > <el-option v-for="dept in departments" :key="dept.value" :label="dept.label" :value="dept.value" /> </el-select> </el-form-item> <!-- 状态选择 --> <el-form-item label="提交状态"> <el-select v-model="searchForm.state" placeholder="选择状态" clearable > <el-option label="已提交" value="1" /> <el-option label="未提交" value="0" /> </el-select> </el-form-item> <el-form-item> <el-button type="primary" @click="handleSearch" icon="el-icon-search" >搜索</el-button> </el-form-item> </el-form> </div> <!-- 数据表格 --> <el-table :data="filteredData"> <!-- 表格列定义 --> </el-table> </template> <script> export default { data() { return { searchForm: { dcWjTitle: '', dcid: '', dcDept: '', state: '' }, rawData: [], // 原始数据 departments: [ // 部门选项 { label: '技术部', value: 'tech' }, { label: '市场部', value: 'market' } ] } }, computed: { // 过滤后的数据 filteredData() { return this.rawData.filter(item => (!this.searchForm.dcWjTitle || item.title.includes(this.searchForm.dcWjTitle)) && (!this.searchForm.dcid || item.dcid === this.searchForm.dcid) && (!this.searchForm.dcDept || item.dept === this.searchForm.dcDept) && (!this.searchForm.state || item.state === this.searchForm.state) ) } }, methods: { // 触发搜索 handleSearch() { this.fetchFilteredData() }, // 从后端获取数据 async fetchFilteredData() { try { const params = { title: this.searchForm.dcWjTitle, dcid: this.searchForm.dcid, dept: this.searchForm.dcDept, state: this.searchForm.state } // 调用后端API(示例) const { data } = await axios.get('/api/questionnaires', { params }) this.rawData = data } catch (error) { console.error('获取数据失败', error) } } } } </script> 帮我优化一下布局,适用于移动端
07-15
<template> <div class="app-container"> <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"> <el-form-item label="编码" prop="key"> <el-input v-model="queryParams.key" placeholder="请输入编码" clearable @keyup.enter.native="handleQuery"/> </el-form-item> <el-form-item label="名称" prop="name"> <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery"/> </el-form-item> <el-form-item label="表单分类" prop="category"> <el-select v-model="queryParams.category" placeholder="请选择表单分类" clearable> <el-option v-for="dict in dict.type.oa_customform_category" :key="dict.value" :label="dict.label" :value="dict.value"/> </el-select> </el-form-item> <el-form-item> <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button> <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> </el-form-item> </el-form> <TableCard> <el-row :gutter="10" class="mb8"> <el-col :span="1.5"> <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['oa:model:add']">创建新流程 </el-button> </el-col> <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> </el-row> <el-table :border="Global.tableShowBorder" v-loading="loading" :data="modelList" @selection-change="handleSelectionChange"> <!-- <el-table-column label="ID" align="center" prop="id"/>--> <el-table-column label="流程编码" align="center" prop="key"/> <el-table-column label="名称" align="center" prop="name"/> <el-table-column label="版本" align="center" prop="version"/> <el-table-column label="所属分类" align="center" prop="category"> <template slot-scope="scope"> <dict-tag :options="dict.type.oa_customform_category" :value="scope.row.category"/> </template> </el-table-column> <el-table-column label="创建间" align="center" prop="createTime"> <template slot-scope="scope"> <span>{{ parseTime(scope.row.createTime) }}</span> </template> </el-table-column> <el-table-column label="最后更新间" align="center" prop="lastUpdateTime"> <template slot-scope="scope"> <span>{{ parseTime(scope.row.lastUpdateTime) }}</span> </template> </el-table-column> <el-table-column label="元数据" align="center" prop="metaInfo" :show-overflow-tooltip="true"/> <el-table-column label="操作" align="center" class-name="small-padding fixed-width"> <template slot-scope="scope"> <el-button size="mini" type="text" icon="el-icon-edit" @click="edit(scope.row)" v-hasPermi="['oa:model:modelDesigner']">编辑</el-button> <el-button size="mini" type="text" icon="el-icon-edit" @click="deploy(scope.row)" v-hasPermi="['oa:model:edit']">运行</el-button> <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['oa:model:remove']">删除</el-button> </template> </el-table-column> </el-table> <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/> </TableCard> <!-- 添加或修改流程图对话 --> <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body> <el-form ref="form" :model="form" :rules="rules" label-width="80px"> <el-form-item label="类型" prop="type"> <el-radio-group v-model="form.type"> <el-radio label="0">使用单据类型创建</el-radio> <el-radio label="1">自定义创建</el-radio> </el-radio-group> </el-form-item> <el-form-item label="单据类型1" prop="billType" v-if="form.type=='0'"> <el-select v-model="form.billType" placeholder="请选择单据类型1"@change="selectBillType" filterable> <el-option v-for="(b, index) in billTypeList" :key="index" :label="b.billName" :value="b.billType"/> </el-select> </el-form-item> <el-form-item label="流程编码" prop="key"> <el-input v-model="form.key" placeholder="请输入流程编码" :disabled="form.type=='0'"/> </el-form-item> <el-form-item label="流程名称" prop="name"> <el-input v-model="form.name" placeholder="请输入流程名称" :disabled="form.type=='0'" /> </el-form-item> <el-form-item label="表单分类" prop="category"> <el-select v-model="form.category" placeholder="请选择表单分类" clearable> <el-option v-for="dict in dict.type.oa_customform_category" :key="dict.value" :label="dict.label" :value="dict.value"/> </el-select> </el-form-item> <el-form-item label="流程描述" prop="category"> <el-input type="textarea" v-model="form.description" placeholder="请输入流程描述" /> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button type="primary" @click="submitForm">确 定</el-button> <el-button @click="cancel">取 消</el-button> </div> </el-dialog> </div> </template> <script> import {listModel, getModel, delModel, addModel, updateModel,deploy} from "@/api/workflow/model"; import {listBillTypeAll} from "@/api/common/billType" export default { name: "Model", dicts: ['oa_customform_category'], data() { return { // 遮罩层 loading: true, // 选中数组 ids: [], // 非单个禁用 single: true, // 非多个禁用 multiple: true, // 显示搜索条件 showSearch: true, // 总条数 total: 0, // 流程图表格数据 modelList: [], // 弹出层标题 title: "", // 是否显示弹出层 open: false, // 查询参数 queryParams: { pageNum: 1, pageSize: 20, name: null, key: null, category: null, createTime: null, lastUpdateTime: null, version: null, metaInfo: null, deploymentId: null, // orderByColumn:"lastUpdateTime", // isAsc:"descending" }, // 表单参数 form: { type:'0' }, // 表单校验 rules: { billType: [ {required: true, trigger: "blur", message: "单据类型不能为空"}, ], key: [ {required: true, trigger: "blur", message: "流程编码不能为空"}, ], name: [ {required: true, trigger: "blur", message: "流程名称不能为空"}, ], category: [ {required: true, trigger: "blur", message: "表单分类不能为空"}, ] }, billTypeList:[], }; }, created() { this.getList(); this.getListTypeList(); }, methods: { getListTypeList(){ listBillTypeAll({status:'0'}).then(res=>{ this.billTypeList = res.data; }) }, selectBillType(billType){ if(billType){ this.billTypeList.forEach((item)=>{ if(item.billType==billType){ this.form.key = item.billType; this.form.name = item.billName; } }) } }, // selectBillType(billType) { // if (billType) { // this.billTypeList.forEach((item) => { // if (item.billType === billType) { // console.log('匹配到:', item); // 👈 加这行调试 // this.form.key = item.billType; // this.form.name = item.billName; // // 关键:自动填充表单分类 // this.form.category = item.category; // } // }); // } else { // // 可选:如果清空选择,则清除相关字段 // this.form.key = ""; // this.form.name = ""; // this.form.category = ""; // } // }, /** 查询流程图列表 */ getList() { this.loading = true; listModel(this.queryParams).then(response => { let list = response.rows; this.modelList = list; this.total = response.total; this.loading = false; }); }, // 取消按钮 cancel() { this.open = false; this.reset(); }, // 表单重置 reset() { this.form = { id: null, rev: null, name: null, type:'0', key: null, category: null, createTime: null, lastUpdateTime: null, version: null, metaInfo: null, deploymentId: null, editorSourceValueId: null, editorSourceExtraValueId: null, tenantId: null }; this.resetForm("form"); }, /** 搜索按钮操作 */ handleQuery() { this.queryParams.pageNum = 1; this.getList(); }, /** 重置按钮操作 */ resetQuery() { this.resetForm("queryForm"); this.handleQuery(); }, // 多选选中数据 handleSelectionChange(selection) { this.ids = selection.map(item => item.id) this.single = selection.length !== 1 this.multiple = !selection.length }, /** 新增按钮操作 */ handleAdd() { this.reset(); this.open = true; this.title = "添加流程图"; }, /** 修改按钮操作 */ handleUpdate(row) { this.reset(); const id = row.id || this.ids getModel(id).then(response => { this.form = response.data; this.open = true; this.title = "修改流程图"; }); }, /** 保存按钮 */ submitForm() { this.$refs["form"].validate(valid => { if (valid) { addModel(this.form).then(response => { this.$modal.msgSuccess("创建成功"); this.open = false; this.getList(); }); } }); }, /** 删除按钮操作 */ handleDelete(row) { const ids = row.id || this.ids; this.$modal.confirm('是否确认删除流程图版本号为"' + row.version + '"的数据项?').then(function () { return delModel(ids); }).then(() => { this.getList(); this.$modal.msgSuccess("删除成功"); }).catch(() => { }); }, /** 导出按钮操作 */ handleExport() { this.download('oa/model/export', { ...this.queryParams }, `model_${new Date().getTime()}.xlsx`) }, /** 运行 **/ deploy(row){ this.$modal.confirm('是否确认运行该流程图吗?').then(()=> { deploy(row.id).then(res=>{ this.$modal.msgSuccess("运行成功"); }) }).then(() => { this.getList(); }).catch(() => { }); }, //编辑 edit(row){ this.$router.push({path: "/oa/model/modelDesigner/" + row.id}); } } }; </script> 为啥点击<el-form-item label="单据类型1" prop="billType" v-if="form.type=='0'"> <el-select v-model="form.billType" placeholder="请选择单据类型1"@change="selectBillType" filterable> <el-option v-for="(b, index) in billTypeList" :key="index" :label="b.billName" :value="b.billType"/> </el-select> </el-form-item>没有反应了
11-07
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值