<!-- 员工信息列表 -->
<template>
  <div>
    <!-- 搜索框 -->
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input v-model="searchForm.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="searchForm.phone" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">搜索</el-button>
      </el-form-item>
    </el-form>
<pre><code>&lt;!-- 操作按钮 --&gt;
&lt;el-row class=&quot;toolbar&quot;&gt;
  &lt;el-col span=&quot;6&quot;&gt;
    &lt;el-button type=&quot;primary&quot; @click=&quot;add&quot;&gt;新增&lt;/el-button&gt;
    &lt;el-button type=&quot;danger&quot; @click=&quot;batchDelete&quot;&gt;批量删除&lt;/el-button&gt;
  &lt;/el-col&gt;
&lt;/el-row&gt;

&lt;!-- 员工信息表格 --&gt;
&lt;el-table :data=&quot;employeeList&quot; style=&quot;width: 100%&quot; v-loading=&quot;loading&quot;&gt;
  &lt;el-table-column type=&quot;selection&quot; width=&quot;55&quot;&gt;&lt;/el-table-column&gt;
  &lt;el-table-column prop=&quot;name&quot; label=&quot;姓名&quot;&gt;&lt;/el-table-column&gt;
  &lt;el-table-column prop=&quot;phone&quot; label=&quot;手机号&quot;&gt;&lt;/el-table-column&gt;
  &lt;el-table-column prop=&quot;username&quot; label=&quot;用户名&quot;&gt;&lt;/el-table-column&gt;
  &lt;el-table-column prop=&quot;password&quot; label=&quot;密码&quot;&gt;&lt;/el-table-column&gt;
  &lt;el-table-column prop=&quot;gender&quot; label=&quot;性别&quot;&gt;&lt;/el-table-column&gt;
  &lt;el-table-column prop=&quot;idCard&quot; label=&quot;身份证号&quot;&gt;&lt;/el-table-column&gt;
  &lt;el-table-column prop=&quot;type&quot; label=&quot;员工类型&quot;&gt;&lt;/el-table-column&gt;
  &lt;el-table-column label=&quot;操作&quot;&gt;
    &lt;template slot-scope=&quot;scope&quot;&gt;
      &lt;el-button type=&quot;text&quot; @click=&quot;view(scope.row)&quot;&gt;查看&lt;/el-button&gt;
      &lt;el-button type=&quot;text&quot; @click=&quot;edit(scope.row)&quot;&gt;编辑&lt;/el-button&gt;
      &lt;el-button type=&quot;text&quot; @click=&quot;remove(scope.row)&quot;&gt;删除&lt;/el-button&gt;
    &lt;/template&gt;
  &lt;/el-table-column&gt;
&lt;/el-table&gt;

&lt;!-- 分页器 --&gt;
&lt;el-pagination
  background
  :total=&quot;total&quot;
  :page-size=&quot;pageSize&quot;
  layout=&quot;total,prev,pager,next&quot;
  @current-change=&quot;handleCurrentChange&quot;
&gt;&lt;/el-pagination&gt;

&lt;!-- 新增/编辑员工信息弹窗 --&gt;
&lt;el-dialog :visible.sync=&quot;dialogVisible&quot; title=&quot;员工信息&quot; :close-on-click-modal=&quot;false&quot;&gt;
  &lt;el-form ref=&quot;form&quot; :model=&quot;employee&quot; :rules=&quot;rules&quot; label-width=&quot;80px&quot;&gt;
    &lt;el-form-item label=&quot;姓名&quot; prop=&quot;name&quot;&gt;
      &lt;el-input v-model=&quot;employee.name&quot;&gt;&lt;/el-input&gt;
    &lt;/el-form-item&gt;
    &lt;el-form-item label=&quot;手机号&quot; prop=&quot;phone&quot;&gt;
      &lt;el-input v-model=&quot;employee.phone&quot;&gt;&lt;/el-input&gt;
    &lt;/el-form-item&gt;
    &lt;el-form-item label=&quot;用户名&quot; prop=&quot;username&quot;&gt;
      &lt;el-input v-model=&quot;employee.username&quot;&gt;&lt;/el-input&gt;
    &lt;/el-form-item&gt;
    &lt;el-form-item label=&quot;密码&quot; prop=&quot;password&quot;&gt;
      &lt;el-input v-model=&quot;employee.password&quot;&gt;&lt;/el-input&gt;
    &lt;/el-form-item&gt;
    &lt;el-form-item label=&quot;性别&quot; prop=&quot;gender&quot;&gt;
      &lt;el-radio-group v-model=&quot;employee.gender&quot;&gt;
        &lt;el-radio label=&quot;男&quot;&gt;男&lt;/el-radio&gt;
        &lt;el-radio label=&quot;女&quot;&gt;女&lt;/el-radio&gt;
      &lt;/el-radio-group&gt;
    &lt;/el-form-item&gt;
    &lt;el-form-item label=&quot;身份证号&quot; prop=&quot;idCard&quot;&gt;
      &lt;el-input v-model=&quot;employee.idCard&quot;&gt;&lt;/el-input&gt;
    &lt;/el-form-item&gt;
    &lt;el-form-item label=&quot;员工类型&quot; prop=&quot;type&quot;&gt;
      &lt;el-select v-model=&quot;employee.type&quot; placeholder=&quot;请选择&quot;&gt;
        &lt;el-option label=&quot;普通员工&quot; value=&quot;普通员工&quot;&gt;&lt;/el-option&gt;
        &lt;el-option label=&quot;管理员&quot; value=&quot;管理员&quot;&gt;&lt;/el-option&gt;
      &lt;/el-select&gt;
    &lt;/el-form-item&gt;
  &lt;/el-form&gt;
  &lt;div slot=&quot;footer&quot; class=&quot;dialog-footer&quot;&gt;
    &lt;el-button @click=&quot;dialogVisible = false&quot;&gt;取 消&lt;/el-button&gt;
    &lt;el-button type=&quot;primary&quot; @click=&quot;submitForm&quot;&gt;确 定&lt;/el-button&gt;
  &lt;/div&gt;
&lt;/el-dialog&gt;
</code></pre>
  </div>
</template>
<script>
import { getEmployeeList, deleteEmployee, batchDeleteEmployee, addEmployee, updateEmployee } from '@/api/employee'

export default {
  data() {
    return {
      searchForm: {
        name: '',
        phone: ''
      },
      employeeList: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      loading: false,
      dialogVisible: false,
      employee: {
        name: '',
        phone: '',
        username: '',
        password: '',
        gender: '男',
        idCard: '',
        type: '普通员工'
      },
      rules: {
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'blur' }
        ],
        idCard: [
          { required: true, message: '身份证号不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择员工类型', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 搜索
    search() {
      this.currentPage = 1
      this.getEmployeeList()
    },
    // 新增员工信息
    add() {
      this.dialogVisible = true
      this.employee = {
        name: '',
        phone: '',
        username: '',
        password: '',
        gender: '男',
        idCard: '',
        type: '普通员工'
      }
    },
    // 查看员工信息
    view(row) {
      this.dialogVisible = true
      this.employee = Object.assign({}, row)
    },
    // 编辑员工信息
    edit(row) {
      this.dialogVisible = true
      this.employee = Object.assign({}, row)
    },
    // 提交表单
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.employee.id) {
            // 编辑员工信息
            updateEmployee(this.employee).then(() => {
              this.$message.success('编辑成功')
              this.getEmployeeList()
              this.dialogVisible = false
            })
          } else {
            // 新增员工信息
            addEmployee(this.employee).then(() => {
              this.$message.success('新增成功')
              this.getEmployeeList()
              this.dialogVisible = false
            })
          }
        }
      })
    },
    // 删除员工信息
    remove(row) {
      this.$confirm('确定删除该员工信息吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteEmployee(row.id).then(() => {
          this.$message.success('删除成功')
          this.getEmployeeList()
        })
      })
    },
    // 批量删除员工信息
    batchDelete() {
      const ids = this.employeeList.filter(item => item.checked).map(item => item.id)
      if (ids.length === 0) {
        this.$message.warning('请先选择要删除的员工信息')
        return
      }
      this.$confirm('确定删除选中的员工信息吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        batchDeleteEmployee(ids).then(() => {
          this.$message.success('删除成功')
          this.getEmployeeList()
        })
      })
    },
    // 分页器页码改变时触发
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage
      this.getEmployeeList()
    },
    // 获取员工信息列表
    getEmployeeList() {
      this.loading = true
      getEmployeeList({
        name: this.searchForm.name,
        phone: this.searchForm.phone,
        currentPage: this.currentPage,
        pageSize: this.pageSize
      }).then(res => {
        this.employeeList = res.list
        this.total = res.total
        this.loading = false
      })
    }
  },
  mounted() {
    this.getEmployeeList()
  }
}
</script>
用elementUI写一个员工信息列表姓名手机号用户名密码性别身份证号员工类型具有搜索新增查看编辑删除批量删除功能

原文地址: https://www.cveoy.top/t/topic/b5XL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录