在views/user下新建AddUser.vue模块,细分新增功能

完善新增按钮功能
通过按钮实现表单弹窗
根据Element-plus提供的表单组件,修改表单属性

根据数据库表中的字段,设置需要从表单中获取的信息
pull/28/head
riverflow 2 months ago
parent 0ff2c7b98b
commit efb5d3995d

@ -3,5 +3,4 @@
<router-view /> <router-view />
</template> </template>
<style lang="scss"> <style lang="scss"></style>
</style>

@ -5,3 +5,14 @@ export type ListUserParm = {
name: string; name: string;
phone: string; phone: string;
} }
// 定义表单数据类型
export type UserModel = {
userId: string;
username: string;
password: string;
phone: string;
email: string;
sex: string;
name: string;
}

@ -59,7 +59,7 @@ const onConfirm = ()=>{
<style lang="scss"> <style lang="scss">
.container{ .container{
overflow-x: initial; overflow-x: hidden;
overflow-y: auto; overflow-y: auto;
} }

@ -0,0 +1,28 @@
import { ref } from "vue"
// 抽取增删改的业务操作
export default function useUser() {
// 获取AddUser里面的show方法
const showBtn = ref<{ show: (title: string, width: number, height: number) => void }>();
// 增
const addBtn = () => {
showBtn.value.show("新增", 630, 180)
}
// 删
const deleteBtn = () => {
}
// 改
const editBtn = () => {
}
return {
showBtn,
addBtn,
deleteBtn,
editBtn
}
}

@ -1,7 +1,7 @@
// 抽离表格相关业务 // 抽离表格相关业务
import type { ListUserParm } from "../../api/user/userModel"; import type { ListUserParm } from "@/api/user/userModel";
import { reactive } from "vue"; import { ref, reactive } from "vue";
export default function useUserTable() { export default function useUserTable() {
// 列表查询的参数 // 列表查询的参数
@ -13,8 +13,29 @@ export default function useUserTable(){
}) })
// 列表
const getList = () => {
}
// 获取show方法
const tableShowBtn = ref<{show:(title: string)=>void}>()
// 搜索按钮
const searchBtn = () => {
}
// 重置按钮
const resetBtn = () => {
tableShowBtn.value.show("重置")
}
// 将表格查询的方法返回出去 // 将表格查询的方法返回出去
return { return {
listParm tableShowBtn,
listParm,
getList,
searchBtn,
resetBtn
} }
} }

@ -8,8 +8,8 @@ export default function useDialog(){
const dialog = reactive({ const dialog = reactive({
title: '新增', title: '新增',
visible: false, visible: false,
width: 600, width: 630,
height: 100 height: 280
}) })
// 弹框关闭 // 弹框关闭
@ -23,7 +23,12 @@ export default function useDialog(){
} }
// 显示弹框 // 显示弹框
const onShow = ()=>{ const onShow = (title: string, width: number, height: number)=>{
// 设置一个string参数当不同按钮调用时可以直接传参改弹框标题
dialog.title = title
// 宽高参数
dialog.width = width
dialog.height = height
dialog.visible = true dialog.visible = true
} }

@ -48,7 +48,7 @@
justify-content: space-between; justify-content: space-between;
} }
.main { // .main {
background-color: darkcyan; // background-color: darkcyan;
} // }
</style> </style>

@ -1,7 +1,7 @@
<template> <template>
<div> <div>
首页面 首页面
<el-button type="primary" size="default" @click="onShow"></el-button> <el-button type="primary" size="default" @click="onShow('测试')"></el-button>
</div> </div>
<SysDialog <SysDialog

@ -0,0 +1,149 @@
<template>
<SysDialog :title="dialog.title" :visible="dialog.visible" :width="dialog.width" :height="dialog.height"
@onClose="onClose" @onConfirm="commit">
<!-- 通过插槽名称输入内容 -->
<template #content>
<el-form :model="addModel" ref="addFormRef" :rules="rules" label-width="80px" :inline="false" size="default">
<!-- 创建el-row容器将姓名性别放入一行美化页面 -->
<el-row :gutter="20">
<el-col :span="12" :offset="0">
<el-form-item prop="name" label="姓名">
<el-input v-model="addModel.name"></el-input>
</el-form-item>
</el-col>
<el-col :span="12" :offset="0">
<!-- 通过单选框实现性别选择 -->
<el-form-item prop="sex" label="性别">
<el-radio-group v-model="addModel.sex">
<el-radio :value="'0'"></el-radio>
<el-radio :value="'1'"></el-radio>
</el-radio-group>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="20">
<el-col :span="12" :offset="0">
<el-form-item prop="phone" label="电话">
<el-input v-model="addModel.phone"></el-input>
</el-form-item>
</el-col>
<el-col :span="12" :offset="0">
<el-form-item prop="email" label="邮箱">
<el-input v-model="addModel.email"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="20">
<el-col :span="12" :offset="0">
<el-form-item prop="username" label="账户">
<el-input v-model="addModel.username"></el-input>
</el-form-item>
</el-col>
<el-col :span="12" :offset="0">
<el-form-item prop="password" label="密码">
<el-input v-model="addModel.password"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
</template>
</SysDialog>
</template>
<script setup lang="ts">
//
import SysDialog from '@/components/SysDialog.vue';
//
import useDialog from '@/hooks/useDialog';
import { ref, reactive } from 'vue';
// UserModel
import { UserModel } from '@/api/user/userModel';
import { FormInstance } from 'element-plus';
//
const { dialog, onClose, onConfirm, onShow } = useDialog()
//
const show = (title: string, width: number, height: number) => {
// dialog.title:
onShow(title, width, height)
// dialog.visible = true;
//
addFormRef.value.resetFields()
}
//
defineExpose({
show
})
//
const addModel = reactive<UserModel>({
userId: "",
username: "",
password: "",
phone: "",
email: "",
sex: "",
name: "",
})
// ref
const addFormRef = ref<FormInstance>()
//
const rules = reactive({
name: [{
required: true,
message: '请输入姓名',
trigger: 'blur'
}],
sex: [{
required: true,
message: '请选择性别',
trigger: 'blur'
}],
phone: [{
required: true,
message: '请输入电话',
trigger: 'blur'
}],
username: [{
required: true,
message: '请输入用户名',
trigger: 'blur'
}],
password: [{
required: true,
message: '请输入密码',
trigger: 'blur'
}]
})
//
const commit = () => {
addFormRef.value.validate((valid) => {
if(valid){
onConfirm()
}
})
}
</script>
<style></style>

@ -9,14 +9,18 @@
<el-input v-model="listParm.phone" placeholder="请输入电话:"></el-input> <el-input v-model="listParm.phone" placeholder="请输入电话:"></el-input>
</el-form-item> </el-form-item>
<el-form-item> <el-form-item>
<el-button :icon="Search">搜索</el-button> <el-button :icon="Search" @click="searchBtn"></el-button>
<el-button :icon="Close">重置</el-button> <el-button plain :icon="Close" type="danger" @click="resetBtn"></el-button>
<el-button type="primary" :icon="Plus">新增</el-button> <el-button plain type="primary" :icon="Plus" @click="addBtn"></el-button>
</el-form-item> </el-form-item>
</el-form> </el-form>
</el-main> </el-main>
<!-- 新增编辑按钮弹框 -->
<AddUser ref="showBtn"></AddUser>
<AddUser ref="tableShowBtn"></AddUser>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
@ -27,9 +31,15 @@
import useUserTable from '@/compositions/user/useUserTable'; import useUserTable from '@/compositions/user/useUserTable';
// //
const { listParm } = useUserTable() const { tableShowBtn, listParm, getList, searchBtn, resetBtn } = useUserTable()
</script>
// useUser,
import useUser from '@/compositions/user/useUser';
<style scoped> const { showBtn, addBtn, deleteBtn, editBtn } = useUser()
// AddUser,
import AddUser from './AddUser.vue';
</script>
</style> <style scoped></style>

@ -59,11 +59,21 @@ declare module '@/compositions/*' {
} }
// 添加api路径别名支持 // 添加api路径别名支持
// declare module '@/api/user/userModel' { declare module '@/api/user/userModel' {
// export type ListUserParm = { export type ListUserParm = {
// currentPage: number; currentPage: number;
// pageSize: number; pageSize: number;
// name: string; name: string;
// phone: string; phone: string;
// } }
// }
export type UserModel = {
userId: string;
username: string;
password: string;
phone: string;
email: string;
sex: string;
name: string;
}
}
Loading…
Cancel
Save