index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <template>
  2. <div>
  3. <div class="filter-container">
  4. <slot name="filter-content" />
  5. <el-row>
  6. <el-col>
  7. <el-button v-if="options.addRoute" type="primary" icon="el-icon-plus" @click="handleAdd">添加</el-button>
  8. <el-button v-permission="[options.add.permission]" v-if="options.add && options.add.enable" size="small" type="primary" icon="el-icon-plus" @click="optAdd">添加</el-button>
  9. <el-button v-permission="[options.edit.permission]" v-if="options.edit && options.edit.enable" :disabled="selectedIds.length!==1 " size="small" type="warning" icon="el-icon-edit" @click="optEdit">修改</el-button>
  10. <el-button v-permission="[options.delete.permission]" v-if="options.delete && options.delete.enable" :disabled="selectedIds.length===0 " size="small" type="danger" icon="el-icon-delete" @click="optDelete">删除</el-button>
  11. <el-dropdown
  12. v-if="options.multiActions && options.multiActions.length>0 && selectedIds.length>0"
  13. size="small"
  14. style="margin-left: 10px"
  15. split-button
  16. @command="optAction"
  17. >
  18. 批量操作
  19. <el-dropdown-menu slot="dropdown">
  20. <el-dropdown-item v-for="item in options.multiActions" :key="item.id" :command="item">{{ item.label }}</el-dropdown-item>
  21. </el-dropdown-menu>
  22. </el-dropdown>
  23. </el-col>
  24. </el-row>
  25. </div>
  26. <el-table
  27. v-loading="listLoading"
  28. ref="table"
  29. :data="dataList.records"
  30. :row-key="getRowKeys"
  31. :header-cell-style="{'background':'#f2f3f4', 'color':'#555', 'font-weight':'bold', 'line-height':'32px'}"
  32. border
  33. fit
  34. highlight-current-row
  35. @sort-change="tableSortChange"
  36. @selection-change="handleSelection"
  37. >
  38. <el-table-column
  39. v-if="options.multi"
  40. :reserve-selection="true"
  41. align="center"
  42. type="selection"
  43. width="55"
  44. />
  45. <slot name="data-columns" />
  46. </el-table>
  47. <pagination v-show="dataList.total>0" :total="dataList.total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getList" />
  48. </div>
  49. </template>
  50. <script>
  51. import { fetchList, deleteData, changeState } from '@/api/common'
  52. import Pagination from '@/components/Pagination'
  53. import permission from '@/directive/permission/index.js' // 权限判断指令
  54. export default {
  55. name: 'PagingTable',
  56. directives: { permission },
  57. components: { Pagination },
  58. // 组件入参
  59. props: {
  60. options: {
  61. type: Object,
  62. default: () => {
  63. return {
  64. keyId: 'id',
  65. add: {
  66. enable: false,
  67. permission: '',
  68. router: {}
  69. },
  70. edit: {
  71. enable: false,
  72. permission: '',
  73. router: {}
  74. },
  75. delete: {
  76. enable: false,
  77. permission: '',
  78. url: ''
  79. },
  80. // 批量操作
  81. multiActions: [],
  82. // 列表请求URL
  83. listUrl: '/api/',
  84. // 删除请求URL
  85. deleteUrl: '',
  86. // 启用禁用
  87. stateUrl: '',
  88. // 可批量操作
  89. multi: false
  90. }
  91. }
  92. },
  93. // 列表查询参数
  94. listQuery: {
  95. type: Object,
  96. default: () => {
  97. return {
  98. current: 1,
  99. size: 10,
  100. params: {},
  101. t: 0
  102. }
  103. }
  104. }
  105. },
  106. data() {
  107. return {
  108. // 接口数据返回
  109. dataList: {
  110. total: 0
  111. },
  112. // 数据加载标识
  113. listLoading: true,
  114. // 选定和批量操作
  115. selectedIds: [],
  116. selectedObjs: [],
  117. // 显示已中多少项
  118. selectedLabel: '',
  119. // 显示批量操作
  120. multiShow: false,
  121. // 批量操作的标识
  122. multiNow: ''
  123. }
  124. },
  125. watch: {
  126. // 检测查询变化
  127. listQuery: {
  128. handler() {
  129. this.getList()
  130. },
  131. deep: true
  132. }, // 检测判定弹框类嵌套组件时, 激活时清空表格选择项
  133. '$parent.$parent.dialogVisible': {
  134. handler(val) {
  135. if (val) {
  136. this.clearSelection()
  137. }
  138. },
  139. deep: true
  140. }
  141. },
  142. created() {
  143. // 创建时 清空选中的所有项
  144. this.selectedIds = []
  145. this.selectedObjs = []
  146. this.getList()
  147. },
  148. methods: {
  149. // 获取选定的key
  150. getRowKeys(row) {
  151. if (this.options.keyId != null && this.options.keyId !== '') {
  152. return row[this.options.keyId]
  153. }
  154. return row['id']
  155. },
  156. // 添加
  157. optAdd() {
  158. if (!this.options.add || !this.options.add.enable) {
  159. return
  160. }
  161. // 跳转的
  162. if (this.options.add.router) {
  163. this.$router.push(this.options.add.router)
  164. }
  165. this.$emit('add')
  166. },
  167. optEdit() {
  168. if (!this.options.edit || !this.options.edit.enable) {
  169. return
  170. }
  171. // 跳转的
  172. if (this.options.edit.router) {
  173. const router = this.options.edit.router
  174. const params = router.params
  175. params.id = this.selectedIds[0]
  176. router.params = params
  177. this.$router.push(router)
  178. }
  179. this.$emit('edit', this.selectedIds[0], this.selectedObjs[0])
  180. },
  181. optDelete() {
  182. if (!this.options.delete || !this.options.delete.enable) {
  183. return
  184. }
  185. if (this.options.delete.url) {
  186. // 删除
  187. this.$confirm('确实要删除吗?', '提示', {
  188. confirmButtonText: '确定',
  189. cancelButtonText: '取消',
  190. type: 'warning'
  191. }).then(() => {
  192. deleteData(this.options.delete.url, this.selectedIds).then(() => {
  193. this.$message({
  194. type: 'success',
  195. message: '删除成功!'
  196. })
  197. this.getList()
  198. })
  199. })
  200. }
  201. this.$emit('delete', this.selectedIds)
  202. },
  203. // 批处理回调
  204. optAction(item) {
  205. console.log('执行', item)
  206. if (item.value === 'enable') {
  207. // 修改状态
  208. changeState(item.url, this.selectedIds, 0).then(response => {
  209. if (response.code === 0) {
  210. this.$message({
  211. type: 'success',
  212. message: '状态修改成功!'
  213. })
  214. // 重新搜索
  215. this.getList()
  216. }
  217. })
  218. return
  219. }
  220. if (item.value === 'disable') {
  221. // 修改状态
  222. changeState(item.url, this.selectedIds, 1).then(response => {
  223. if (response.code === 0) {
  224. this.$message({
  225. type: 'success',
  226. message: '状态修改成功!'
  227. })
  228. // 重新搜索
  229. this.getList()
  230. }
  231. })
  232. return
  233. }
  234. // 向外回调的操作
  235. this.$emit('multi-actions', { opt: item.value, ids: this.selectedIds, objs: this.selectedObjs })
  236. },
  237. /**
  238. * 添加数据跳转
  239. */
  240. handleAdd() {
  241. if (this.options.addRoute) {
  242. this.$router.push({ name: this.options.addRoute, params: {}})
  243. return
  244. }
  245. console.log('未设置添加数据跳转路由!')
  246. },
  247. /**
  248. * 查询数据列表
  249. */
  250. getList() {
  251. this.listLoading = true
  252. this.listQuery.t = new Date().getTime()
  253. fetchList(this.options.listUrl, this.listQuery).then(response => {
  254. this.dataList = response.data
  255. this.listLoading = false
  256. })
  257. },
  258. /**
  259. * 搜索
  260. */
  261. handleFilter() {
  262. // 重新搜索
  263. this.getList()
  264. },
  265. /**
  266. * 批量操作回调
  267. */
  268. handleOption(v) {
  269. this.multiNow = ''
  270. // 内部消化的操作
  271. if (v === 'delete') {
  272. this.handleDelete()
  273. return
  274. }
  275. if (v === 'enable') {
  276. this.handleState(0)
  277. return
  278. }
  279. if (v === 'disable') {
  280. this.handleState(1)
  281. return
  282. }
  283. // 向外回调的操作
  284. this.$emit('multi-actions', { opt: v, ids: this.selectedIds })
  285. },
  286. /**
  287. * 修改状态,启用禁用
  288. */
  289. handleState(state) {
  290. // 修改状态
  291. changeState(this.options.stateUrl, this.selectedIds, state).then(response => {
  292. if (response.code === 0) {
  293. this.$message({
  294. type: 'success',
  295. message: '状态修改成功!'
  296. })
  297. // 重新搜索
  298. this.getList()
  299. }
  300. })
  301. },
  302. /**
  303. * 删除数据
  304. */
  305. handleDelete() {
  306. if (this.selectedIds.length === 0) {
  307. this.$message({
  308. message: '请至少选择一条数据!',
  309. type: 'warning'
  310. })
  311. return
  312. }
  313. // 删除
  314. this.$confirm('确实要删除吗?', '提示', {
  315. confirmButtonText: '确定',
  316. cancelButtonText: '取消',
  317. type: 'warning'
  318. }).then(() => {
  319. deleteData(this.options.deleteUrl, this.selectedIds).then(() => {
  320. this.$message({
  321. type: 'success',
  322. message: '删除成功!'
  323. })
  324. this.getList()
  325. })
  326. })
  327. },
  328. /**
  329. * 列表多选操作
  330. * @param val
  331. */
  332. handleSelection(val) {
  333. const ids = []
  334. val.forEach(row => {
  335. ids.push(row.id)
  336. })
  337. this.selectedObjs = val
  338. this.selectedIds = ids
  339. this.multiShow = ids.length > 0
  340. this.selectedLabel = '已选' + ids.length + '项'
  341. this.$emit('select-changed', { ids: this.selectedIds, objs: this.selectedObjs })
  342. },
  343. tableSortChange(column) {
  344. this.listQuery.pageNo = 1
  345. if (column.order === 'descending') {
  346. this.listQuery.orders = [{ column: column.prop, asc: false }]
  347. } else {
  348. this.listQuery.orders = [{ column: column.prop, asc: true }]
  349. }
  350. this.getList()
  351. },
  352. // 清理选择的
  353. clearSelection() {
  354. this.selectedIds = []
  355. this.selectedObjs = []
  356. this.$refs.table.clearSelection()
  357. }
  358. }
  359. }
  360. </script>
  361. <style scoped>
  362. ::v-deep
  363. .filter-container .filter-item{
  364. margin-left: 10px;
  365. }
  366. ::v-deep
  367. .filter-container .filter-item:first-child{
  368. margin-left: 0px;
  369. }
  370. </style>