index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <template>
  2. <div class="staff-list-container">
  3. <!-- 顶部搜索 -->
  4. <div class="header">
  5. <el-input
  6. v-model="content"
  7. placeholder="搜索姓名"
  8. clearable
  9. @input="handleInputSearch"
  10. @clear="clearContent"
  11. />
  12. <el-button class="search" @click="searchServer(content)">搜索</el-button>
  13. <el-button type="primary" class="add" @click="dialogFormVisible = true"> 新增员工</el-button>
  14. </div>
  15. <!-- 表格数据展示区 -->
  16. <el-table
  17. ref="multipleTable"
  18. :data="list"
  19. tooltip-effect="dark"
  20. :header-cell-style="{background:'#fafafa',color:'#000', fontWeight: 700, borderTop: '1px solid #ebeef5'}"
  21. style="width: 100%"
  22. @selection-change="handleSelectionChange"
  23. >
  24. <el-table-column
  25. type="selection"
  26. width="55"
  27. />
  28. <el-table-column
  29. type="index"
  30. label="编号"
  31. width="50"
  32. />
  33. <el-table-column
  34. label="姓名"
  35. prop="username"
  36. />
  37. <el-table-column
  38. label="职位"
  39. prop="positionName"
  40. />
  41. <el-table-column
  42. label="联系电话"
  43. prop="telephone"
  44. />
  45. <el-table-column
  46. label="入职时间"
  47. prop="entryTime"
  48. >
  49. <template #default="scope">
  50. {{ scope.row.entryTime | timeFormat('YYYY-MM-DD') }}
  51. </template>
  52. </el-table-column>
  53. <el-table-column
  54. label="在职状态"
  55. prop="status"
  56. >
  57. <template #default="scope">
  58. <el-tag v-if="scope.row.status === 1" type="primary">在职</el-tag>
  59. <el-tag v-if="scope.row.status === 2" type="danger">离职</el-tag>
  60. </template>
  61. </el-table-column>
  62. <el-table-column
  63. label="操作"
  64. width="200"
  65. align="center"
  66. >
  67. <template #default="scope">
  68. <el-button type="text" :style="{color: '#1990FF'}" @click="showUpdate(scope.row)">编辑</el-button>
  69. <el-divider direction="vertical" /> <!-- 中间的小竖线 -->
  70. <el-button type="text" :style="{color: '#E51C23'}" @click="del(scope.row.id)">删除</el-button>
  71. </template>
  72. </el-table-column>
  73. </el-table>
  74. <!-- 分页模块 -->
  75. <pagination
  76. v-show="totalpages>0"
  77. :total="totalrecords"
  78. :page.sync="currentPage"
  79. layout="total, prev, pager, next, jumper"
  80. @pagination="handlePaginate"
  81. />
  82. <!-- 添加服务器信息dialog -->
  83. <el-dialog
  84. :visible.sync="dialogFormVisible"
  85. width="600px"
  86. :close-on-click-modal="false"
  87. @close="close('addStaffForm')"
  88. >
  89. <el-form ref="addStaffForm" :model="userFormData" label-width="140px" :rules="rules" label-position="right">
  90. <el-form-item label="姓名" prop="username">
  91. <el-input v-model="userFormData.username" />
  92. </el-form-item>
  93. <el-form-item label="联系方式" prop="telephone">
  94. <el-input v-model="userFormData.telephone" />
  95. </el-form-item>
  96. <el-form-item label="登录名" prop="account">
  97. <el-input v-model="userFormData.account" />
  98. </el-form-item>
  99. <el-form-item label="密码" prop="password">
  100. <el-input v-model="userFormData.password" />
  101. </el-form-item>
  102. <el-form-item label="职位" prop="positionId">
  103. <el-select v-model.number="userFormData.positionId" clearable placeholder="请选择职位" style="width: 320px">
  104. <el-option
  105. v-for="item in staffList"
  106. :key="item.id"
  107. :label="item.positionName"
  108. :value="item.id"
  109. />
  110. </el-select>
  111. </el-form-item>
  112. <el-form-item label="入职时间" prop="entryTime">
  113. <el-date-picker
  114. v-model="userFormData.entryTime"
  115. type="datetime"
  116. placeholder="选择入职时间"
  117. />
  118. </el-form-item>
  119. <el-form-item label="在职状态" prop="status">
  120. <el-select v-model.number="userFormData.status" clearable placeholder="请选择在职状态" style="width: 320px">
  121. <el-option
  122. v-for="item in statusList"
  123. :key="item.value"
  124. :label="item.label"
  125. :value="item.value"
  126. />
  127. </el-select>
  128. </el-form-item>
  129. <el-form-item label="备注" prop="remark">
  130. <el-input v-model="userFormData.remark" type="textarea" />
  131. </el-form-item>
  132. <el-form-item>
  133. <el-button @click="close('addStaffForm')">取 消</el-button>
  134. <el-button type="primary" @click="submitForm('addStaffForm')">确 认</el-button>
  135. </el-form-item>
  136. </el-form>
  137. </el-dialog>
  138. </div>
  139. </template>
  140. <script>
  141. import { getStaffList, addStaff, delStaff, updateStaff, getAllUserPosition } from '@/api/staff'
  142. import Pagination from '@/components/Pagination/index'
  143. export default {
  144. name: 'StaffList',
  145. components: {
  146. Pagination
  147. },
  148. data() {
  149. return {
  150. // 搜索关键词
  151. content: '',
  152. // 服务器列表数据
  153. list: [],
  154. // 总条数
  155. totalrecords: 0,
  156. // 是否是新增
  157. isAdd: true,
  158. // 当前页
  159. currentPage: 1,
  160. // 总页数
  161. totalpages: 0,
  162. // 控制dialog显示和隐藏
  163. dialogFormVisible: false,
  164. // 用户和职位信息
  165. staffList: [],
  166. // 在职状态列表
  167. statusList: [
  168. {
  169. label: '在职',
  170. value: 1
  171. },
  172. {
  173. label: '离职',
  174. value: 2
  175. }
  176. ],
  177. // 用户信息
  178. userFormData: {
  179. account: '',
  180. entryTime: '',
  181. id: 0,
  182. password: '',
  183. positionId: '',
  184. remark: '',
  185. status: 1,
  186. telephone: '',
  187. username: ''
  188. },
  189. // 服务器信息表单校验规则
  190. rules: {
  191. username: [
  192. {
  193. required: true,
  194. message: '姓名不能为空'
  195. }
  196. ],
  197. account: [
  198. {
  199. required: true,
  200. message: '登录名不能为空'
  201. }
  202. ],
  203. password: [
  204. {
  205. required: true,
  206. message: '密码不能为空'
  207. }
  208. ],
  209. positionId: [
  210. {
  211. required: true,
  212. message: '职位不能为空'
  213. }
  214. ]
  215. }
  216. }
  217. },
  218. created() {
  219. this.fetchList({ content: this.content })
  220. this.fetchAllUserPosition()
  221. },
  222. methods: {
  223. handleSelectionChange() {
  224. console.log('handleSelectionChange')
  225. },
  226. // 获取服务器列表数据
  227. async fetchList(params) {
  228. const res = await getStaffList(params)
  229. const { list, totalrecords, totalpages } = res
  230. // console.log(list)
  231. // console.log(res)
  232. this.list = list
  233. this.totalrecords = totalrecords
  234. this.totalpages = totalpages
  235. },
  236. // 获取所有用户和职位信息
  237. async fetchAllUserPosition() {
  238. const { list } = await getAllUserPosition()
  239. this.staffList = list
  240. },
  241. // 处理分页模块
  242. handlePaginate(params) {
  243. // console.log(params)
  244. params = Object.assign(params, { content: this.content })
  245. this.fetchList(params)
  246. },
  247. // 显示更新服务器dialog
  248. showUpdate(item) {
  249. // console.log(item)
  250. this.userFormData = item
  251. this.dialogFormVisible = true
  252. // 修改为更新状态
  253. this.isAdd = false
  254. },
  255. // 根据id删除服务器数据
  256. del(id) {
  257. // 弹出提示框,询问是否确认删除
  258. this.$confirm('你将要删除一条数据, 是否继续?', '温馨提示', {
  259. confirmButtonText: '确定',
  260. cancelButtonText: '取消',
  261. type: 'error'
  262. }).then(async() => {
  263. // 发送删除服务器信息请求
  264. await delStaff(id)
  265. this.$message({
  266. type: 'success',
  267. message: '删除成功!'
  268. })
  269. await this.fetchList({ page: this.currentPage, content: this.content })
  270. // 删除完后,如果当前页没有数据并且当前页不是第一页,那么就重新请求上一页的数据
  271. if (this.list.length === 0 && this.currentPage > 1) {
  272. this.currentPage -= 1
  273. }
  274. await this.fetchList({ page: this.currentPage, content: this.content })
  275. }).catch(() => {
  276. this.$message({
  277. type: 'info',
  278. message: '已取消删除'
  279. })
  280. })
  281. },
  282. // 搜索服务器数据
  283. searchServer(content) {
  284. // console.log(content)
  285. this.fetchList({ content })
  286. },
  287. async handleInputSearch() {
  288. // 当用户清除了搜索框中的文字时或者搜索框内容为空时,重新发送请求获取服务器列表数据
  289. if (!this.content) {
  290. await this.fetchList()
  291. }
  292. },
  293. // dialog关闭时要做的事
  294. close(formName) {
  295. // 隐藏dialog
  296. this.dialogFormVisible = false
  297. this.isAdd = true
  298. // 清空userFormData
  299. this.userFormData = {}
  300. // 清除校验的效果
  301. this.$refs[formName].resetFields()
  302. },
  303. // 当搜索框点击清除按钮时
  304. clearContent() {
  305. // console.log(this.content)
  306. // 让当前的页码为1
  307. this.currentPage = 1
  308. this.fetchList({ content: this.content })
  309. },
  310. submitForm(formName) {
  311. // 校验表单
  312. this.$refs[formName].validate(async(valid) => {
  313. // 通过就发送请求
  314. if (valid) {
  315. let res
  316. if (this.isAdd) {
  317. res = await addStaff(this.userFormData)
  318. } else {
  319. res = await updateStaff(this.userFormData)
  320. }
  321. // console.log(res)
  322. // 提示添加成功
  323. this.$message.success(res.msg)
  324. // 重新获取服务器列表的数据
  325. await this.fetchList({ page: this.currentPage, content: this.content })
  326. // 隐藏dialog
  327. this.dialogFormVisible = false
  328. } else {
  329. // 不通过就停止发送请求
  330. console.log('error submit!!')
  331. return false
  332. }
  333. })
  334. }
  335. }
  336. }
  337. </script>
  338. <style lang="scss" scoped>
  339. .staff-list-container {
  340. padding-left: 30px;
  341. .header {
  342. padding-top: 30px;
  343. margin-bottom: 15px;
  344. ::v-deep .el-input {
  345. width: 300px;
  346. }
  347. .search {
  348. margin-left: 15px;
  349. }
  350. .add {
  351. float: right;
  352. margin-right: 30px;
  353. }
  354. }
  355. ::v-deep .el-pagination {
  356. display: flex;
  357. justify-content: flex-end;
  358. }
  359. }
  360. </style>