List.vue 6.43 KB
Newer Older
liuxin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<template>
  <div class="m-table-list">

    <slot name="operation"></slot>
    <div>
      <div class="m-table-list-selection" v-if="multipleSelect">
        <span class="desc">已选中{{isSelectAll? listConfig.total: count}}条/共{{listConfig.total}}</span>
        <c-button type="text" @click="handleSelectAll">{{isSelectAll?'取消全选': '全选'}}</c-button>
      </div>
      <c-table
        v-bind="listConfig"
        :loading="loading"
        ref="tablelist"
        :default-sort="listConfig.sort ? {prop: listConfig.sort, order: listConfig.order}: {}"
        @selection-change="handleSelectionChange"
        @current-change="handleCurrentChange"
        @sort-change="handleSortChange"
		    v-on="$listeners"
        :tableRowClassName="tableRowClass"
        :maxHeight="maxHeight"
        :outerHeight="outerHeight"
        :row-key="getRowKey"
      >
        <slot></slot>
      </c-table>
    </div>
  </div>
</template>

<script>
wangren committed
31
import request from '~/utils/request'
liuxin committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

export default {
  props: {
    columnsConfig: { // 列配置项
      type: Array,
      default: []
    },
    params: { // 搜索查询参数
      type: Object,
      default: ()=>{}
    },
    pageSize: { // 每页限制展示数目
      type: Number,
      default: 20
    },
    url: { // 拉取列表接口名称
      type: String
    },
    method: { // 拉取列表接口方法,默认 post
      type: String,
      default: 'post'
    },
    defaultSort: { // 默认首次加载排序字段名称
      type: String,
      default: ""
    },
    defaultOrder: { // 默认首次加载排序顺序
      type: String,
      default: "ascending"
    },
    multipleSelect: { // 是否要展示多项选择框
      type: Boolean,
      default: false
    },
    loadDataFirstRender: {
      type: Boolean,
      default: true
    },
    tableRowClassName: {
      type: Function
    },
	  watchCurrentChg:{
	    type: Boolean,
	    default: true
    },
    maxHeight:{
      type:String,
      default:undefined
    },
    outerHeight: {
      type: Number,
      default: undefined
    },

	},
  data(){
    return{
      listConfig: {
        list: [],
        total: 0,
        current: 1,
        limit: this.pageSize,
        columnsConfig: this.columnsConfig,
        sort: this.defaultSort,
        order: this.defaultOrder,
      },
      count: 0,
      isSelectAll: false,
      selection: [],
      loading: false,
      isMounted: false
    }
  },
  computed: {
    'searchParams': function () {
      console.log('computed searchParams')
	  let params = {}
	  if(this.watchCurrentChg){
		params = {
          current: this.listConfig.current,
          size: this.pageSize,
          data: this.params
        }
	  }else{
	    params = {
          size: this.pageSize,
          data: this.params
        }
	  }
      if (this.listConfig.sort && this.listConfig.order) {
        params.orders = [
          {
            column: this.listConfig.sort,
            asc: this.listConfig.order == 'ascending'
          }
        ]
      }
      return params
    }
  },
  watch: {
    'params': function () {
      // 更改搜索条件需要重置为 0
      this.listConfig.current = 1
    },
    'searchParams': function () {
      if (this.isMounted) {
        this.getList()
      }
    }
  },
  created() {
    if (this.multipleSelect &&
      this.listConfig.columnsConfig &&
      this.listConfig.columnsConfig[0].type !== 'selection') {
      this.listConfig.columnsConfig.unshift({
        type: "selection",
        width: "55",
        fixed: "left"
      })
    }
    
  },
  mounted () {
    if (this.loadDataFirstRender) {
      this.getList()
    }
    this.isMounted = true
  },
  methods: {
    handleSelectionChange: function (selection) {
      this.count = selection.length
      this.selection = selection
      this.isSelectAll = false
      this.$emit('selection-change', selection)
    },
    handleCurrentChange: function (current) {
      this.listConfig.current = current
      this.$emit('current-change',current)
    },
    handleSortChange: function ({column, prop, order}) {
      this.listConfig.sort = column.columnKey || prop
      if (order == 'ascending') {
        this.listConfig.order = 'ascending'
      } else {
        this.listConfig.order = 'descending'
      }
      this.listConfig.current = 1
    },
    getList() {
      if (this.url && this.method == 'post') {
        let self = this
        self.loading = true
        Request.post(this.url, this.searchParams).then(res => {
          if (res.code === SUCCESS) {
			  
            self.listConfig.list = res.data || []
            self.listConfig.total = res.pageInfo && res.pageInfo.total || 0
            self.$emit('update-list', self.listConfig.list, self.listConfig.total, res)
          } else {
            self.listConfig.list = []
            self.listConfig.total = 0
          }
          self.loading = false
        }).catch(err=> {
          self.loading = false
        })
      }
    },
    handleSelectAll: function() {
      let currentSelectAll = !this.isSelectAll
      if (currentSelectAll) {
        // 全选
        this.$refs["tablelist"].$refs["table"].clearSelection()
        this.listConfig.list.forEach(row => {
          this.$refs["tablelist"].$refs["table"].toggleRowSelection(row)
        })
        this.selection = []
        this.count = this.listConfig.total
        this.$emit('selection-change', this.selection, currentSelectAll)
      } else {
        // 取消全选
        this.$refs["tablelist"].$refs["table"].clearSelection()
        this.selection = []
        this.count = 0
        this.$emit('selection-change', this.selection, currentSelectAll)
      }
      this.isSelectAll = currentSelectAll
    },
    tableRowClass: function ({row, rowIndex}) {
      if (this.tableRowClassName) {
        return this.tableRowClassName({row, rowIndex})
      } else {
        return ''
      }
    },
    getRowKey(row){
      let key = null
      this.$emit('getRowKey',row,val => {key = val})
      return key
    }
  },
}
</script>

<style>
.eContainer-table-block .paginationLable{
  font-size: 12px;
  color: #808080;
  height: 26px;
  line-height: 26px;
  float:right;
liushikai committed
244
  /* margin-top:20px; */
liuxin committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
}
.eContainer-table-block .el-table__body-wrapper {
  overflow: auto;
}
.m-table-list {
  margin-top: 20px;
}
.m-table-list-operation {
  margin-bottom: 15px;
}
.m-table-list-selection .desc{
  font-size: 14px;
  color: #808080;
}
.m-table-search-operation-simple {
  position: absolute;
  right: 20px;
  top: 20px;
}
</style>