关于前端:vue-ElementUI-实现数据双向绑定

38次阅读

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

目前在练手做一个后盾管理系统,想要实现用户列表页面的显示,能够进行用户查问、分页以及增删改性能,以下是最终效果图:

废话不多说,先上代码:

通过标签中 v-model 进行数据绑定

<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="{}">
            <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-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>
  </div>
</template>

数据初始化、数据校验,以及业务逻辑解决模块

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'
          }
        ]
      }
    }
  },
  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()})
    }
  }

来不及了,今天再持续~

正文完
 0