关于前端:vue-ElementUI-实现完整版用户列表以及增删改功能

49次阅读

共计 7025 个字符,预计需要花费 18 分钟才能阅读完成。

用户列表曾经齐全做好,其中包含用户列表查问,数据增删改,以及翻页性能。

<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>
      <!-- 搜寻与增加区域 -->
      <el-row :gutter="20">
        <el-col :span="15">
          <el-input
            placeholder="请输出内容"
            v-model="queryInfo.query"
            clearable
            @clear="getUserList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getUserList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            > 增加用户 </el-button
          >
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="userList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <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="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              @change="userStateChanged(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 批改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.id)"
            ></el-button>
            <el-tooltip
              effect="dark"
              content="调配角色"
              placement="top-start"
              :enterable="false"
            >
              <el-button
                type="warning"
                icon="el-icon-setting"
                size="mini"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <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%"
      @close="addDialogClosed"
    >
      <!-- 主体区域 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="明码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="addForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false"> 取 消 </el-button>
        <el-button type="primary" @click="addUser"> 确 定 </el-button>
      </span>
    </el-dialog>
    <!-- 批改用户对话框 -->
    <el-dialog
      title="批改用户"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="70px"
      >
        <el-form-item label="用户名">
          <el-input v-model="editForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="editForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false"> 取 消 </el-button>
        <el-button type="primary" @click="editUserInfo"> 确 定 </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {data() {
    // 校验邮箱
    var checkEmail = (rule, value, cb) => {const regEmail = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/
      if (regEmail.test(value)) {
        // 非法邮箱
        return cb()}
      cb(new Error('请输出非法的邮箱'))
    }
    // 校验手机号
    var checkMobile = (rule, value, cb) => {const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
      if (regMobile.test(value)) {return cb()
      }
      cb(new Error('请输出非法手机号'))
    }
    return {
      // 获取用户列表的参数对象
      queryInfo: {
        query: '',
        // 以后的页数
        pagenum: 1,
        // 以后每页显示的条数
        pagesize: 2
      },
      userList: [],
      total: 0,
      // 管制增加用户对话框显示与暗藏
      addDialogVisible: false,
      // 增加用户的表单数据
      addForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      // 增加表单验证规定对象
      addFormRules: {
        username: [
          {
            required: true,
            message: '请输出用户名',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 10,
            message: '用户名的长度在 3 -10 个字符之间',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输出明码',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 15,
            message: '明码的长度在 6 -15 个字符之间',
            trigger: 'blur'
          }
        ],
        email: [
          {
            required: true,
            message: '请输出邮箱',
            trigger: 'blur'
          },
          {
            validator: checkEmail,
            trigger: 'blur'
          }
        ],
        mobile: [
          {
            required: true,
            message: '请输出手机',
            trigger: 'blur'
          },
          {
            validator: checkMobile,
            trigger: 'blur'
          }
        ]
      },
      // 批改用户对话框
      editDialogVisible: false,
      editForm: {},
      // 批改表单验证对象
      editFormRules: {
        email: [
          {
            required: true,
            message: '请输出邮箱',
            trigger: 'blur'
          },
          {
            validator: checkEmail,
            trigger: 'blur'
          }
        ],
        mobile: [
          {
            required: true,
            message: '请输出手机',
            trigger: 'blur'
          },
          {
            validator: checkMobile,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {this.getUserList()
  },
  methods: {async getUserList() {const { data: res} = await this.$http.get('users', {params: this.queryInfo})
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      console.log(res)
      this.userList = res.data.users
      this.total = res.data.total
    },
    // 监听 pagesize 扭转的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getUserList()
      console.log(newSize)
    },
    // 监听页码值扭转的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getUserList()
      console.log(newPage)
    },
    // 批改用户状态
    async userStateChanged(userInfo) {const { data: res} = await this.$http.put(`users/${userInfo.id}/state/${userInfo.mg_state}`
      )
      if (res.meta.status !== 200) {
        userInfo.mg_state = !userInfo.mg_state
        return this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
    },
    // 监听增加用户对话框的敞开事件
    async addDialogClosed() {
      // 重置表单
      this.$refs.addFormRef.resetFields()},
    // 点击按钮,增加新用户
    addUser() {
      this.$refs.addFormRef.validate(async valid => {if (!valid) return
        // 能够发动增加用户的网络申请
        const {data: res} = await this.$http.post('users', this.addForm)
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
        // 增加胜利
        this.$message.success(res.meta.msg)
        // 暗藏弹出框
        this.addDialogVisible = false
        // 刷新用户列表
        this.getUserList()})
    },
    // 批改用户
    async showEditDialog(id) {const { data: res} = await this.$http.get('users/' + id)
      if (res.meta.status !== 200) {return this.$message.error(this.meta.msg)
      }
      this.editForm = res.data
      this.editDialogVisible = true
    },
    // 监听批改用户对话框的敞开事件
    editDialogClosed() {this.$refs.editFormRef.resetFields()
    },
    // 批改用户信息并提交
    editUserInfo() {
      this.$refs.editFormRef.validate(async valid => {if (!valid) return
        // 发动批改用户信息的数据申请
        const {data: res} = await this.$http.put(
          'users/' + this.editForm.id,
          {
            email: this.editForm.email,
            mobile: this.editForm.mobile
          }
        )
        if (res.meta.status !== 200) {return this.$message.error(this.meta.msg)
        }
        // 敞开对话框
        this.editDialogVisible = false
        // 刷新数据列表
        this.getUserList()
        // 提醒批改胜利
        this.$message.success('更新胜利')
      })
    },
    // 依据 id 删除对应的用户信息
    async removeUserById(id) {
      // 弹框提醒用户是否删除
      const confirmResult = await this.$confirm(
        '此操作将永恒删除该用户, 是否持续?',
        '提醒',
        {
          confirmButtonText: '确定',
          cancelButtonText: '勾销',
          type: 'warning'
        }
      ).catch(err => err)
      // 如果用户确认删除,则返回字符串 confirm
      // 如果用户勾销删除,则返回字符串 cancel
      if (confirmResult !== 'confirm') {return this.$message.info('已勾销删除')
      }
      // 发送删除申请
      const {data: res} = await this.$http.delete('users/' + id)
      if (res.meta.status !== 200) {this.$message.error('删除失败')
      }
      this.$message.success('删除胜利')
      this.getUserList()}
  }
}
</script>

<style lang="less" scoped></style>

正文完
 0