Vue_shop学习43:实现添加用户的功能

点击按钮:弹出添加按钮的对话框

组件 | Element

注册


 将对话框基本内容设置好

  <!-- 添加用户 的对话框-->
        <el-dialog
            title="提示"
            :visible.sync="addDialogVisible"
            width="50%">
            <!-- 内容主题区域 -->
            <span>ddd</span>
            <span slot="footer" class="dialog-footer">
                <!-- 底部按钮区域 取消按钮的时候隐藏对话框 -->
                <el-button @click="addDialogVisible= false">取 消</el-button>
                
                <el-button type="primary" @click="addDialogVisible= false">确 定</el-button>
            </span>
        </el-dialog>
 data() {
            return {
           
                addDialogVisible:false,//控制添加用户对话框的隐藏   false 
            }
        },

此时给添加用户按钮绑定事件使得弹出对话框按钮

  <el-button type="primary" @click="addDialogVisible=true">添加用户</el-button>


<template>
    <div>
        <!-- 面包屑导航区 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片视图区域 -->
        <el-card class="box-card">
            <!-- 搜索和添加区域 -->
            <el-row :gutter="20">
                <el-col :span="7">
                    <div>
                        <el-input placeholder="请输入内容" v-model="queryInfo.query"
                            clearable @clear="getUserList">
                            <el-button slot="append" icon="el-icon-search"
                                @click="getUserList"></el-button>
                        </el-input>
                    </div>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="addDialogVisible=true">添加用户</el-button>
                </el-col>
            </el-row>
            <!-- 用户列表区域 -->
            <!-- data指定数据源 stripe斑马 border边框-->
            <el-table border :data="userList" stripe>
                <!-- 索引列 -->
                <el-table-column type="index" label="#"></el-table-column>
                <!-- prop是data数组中的数据 -->
                <el-table-column label="姓名" prop="username"> </el-table-column>
                <el-table-column label="邮箱" prop="email"> </el-table-column>
                <el-table-column label="电话" prop="mobile"> </el-table-column>
                <el-table-column label="角色" prop="role_name"> </el-table-column>
                <el-table-column label="状态">
                    <!-- 作用域插槽 slot-scope获取子组件的slot引用scope引用变量名 具有所有sloat属性 作用域插槽会覆盖prop -->
                    <template slot-scope="scope">
                        <!-- 将拿到的数据渲染 -->
                        <el-switch @change="userStateChanged(scope.row)"
                            v-model=scope.row.mg_state>
                        </el-switch>
                    </template>
                </el-table-column>
                <!-- 为了让一行可以放下3个按钮width -->
                <el-table-column label="操作" width="180px">
                    <!-- 作用域插槽 -->
                    <template slot-scope="scope">
                        <!-- scope.row将拿到的数据渲染 -->
                        <!-- 修改按钮 -->
                        <el-button type="primary" icon="el-icon-edit"
                            size="mini"></el-button>
                        <!-- 删除按钮 -->
                        <el-button type="danger" icon="el-icon-delete"
                            size="mini"></el-button>
                        <!-- 分配角色按钮 -->
                        <el-tooltip effect="dark" content="分配角色" placement="top"
                            :enterable="false">
                            <el-button type="warning" icon="el-icon-setting"
                                size="mini"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页区域 -->
            <!-- current-page当前页码数  page-size当前每页显示多少条数据 layout 展示菜单项-->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[1, 2, 5, 10]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </el-card>
        <!-- 添加用户 的对话框-->
        <el-dialog
            title="提示"
            :visible.sync="addDialogVisible"
            width="50%">
            <!-- 内容主题区域 -->
            <span>ddd</span>
            <span slot="footer" class="dialog-footer">
                <!-- 底部按钮区域 取消按钮的时候隐藏对话框 -->
                <el-button @click="addDialogVisible= false">取 消</el-button>
                
                <el-button type="primary" @click="addDialogVisible= false">确 定</el-button>
            </span>
        </el-dialog>


    </div>
</template>
<script>
    export default{
        data() {
            return {
                // 获取用户列表的参数
                queryInfo:{
                    query:'',//查询参数 可以为空
                    pagenum:1,//当前页码 不能为空 
                    pagesize:2// 每页显示条数 不能为空 
                },
                userList:[],//所有数据列表
                total:0,//总数据条数
                addDialogVisible:false,//控制添加用户对话框的隐藏   false 
            }
        },
        created() {
            //在创建的时候获取用户列表数据 调用函数
            this.getUserList()
        },
        methods: {
           async getUserList(){
                //get请求-写在data中
              const{data:res}= await this.$http.get('users',{params:this.queryInfo})
              //console.log(res)//测试
              if(res.meta.status!==200) return this.$message.error('获取用户列表数据失败')
                this.userList=res.data.users
                this.total=res.data.total
               
            },
            handleSizeChange(val) { //切换每页显示条触发 拿到最新的
                this.queryInfo.pagesize=val
                this.getUserList()//并且重新发送数据请求
            },
            handleCurrentChange(val) {//页码值改变的事件
                this.queryInfo.pagenum=val
                this.getUserList()//并且重新发送数据请求
            },
            async userStateChanged(userinfo){//监听switch状态的改变
                //console.log(userinfo) //测试
                //模板字符串 动态拼接
              const {data:res}= await this.$http.put(`users/${userinfo.id}/state/${userinfo.mg_state}`)
              if(res.meta.status!==200) {
                return this.$message.error('更新用户状态失败')
                //并且将页面已经变化的数据重置回去
                userinfo.mg_state=!userinfo.mg_state
                }
                this.$message.success('更新用户状态成功')
            }
           
            
        },
    }
</script>
<style lang="less" scoped>
    
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值