1+ /**
2+ * 将table组件公共操作(删除,查询,分页查询)封装到mixin
3+ */
4+ import Vue from 'vue'
5+ import { Component } from "vue-property-decorator"
6+ import { Pagination } from "@/utils/const"
7+ import { Modal , message } from "ant-design-vue"
8+
9+ @Component
10+ export default class TableMixin extends Vue {
11+ public tableData = [ ] //table列表数据
12+ public loading : boolean = false //加载loading
13+ public selectedRowKeys : ( string | number ) [ ] = [ ] //勾选项key
14+ public selectedRows : any = [ ] //勾选项
15+ public mixinOptions : StoreState . TableMixinOptions = { //公共操作请求api方法
16+ queryTableApi : ( ) => { } ,
17+ deleteApi : ( ) => { }
18+ }
19+ public pagination : StoreState . Pagination = new Pagination ( { } ) . init ( ) ; //分页
20+ public queryData : StoreState . TableQueryOptions = { //table列表请求参数
21+ pageNo : 1 ,
22+ pageSize : 10
23+ } ;
24+
25+ //inital methods
26+
27+ //查询列表
28+ public async query ( ) {
29+ this . loading = true
30+ try {
31+ let { data } = await this . mixinOptions [ 'queryTableApi' ] ( this . queryData )
32+ this . tableData = data [ 'list' ]
33+ this . pagination . current = data [ "pageNum" ] ;
34+ this . pagination . total = data [ 'total' ]
35+ this . loading = false
36+ } catch ( error ) {
37+ this . loading = false
38+ }
39+ }
40+ //分页查询
41+ public tableChange ( pagination : StoreState . Pagination ) {
42+ this . queryData . pageNo = pagination . current as number
43+ this . query ( )
44+ }
45+ /**
46+ * 批量删除
47+ * @param deleteParams //api参数
48+ */
49+ public batchHandleDetele ( deleteParams : any ) {
50+ if ( ! this . selectedRows . length ) {
51+ message . warning ( "请选择要删除的数据!" )
52+ return
53+ }
54+ this . handleDetele ( deleteParams )
55+ }
56+ //删除确认
57+ public handleDetele ( deleteParams : any ) {
58+ Modal . confirm ( {
59+ title : "确定进行[删除]操作?" ,
60+ okText : "确定" ,
61+ class : "my-modal" ,
62+ cancelText : "取消" ,
63+ onOk : async ( ) => {
64+ this . onDetele ( deleteParams )
65+ } ,
66+ onCancel : ( ) => {
67+ console . log ( "Cancel" ) ;
68+ }
69+ } ) ;
70+ }
71+ //开始删除
72+ public async onDetele ( deleteParams : any ) {
73+ await this . mixinOptions [ 'deleteApi' ] ( deleteParams ) ;
74+ let pageNo = this . queryData . pageNo ;
75+ if ( this . tableData . length == 1 ) {
76+ this . queryData . pageNo = pageNo - 1 <= 0 ? 1 : pageNo - 1 ;
77+ }
78+ this . query ( )
79+ }
80+ //勾选
81+ public onSelectChange ( selectedRowKeys : Array < string | number > , selectedRows : any ) {
82+ console . log ( "selectedRowKeys changed: " , selectedRowKeys ) ;
83+ this . selectedRowKeys = selectedRowKeys ;
84+ this . selectedRows = selectedRows
85+ }
86+
87+ mounted ( ) {
88+ this . query ( )
89+ }
90+ }
0 commit comments