Selaa lähdekoodia

Merge branch 'master' of http://git.cc-lotus.info/live/common

guhongwei 4 vuotta sitten
vanhempi
commit
72aa3f06e3

+ 2 - 0
src/components/frame/filter-page-table.md

@@ -11,6 +11,8 @@
 |usePage|Boolean|true|否|是否使用分页|
 |options|Object|null|否|加些属性,不知道能加啥,反正我把合计加上好使了|
 |useSum|Boolean|false|否|使用合计|
+|sumcol|Array|`[]`|否|计算哪一列,就把哪一列的prop写进去|
+|sumres|String|`total`|否|处理每列结果的要求,默认计算总和(total),平均值(avg),最大值(max),最小值(min)|
 |filter|Array|`[]`|否|额外查询|
 |operaWidth|Number|200|否|操作栏宽度|
 

+ 62 - 1
src/components/frame/filter-page-table.vue

@@ -49,12 +49,13 @@
       v-bind="options"
       :show-summary="useSum"
       @row-click="rowClick"
+      :summary-method="computedSum"
     >
       <el-table-column type="selection" width="55" v-if="select" prop="id" :reserve-selection="true"> </el-table-column>
       <template v-for="(item, index) in fields">
         <template v-if="!item.notable">
           <template v-if="item.custom">
-            <el-table-column :key="index" align="center" :label="item.label" v-bind="item.options">
+            <el-table-column :key="index" align="center" :label="item.label" v-bind="item.options" :show-overflow-tooltip="item.showTip || true">
               <template v-slot="{ row }">
                 <slot name="custom" v-bind="{ item, row }"></slot>
               </template>
@@ -145,6 +146,8 @@ export default {
     total: { type: Number, default: 0 },
     options: null,
     useSum: { type: Boolean, default: false },
+    sumcol: { type: Array, default: () => [] },
+    sumres: { type: String, default: 'total' },
     filter: { type: Array, default: () => [] },
     operaWidth: { type: Number, default: 200 },
     limit: { type: Number, default: _.get(this, `$limit`, undefined) !== undefined ? this.$limit : process.env.VUE_APP_LIMIT * 1 || 10 },
@@ -281,6 +284,64 @@ export default {
       let { prop, filterReturn } = item;
       if (filterReturn) this.$emit('filterReturn', { data, prop });
     },
+    // 计算合计
+    computedSum({ columns, data }) {
+      if (columns.length <= 0 || data.length <= 0) return '';
+      const result = [];
+      const reg = new RegExp(/^\d+$/);
+      for (const column of columns) {
+        // 判断有没有prop属性
+        const prop = _.get(column, 'property');
+        if (!prop) {
+          result.push('');
+          continue;
+        }
+        // 判断是否需要计算
+        const inlist = this.sumcol.find(f => f == prop);
+        if (!inlist) {
+          result.push('');
+          continue;
+        }
+        let res = 0;
+        // 整理出要计算的属性(只取出数字或者可以为数字的值)
+        const resetList = data.map(i => {
+          const d = _.get(i, prop);
+          const res = reg.test(d);
+          if (res) return d * 1;
+          else return 0;
+        });
+        if (this.sumres === 'total') {
+          res = this.totalComputed(resetList);
+        } else if (this.sumres === 'avg') {
+          res = this.avgComputed(resetList);
+        } else if (this.sumres === 'max') {
+          res = this.maxComputed(resetList);
+        } else if (this.sumres === 'min') {
+          res = this.minComputed(resetList);
+        }
+        result.push(res);
+      }
+      result[0] = '合计';
+      return result;
+    },
+    // 合计计算
+    totalComputed(data) {
+      const total = data.reduce((p, n) => p + n, 0);
+      return total;
+    },
+    // 平均值计算
+    avgComputed(data) {
+      const total = this.totalComputed(data);
+      return _.round(_.divide(total, data.length), 2);
+    },
+    // 最大值计算
+    maxComputed(data) {
+      return _.max(data);
+    },
+    // 最小值计算
+    minComputed(data) {
+      return _.min(data);
+    },
   },
   watch: {
     selected: {

+ 2 - 1
src/components/frame/form.md

@@ -12,7 +12,7 @@
 |needSave|Boolean|false|否|是否禁用保存按钮|
 |useEnter|Boolean|true|否|使用回车提交|
 |reset|Boolean|true|否|提交后是否重置表单|
-
+|filterReturn|Function|`-`|否|fields中,filterReturn为true的情况从这个函数走,参数:({data,model})|
 
 ### fields
 >Array类型 必填
@@ -29,6 +29,7 @@
 |tip|String|`-`|否|提示语,例如:请输入11位电话号码|
 |labelWidth|String|`120px`|否|表单label宽度,element的,默认120px|
 |format|Function|`-`|否|当type = text 时需要将该字段内容转换,可以使用format|
+|filterReturn|Boolean|`-`|否|是否返回这个字段,返回到filterReturn方法中|