list.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <div class="app-container">
  3. <el-page-header v-if="parentTitle" :content="parentTitle + ' 下的子菜单'" @back="goBack" />
  4. <div class="filter-container">
  5. <el-select v-model="listQuery.status" style="width: 140px" class="filter-item" @change="handleFilter">
  6. <el-option v-for="item in statusOptions" :key="item.key" :label="item.label" :value="item.key" />
  7. </el-select>
  8. <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
  9. 添加菜单
  10. </el-button>
  11. </div>
  12. <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width:100%">
  13. <el-table-column label="序号" align="center" width="50">
  14. <template slot-scope="scope">{{ scope.$index + 1 }}</template>
  15. </el-table-column>
  16. <el-table-column label="菜单ID" align="center">
  17. <template slot-scope="{row}">{{ row.id }}</template>
  18. </el-table-column>
  19. <el-table-column label="菜单名称" align="center">
  20. <template slot-scope="{row}">{{ row.title }}</template>
  21. </el-table-column>
  22. <el-table-column label="路由名称" align="center">
  23. <template slot-scope="{row}">{{ row.name }}</template>
  24. </el-table-column>
  25. <el-table-column label="前端图标" width="80" align="center">
  26. <template slot-scope="{row}">
  27. <i v-if="isElementIcon(row.icon)" :class="row.icon" />
  28. <svg-icon v-else-if="row.icon" :icon-class="row.icon" />
  29. </template>
  30. </el-table-column>
  31. <el-table-column label="状态" width="80" align="center">
  32. <template slot-scope="{row}">
  33. <el-switch v-model="row.status" :active-value="0" :inactive-value="1" active-color="#1890ff" inactive-color="#DCDFE6" @change="handleHiddenChange($index, row)" />
  34. </template>
  35. </el-table-column>
  36. <el-table-column label="排序" width="80" align="center">
  37. <template slot-scope="{row}">
  38. <el-input v-model="row.sort" maxlength="4" minlength="1" @blur="handleOnInputBlur(row)" />
  39. </template>
  40. </el-table-column>
  41. <el-table-column label="设置" width="250" align="center">
  42. <template slot-scope="{row}">
  43. <el-button plain size="mini" :disabled="row.childCount | disableNextLevel" @click="handleShowNextLevel(row)">查看子菜单</el-button>
  44. <el-button plain size="mini" @click="handleCreateNextLevel(row)">添加子菜单</el-button>
  45. </template>
  46. </el-table-column>
  47. <el-table-column label="操作" width="150" align="center">
  48. <template slot-scope="{row}">
  49. <el-button size="mini" type="primary" @click="handleUpdate(row)">编辑
  50. </el-button>
  51. <el-button size="mini" type="danger" @click="handleDelete(row)">删除
  52. </el-button>
  53. </template>
  54. </el-table-column>
  55. </el-table>
  56. <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
  57. </div>
  58. </template>
  59. <script>
  60. import { fetchList, deleteMenu, updateSelective } from '@/api/sys/menu'
  61. import Pagination from '@/components/Pagination'
  62. export default {
  63. name: 'SysMenuList',
  64. components: { Pagination },
  65. filters: {
  66. disableNextLevel(value) {
  67. if (value === 0) {
  68. return true
  69. } else {
  70. return false
  71. }
  72. }
  73. },
  74. data() {
  75. return {
  76. tableKey: 0,
  77. list: [],
  78. total: 5,
  79. listLoading: true,
  80. parentTitle: '',
  81. listQuery: {
  82. pageNum: 1,
  83. pageSize: 10,
  84. parentId: '',
  85. status: ''
  86. },
  87. statusOptions: [
  88. { label: '状态', key: '' },
  89. { label: '启用', key: '0' },
  90. { label: '停用', key: '1' }
  91. ]
  92. }
  93. },
  94. watch: {
  95. $route(route) {
  96. this.resetParentId()
  97. this.getList()
  98. }
  99. },
  100. created() {
  101. this.resetParentId()
  102. this.getList()
  103. },
  104. methods: {
  105. isElementIcon(value) {
  106. return value && value.substr(0, 7) === 'el-icon'
  107. },
  108. resetParentId() {
  109. this.listQuery.pageNum = 1
  110. if (this.$route.query.parentId) {
  111. this.listQuery.parentId = this.$route.query.parentId
  112. this.parentTitle = this.$route.query.title
  113. } else {
  114. this.listQuery.parentId = ''
  115. this.parentTitle = ''
  116. }
  117. },
  118. handleFilter() {
  119. this.listQuery.pageNum = 1
  120. this.getList()
  121. },
  122. getList() {
  123. this.listLoading = true
  124. fetchList(this.listQuery).then(response => {
  125. this.listLoading = false
  126. this.list = response.data.results
  127. this.total = response.data.totalRecord
  128. // Just to simulate the time of the request
  129. setTimeout(() => {
  130. this.listLoading = false
  131. }, 1.5 * 1000)
  132. })
  133. },
  134. goBack() {
  135. // 调用全局挂载的方法,关闭当前标签页
  136. this.$store.dispatch('tagsView/delView', this.$route)
  137. // 返回上一步路由,返回上一个标签页
  138. this.$router.go(-1)
  139. },
  140. handleCreate() {
  141. this.$router.push({ path: '/sys/menus/add', query: { parentId: this.listQuery.parentId, title: this.parentTitle }})
  142. },
  143. handleUpdate(row) {
  144. this.$router.push({ path: '/sys/menus/edit', query: { id: row.id, parentId: this.listQuery.parentId, title: this.parentTitle }})
  145. },
  146. handleShowNextLevel(row) {
  147. this.$router.push({ path: '/sys/menus/list', query: { parentId: row.id, title: row.title }})
  148. },
  149. handleCreateNextLevel(row) {
  150. this.$router.push({ path: '/sys/menus/add', query: { parentId: row.id, title: row.title }})
  151. },
  152. handleDelete(row) {
  153. this.$confirm('是否要删除该菜单', '提示', {
  154. confirmButtonText: '确定',
  155. cancelButtonText: '取消',
  156. type: 'warning'
  157. }).then(() => {
  158. deleteMenu(row.id).then(response => {
  159. this.$message({
  160. message: '删除成功',
  161. type: 'success',
  162. duration: 1000
  163. })
  164. this.getList()
  165. })
  166. })
  167. },
  168. handleOnInputBlur(row) {
  169. // 更新排序
  170. updateSelective(row.id, { sort: row.sort }).then(response => {
  171. this.$message({
  172. message: '操作成功',
  173. type: 'success',
  174. duration: 1000
  175. })
  176. this.getList()
  177. })
  178. },
  179. handleHiddenChange(index, row) {
  180. // 操作开关
  181. updateSelective(row.id, { status: row.status }).then(response => {
  182. this.$message({
  183. message: '操作成功',
  184. type: 'success',
  185. duration: 1000
  186. })
  187. this.getList()
  188. })
  189. },
  190. handleFilterStatus(status) {
  191. console.log('status', status)
  192. const defObj = { msg: '', status: 0 }
  193. if (status === 0) {
  194. defObj.msg = '是否要停用该菜单?'
  195. defObj.status = 1
  196. } else {
  197. defObj.msg = '是否要启用该菜单?'
  198. defObj.status = 0
  199. }
  200. return defObj
  201. }
  202. }
  203. }
  204. </script>
  205. <style scoped>
  206. </style>