目录
前言
提示:本篇继续点位管理的改造
一、页面原型
- 页面展示
- 新增
二、修改
1、页面展示
页面修改:修改标签换行、顺序顺序、地址过长时换行问题;
html
<el-table v-loading="loading" :data="nodeList" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" align="center" />
<el-table-column label="序号" type="index" align="center" prop="id" width="50" />
<el-table-column label="点位名称" align="center" prop="nodeName" />
<el-table-column label="所在区域" align="center" prop="regionId" />
<el-table-column label="商圈类型" align="center" prop="businessType">
<template #default="scope">
<dict-tag :options="business_type" :value="scope.row.businessType" />
</template>
</el-table-column>
<el-table-column label="合作商ID" align="center" prop="partnerId" />
<el-table-column label="详细地址" align="left" prop="address" show-overflow-tooltip />
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template #default="scope">
<el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['manage:node:edit']">修改</el-button>
<el-button link type="primary" @click="handleDelete(scope.row)"
v-hasPermi="['manage:node:remove']">删除</el-button>
</template>
</el-table-column>
</el-table>
新增表单修改:
html
<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
<el-form-item label="点位名称" prop="nodeName">
<el-input v-model="queryParams.nodeName" placeholder="请输入点位名称" clearable @keyup.enter="handleQuery" />
</el-form-item>
<el-form-item label="区域搜索" prop="regionId">
<el-select v-model="queryParams.regionId" placeholder="请选择区域" clearable @change="handleQuery">
<el-option v-for="item in regionOptions" :key="item.id" :label="item.regionName" :value="item.id" />
</el-select>
</el-form-item>
<!-- <el-form-item label="合作商" prop="partnerId">
<el-input
v-model="queryParams.partnerId"
placeholder="请输入合作商ID"
clearable
@keyup.enter="handleQuery"
/>
</el-form-item> -->
<el-form-item>
<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
<el-button icon="Refresh" @click="resetQuery">重置</el-button>
</el-form-item>
</el-form>
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
<el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['manage:node:add']">新增</el-button>
</el-col>
<el-col :span="1.5">
<el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
v-hasPermi="['manage:node:edit']">修改</el-button>
</el-col>
<el-col :span="1.5">
<el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
v-hasPermi="['manage:node:remove']">删除</el-button>
</el-col>
<el-col :span="1.5">
<el-button type="warning" plain icon="Download" @click="handleExport"
v-hasPermi="['manage:node:export']">导出</el-button>
</el-col>
<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
</el-row>
<el-table v-loading="loading" :data="nodeList" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" align="center" />
<el-table-column label="序号" type="index" align="center" prop="id" width="50" />
<el-table-column label="点位名称" align="center" prop="nodeName" />
<el-table-column label="所在区域" align="center" prop="regionId" />
<el-table-column label="商圈类型" align="center" prop="businessType">
<template #default="scope">
<dict-tag :options="business_type" :value="scope.row.businessType" />
</template>
</el-table-column>
<el-table-column label="合作商ID" align="center" prop="partnerId" />
<el-table-column label="详细地址" align="center" prop="address" />
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template #default="scope">
<el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['manage:node:edit']">修改</el-button>
<el-button link type="primary" @click="handleDelete(scope.row)"
v-hasPermi="['manage:node:remove']">删除</el-button>
</template>
</el-table-column>
</el-table>
2、新增
- 修改标签名称
html
<!-- 添加或修改点位管理对话框 -->
<el-dialog :title="title" v-model="open" width="500px" append-to-body>
<el-form ref="nodeRef" :model="form" :rules="rules" label-width="80px">
<el-form-item label="点位名称" prop="nodeName">
<el-input v-model="form.nodeName" placeholder="请输入点位名称" />
</el-form-item>
<el-form-item label="所在区域" prop="regionId">
<el-select v-model="form.regionId" placeholder="请选择区域">
<el-option v-for="item in regionOptions" :key="item.id" :label="item.regionName" :value="item.id" />
</el-select>
</el-form-item>
<el-form-item label="商圈类型" prop="businessType">
<el-select v-model="form.businessType" placeholder="请选择商圈类型">
<el-option v-for="dict in business_type" :key="dict.value" :label="dict.label"
:value="parseInt(dict.value)"></el-option>
</el-select>
</el-form-item>
<el-form-item label="合作商" prop="partnerId">
<el-select v-model="form.partnerId" placeholder="请选择合作商" filterable>
<el-option
v-for="item in partnerOptions"
:key="item.id"
:label="item.partnerName"
:value="item.id"
/>
</el-select>
</el-form-item>
<el-form-item label="详细地址" prop="address">
<el-input v-model="form.address" placeholder="请输入详细地址" />
</el-form-item>
<el-form-item label="备注" prop="remark">
<el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
</el-form-item>
</el-form>
<template #footer>
<div class="dialog-footer">
<el-button type="primary" @click="submitForm">确 定</el-button>
<el-button @click="cancel">取 消</el-button>
</div>
</template>
</el-dialog>
- 将新增表单中所在区域改为下拉框
javascript
import { listRegion } from "@/api/manage/region";
const regionOptions = ref([]);
/** 获取区域列表 */
function getRegionList() {
listRegion({
pageNum: 1,
pageSize: 10000
}).then(response => {
regionOptions.value = response.rows
});
}
// 在组件挂载时获取区域列表
onMounted(() => {
getRegionList();
});
html
<el-form-item label="所在区域" prop="regionId">
<el-select v-model="form.regionId" placeholder="请选择区域">
<el-option v-for="item in regionOptions" :key="item.id" :label="item.regionName" :value="item.id" />
</el-select>
</el-form-item>
- 将新增表单中归属合作商改为下拉框
javascript
import{listPartner}from"@/api/manage/partner";
const partnerOptions = ref([]);
/** 获取合作商列表 */
function getPartnerList() {
listPartner({
pageNum: 1,
pageSize: 10000
}).then(response => {
partnerOptions.value = response.rows;
});
}
// 在组件挂载时获取合作商列表
onMounted(() => {
getPartnerList();
});
html
<el-form-item label="合作商" prop="partnerId">
<el-select v-model="form.partnerId" placeholder="请选择合作商" filterable>
<el-option
v-for="item in partnerOptions"
:key="item.id"
:label="item.partnerName"
:value="item.id"
/>
</el-select>
</el-form-item>
- 将搜索栏修改为区域搜索,并使用下拉框展示
标签改为下拉框
html
<el-form-item label="区域搜索" prop="regionId">
<el-select v-model="queryParams.regionId" placeholder="请选择区域" clearable @change="handleQuery">
<el-option v-for="item in regionOptions" :key="item.id" :label="item.regionName" :value="item.id" />
</el-select>
</el-form-item>
调用API、JS
javascript
import { listRegion } from "@/api/manage/region";
const regionOptions = ref([]);
/** 获取区域列表 */
function getRegionList() {
listRegion({
pageNum: 1,
pageSize: 10000
}).then(response => {
regionOptions.value = response.rows
});
}
// 在组件挂载时获取区域列表
onMounted(() => {
getRegionList();
});
3 、总结思路
通过本篇,我们学会将修改表单、输入框改为下拉框的通用思路
思路:
- 所涉及的文件:通常是api中所在模块的js文件和view中所在模块vue文件(包含了页面显示的组件和JS函数)
我们以合作商的下拉框为例:
-
调用前端API
想要将输入框变为下拉框并展示数据,我们就要考虑如何从后端获取数据;
在合作商的JS文件中,我们看到有一个可以查询合作商列表的函数,该函数使用get向后端 /manage/partner/list发起请求;
后端 /manage/partner/list 收到请求后会查询合作商列表,并发起响应
-
创建调用函数
设置partnerOPptions用来接收整个后端传来的数据;
listPartner是刚刚API函数,想像它已经获得了后端传来的数据,接着拿到10000条数据(本质是想获得所有数据),并将其中rows数据交给刚刚创建的partnerOptions,用来稍后使用;
javascript
/** 获取合作商列表 */
function getPartnerList() {
listPartner({
pageNum: 1,
pageSize: 10000
}).then(response => {
partnerOptions.value = response.rows;
});
}
// 在组件挂载时获取合作商列表
onMounted(() => {
getPartnerList();
});
- 修改表单
为了展示partnerOptions中的合作商名称数据,使用下拉框;value代表获取数据中的id字段值,key代表获取数据中的id字段作为标识,label代表最终展示的值为数据中的合作商名称;
html
<el-form-item label="合作商" prop="partnerId">
<el-select v-model="form.partnerId" placeholder="请选择合作商" filterable>
<el-option
v-for="item in partnerOptions"
:key="item.id"
:label="item.partnerName"
:value="item.id"
/>
</el-select>
</el-form-item>