IStreamTable.vue 10.5 KB
Newer Older
1 2
<template>
  <div class="eContainer-table-block">
liushikai committed
3 4 5 6 7 8 9
    <div style="text-align: left" v-if="showButtonFlg" class="buttonDiv">
              <c-button
                    icon="el-icon-s-tools"
                    @click="clounmSetting"
                    style=""
                  ></c-button
                ></div>
zhengxiaokui committed
10 11 12 13 14
    <el-table
      ref="table"
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
潘际乾 committed
15
      style="width: 100%"
liushikai committed
16
      class="eContainer-table"
潘际乾 committed
17 18
      @selection-change="handleSelectionChange"
      :row-key="getRowKey"
liushikai committed
19
      :header-cell-style="{ background: 'rgb(235, 235, 235)', color: 'rgb(51, 51, 51)' }"
zhengxiaokui committed
20
      :highlight-current-row="true"
潘际乾 committed
21
      @row-click="rowClick"
zhengxiaokui committed
22 23 24 25 26 27 28 29
      :border="true"
    >
      <el-table-column
        type="selection"
        width="55"
        v-if="showSelection"
        :reserve-selection="true"
      ></el-table-column>
30
      
31
      <c-table-column
32 33
        v-for="(item, key) in tableColumns"
        :key="key"
zhengxiaokui committed
34
        :prop="item.prop"
35 36
        :label="item.label"
        :width="item.width"
wangyanjiao committed
37
        sortable
denyu committed
38
      >
39 40 41
        <template v-slot="{ scope }">
        <!-- <template slot-scope="{ scope }"> -->
          <span>{{!item.render ? scope.row[item.prop] : item.render(item,scope)}}</span>
42
        </template>
43
      </c-table-column>
denyu committed
44
      <slot></slot>
45
    </el-table>
liushikai committed
46
    
liushikai committed
47 48 49 50 51
    <el-dialog
      class="showColumnDialog"
      :visible.sync="setColumnFlg"
      :title="'自定义列属性'"
      appenD-to-body
liushikai committed
52
      v-if="showButtonFlg"
liushikai committed
53 54 55 56
    >
      <el-form-item label-width="0">
        <el-checkbox-group v-model="columnGroup" @change="handleColumnChange">
          <el-checkbox
liushikai committed
57
            class="selectColumnClass"
liushikai committed
58 59 60 61 62 63 64 65 66
            v-for="item in tableColumnsOrigin"
            :key="item.label"
            :label="parseInt(item.index)"
            >{{ item.label }}</el-checkbox
          >
        </el-checkbox-group>
      </el-form-item>
      <span slot="footer">
        <el-checkbox
liushikai committed
67
          class="selectAllClass"
liushikai committed
68 69 70 71 72 73 74 75
          :indeterminate="columnGroup.length > 0 && columnGroup.length < tableColumnsOrigin.length"
          v-model="selectAll"
          @change="setAll"
          >全选</el-checkbox
        >
        <el-button type="primary" @click="saveColumnEvent">保存</el-button>
      </span>
    </el-dialog>
liushikai committed
76
    <c-col :span="16">
zhengxiaokui committed
77
    <el-pagination
liushikai committed
78
      v-if="paginationShow"
79 80 81 82 83 84
      class="eContainer-pagination"
      layout="prev, pager, next, jumper"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="tableData.length"
zhengxiaokui committed
85 86
      @size-change="sizeChange"
      @current-change="currentChange"
87
    ></el-pagination>
liushikai committed
88 89
    </c-col>
    <c-col :span="8">
liushikai committed
90
    <div class="paginationLable" v-if="paginationShow">
zhengxiaokui committed
91 92 93 94 95 96 97
      当前显示第 {{ (currentPage - 1) * pageSize + 1 }}-{{
        currentPage * pageSize > tableData.length
          ? tableData.length
          : currentPage * pageSize
      }}
      条,共 {{ tableData.length }} 条
    </div>
liushikai committed
98
    </c-col>
99 100 101
  </div>
</template>

zhujiazhan committed
102

103
<script>
zhujiazhan committed
104 105
import CodeTable from "~/config/CodeTable";

106 107 108 109 110 111 112 113 114 115 116 117 118 119
export default {
  props: {
    columns: {
      type: Array,
      default: () => {
        return [];
      },
    },
    list: {
      type: Array,
      default: () => {
        return [];
      },
    },
潘际乾 committed
120 121
    showSelection: {
      type: Boolean,
zhengxiaokui committed
122 123
      default: false,
    },
liushikai committed
124 125 126 127 128
    paginationShow: {
      type: Boolean,
      required: false,
      default: true,
    },
liushikai committed
129 130 131 132 133 134 135 136 137 138
    showButtonFlg: {
      type: Boolean,
      required: false,
      default: false,
    }
  },
  watch: {
    columns() {
      this.generateColumns();
    }
139 140
  },
  computed: {
liushikai committed
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
    tableData() {
      // return this.list.map((row) => {
      // 	const res = {}
      //   const vals = row.split("\t");
      // 	for (let i = 0; i < vals.length; i++) {
      // 		res[`${i}`] = vals[i];
      // 	}
      // 	return res;
      // });
      const arr = [];
      for (let i = 0; i < this.list.length; i++) {
        const d = this.list[i];
        const items = d.split("\t");
        const it = {};
        for (let j = 0; j < this.tableColumns.length; j++) {
          const column = this.tableColumns[j];
          it[column["prop"]] = column.children
            .map((c) => items[c["idx"]] || " ")
            .join("\n");
        }
        it["IDX"] = i;
        it['INR'] = items[0];
        if(it['INR'].length < 8){
          it['INR'] = items[1];
        }
        it.srcStr = d;
        arr.push(it);
      }
liushikai committed
169
      this.currentPage=1
liushikai committed
170 171 172 173 174 175 176 177 178 179 180 181
      return arr;
    },
  },
  data() {
    return {
      tableColumnsOrigin: [],
      tableColumns: [],
      currentPage: 1,
      pageSizes: [5, 10, 20, 30, 40, 50, 100],
      pageSize: 10,
      selectAll: true,
      columnGroup: [],
zhujiazhan committed
182 183 184 185
      setColumnFlg: false,
      codes:{
         ...CodeTable
      }
liushikai committed
186 187 188 189 190 191 192
    };
  },
  mounted() {
    this.generateColumns();
  },
  methods: {
    generateColumns() {
zhengxiaokui committed
193
      const columnArr = [];
潘际乾 committed
194
      const lines = this.columns;
zhengxiaokui committed
195 196
      const etyReg = /\"([^\"]*)\"/;
      const obj = {};
潘际乾 committed
197
      for (let i = 0; i < lines.length; i++) {
zhengxiaokui committed
198
        const line = lines[i];
199 200 201
        if(typeof line === "object" ){
          //如果是对象,支持自定义处理
          //{width,position,index,label,pattern,render}
zhujiazhan committed
202
          let {width,position,index,label,pattern,render,...rest} = line
203 204 205 206 207 208 209
          position += ''
          if (!obj[position]) {
            obj[position] = [];
          }
          obj[position].push({
            idx:index,
            prop:label,
潘际乾 committed
210 211
            // width:width+'px',
            width:width === 'auto' ? width : width +'px',
212 213 214 215 216 217
            pattern,
            render,
            ...rest
          })
          continue
        }
zhengxiaokui committed
218 219 220 221 222 223 224 225
        if (etyReg.test(line)) {
          const gs = line.match(etyReg);
          const columnName = gs[1];
          const newLine = line.replace(gs[0], "_");
          const colPropArr = newLine.split(" ");
          const positionArr = colPropArr[1].split(":");
          if (!obj[positionArr[0]]) {
            obj[positionArr[0]] = [];
潘际乾 committed
226
          }
zhengxiaokui committed
227 228 229
          obj[positionArr[0]].push({
            idx: colPropArr[0],
            prop: columnName,
潘际乾 committed
230 231
            // width: colPropArr[3] + 'px',
            width: colPropArr[3] === 'auto' ? colPropArr[3] : colPropArr[3] + 'px',
zhengxiaokui committed
232 233
          });
        }
234
      }
潘际乾 committed
235
      for (const k in obj) {
zhengxiaokui committed
236 237 238
        if (Object.hasOwnProperty.call(obj, k)) {
          const o = obj[k];
          const tableColumn = o.map((item) => item.prop).join("\n");
239
          let colInfo = {
wangren committed
240
            prop: tableColumn,
zhengxiaokui committed
241
            label: tableColumn,
liushikai committed
242 243
            width: o[0].width,
            // width: "auto",
zhengxiaokui committed
244 245
            index: k,
            children: o,
246 247 248 249 250 251 252 253 254 255 256
          }
          //支持自定义处理
          if(o.length == 1){
            if(o[0].pattern && this[o[0].pattern]){
              colInfo.render = this[o[0].pattern]
              colInfo.code = o[0].code
            }else if(o[0].render){
              colInfo.render = o[0].render
            }
          }
          columnArr.push(colInfo);
zhengxiaokui committed
257
        }
潘际乾 committed
258
      }
liushikai committed
259
      const arr = columnArr.sort((a, b) => {
zhengxiaokui committed
260 261
        return parseInt(a.index) - parseInt(b.index);
      });
liushikai committed
262 263 264
      this.tableColumnsOrigin = arr;
      this.tableColumns = arr;
      this.columnGroup = arr.map((item) => parseInt(item.index));
zhengxiaokui committed
265
      return arr;
266
    },
zhengxiaokui committed
267 268 269 270 271 272
    sizeChange(size) {
      this.pageSize = size;
    },
    currentChange(currentPage) {
      this.currentPage = currentPage;
    },
潘际乾 committed
273
    handleSelectionChange(val) {
zhengxiaokui committed
274
      this.$emit("multipleSelect", this.getSelectedRowIndex(val));
潘际乾 committed
275 276
    },
    getRowKey(row) {
zhengxiaokui committed
277
      return row["IDX"];
潘际乾 committed
278 279
    },
    getSelectedRowIndex(val) {
zhengxiaokui committed
280
      const indexArr = [];
潘际乾 committed
281 282 283 284
      for (let j = 0; j < val.length; j++) {
        const v = val[j];
        for (let i = 0; i < this.tableData.length; i++) {
          const data = this.tableData[i];
zhengxiaokui committed
285 286 287
          if (v["IDX"] === data["IDX"]) {
            indexArr.push(i);
          }
潘际乾 committed
288 289
        }
      }
zhengxiaokui committed
290
      return indexArr;
潘际乾 committed
291 292 293 294
    },
    // 行点击,设置高亮
    rowClick(row, column, event) {
      this.$refs.table.setCurrentRow(row);
zhengxiaokui committed
295 296
      this.$emit("chooseRowEvent", row);
    },
liushikai committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    clounmSetting() {
      this.setColumnFlg = true;
    },
    saveColumnEvent() {
      this.setColumnFlg = false;
      const arr = this.columnGroup.map(idx => parseInt(idx));
      arr.sort((a,b) => a - b);
      this.columnGroup = arr;
      this.tableColumns = this.columnGroup.map(
        (index) => this.tableColumnsOrigin[parseInt(index) - 1]
      );
    },
    setAll(val) {
      this.columnGroup = val ? this.tableColumnsOrigin.map((item) => parseInt(item.index)) : [];
    },
    handleColumnChange() {
      this.selectAll = this.tableColumnsOrigin.length === this.columnGroup.length;
    },
315 316

    //补充自定义列处理函数
liushikai committed
317
    //去掉日期的时分秒毫秒
fukai committed
318 319
    date(item,scope){
      let value = scope.row[item.prop]
320 321 322 323 324 325 326 327
      if(!value){
        return ""
      }
      let idx = value.indexOf(" ")
      if(idx > 0)
        return value.substring(0,idx)
      return value
    },
liushikai committed
328
    //code映射
fukai committed
329 330 331
    code(item,scope){
      let value = scope.row[item.prop]
      let code = item.code
332 333
      if(typeof code == 'string'){
        code = this.codes[code]
334 335
      }
      if(!value || !code){
wangren committed
336 337
        return ""
      }
zhujiazhan committed
338
      let em = code.find(item=>item.value.trim() == value.trim())
339 340 341 342
      if(!em){
        return value
      }
      return em.label
343
    },
liushikai committed
344 345
    //去掉时间的毫秒
    time(item,scope){
346 347 348 349 350
      let value = scope.row[item.prop];
      if(!value){
        return ""
      }
      let idx = value.indexOf(".");
liushikai committed
351 352 353
      if(idx>0)
         return value.substring(0,idx);
      return value
354
    },
liushikai committed
355
    //日期格式化
356 357 358 359 360
    dateFormat(item,scope){
      let value = scope.row[item.prop];
      if(!value){
        return ""
      }
liushikai committed
361 362
      return value.substring(0,4)+"-"+value.substring(4,6)+"-"+value.substring(6)
    },
liushikai committed
363
  }
364 365 366
};
</script>

潘际乾 committed
367
<style scoped>
zhengxiaokui committed
368
.eContainer-table-block {
潘际乾 committed
369
  margin-top: 15px;
潘际乾 committed
370
  position: relative;
潘际乾 committed
371
}
zhengxiaokui committed
372 373 374 375 376 377
.eContainer-table-block .paginationLable {
  font-size: 12px;
  color: #808080;
  height: 26px;
  line-height: 26px;
  float: right;
liushikai committed
378
  /* margin-top: 20px; */
379 380 381 382 383 384 385 386 387 388
}
.eContainer-table-block .el-table__body-wrapper {
  overflow: auto;
}
.el-table .warning-row {
  background: oldlace;
}
.el-table .success-row {
  background: #f0f9eb;
}
liushikai committed
389
.selectColumnClass .el-checkbox__label {
390
  width: 67px;
liushikai committed
391 392 393 394 395 396 397
  font-size: 13px;
}
.selectAllClass .el-checkbox__label {
  width: 30px;
  font-size: 13px;
  padding-left: 5px;
}
wangren committed
398
.eContainer-table-block >>> .el-table .cell {
zhengxiaokui committed
399
  white-space: pre-wrap;
潘际乾 committed
400
}
liushikai committed
401 402 403 404 405 406 407 408
.buttonDiv{
  text-align: left;
    display: inline-block;
    margin: 0;
    position: absolute;
    z-index: 999;
    right: 5px;
    padding-top: 3px;
liushikai committed
409
}
410
</style>