IStreamTable.vue 11.2 KB
Newer Older
1 2
<template>
  <div class="eContainer-table-block">
liushikai committed
3
    <div style="text-align: left" v-if="showButtonFlg" class="buttonDiv">
taojinrui committed
4 5 6
      <c-button
        icon="el-icon-s-tools"
        @click="clounmSetting"
nanrui committed
7
        style="margin-right: 20px;margin-top: 10px;"
taojinrui committed
8 9
      ></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"
taojinrui committed
19 20 21 22
      :header-cell-style="{
        background: 'rgb(235, 235, 235)',
        color: 'rgb(51, 51, 51)',
      }"
zhengxiaokui committed
23
      :highlight-current-row="true"
潘际乾 committed
24
      @row-click="rowClick"
zhengxiaokui committed
25
      :border="true"
26
      max-height="380"
zhengxiaokui committed
27 28 29 30 31 32 33
    >
      <el-table-column
        type="selection"
        width="55"
        v-if="showSelection"
        :reserve-selection="true"
      ></el-table-column>
taojinrui committed
34

35
      <c-table-column
36 37
        v-for="(item, key) in tableColumns"
        :key="key"
zhengxiaokui committed
38
        :prop="item.prop"
39 40
        :label="item.label"
        :width="item.width"
wangyanjiao committed
41
        sortable
denyu committed
42
      >
43
        <template v-slot="{ scope }">
taojinrui committed
44 45 46 47
          <!-- <template slot-scope="{ scope }"> -->
          <span>{{
            !item.render ? scope.row[item.prop] : item.render(item, scope)
          }}</span>
48
        </template>
49
      </c-table-column>
denyu committed
50
      <slot></slot>
51
    </el-table>
taojinrui committed
52

liushikai committed
53 54 55 56 57
    <el-dialog
      class="showColumnDialog"
      :visible.sync="setColumnFlg"
      :title="'自定义列属性'"
      appenD-to-body
liushikai committed
58
      v-if="showButtonFlg"
liushikai committed
59
    >
Wuyuqiu committed
60 61
      <el-form-item label-width="0" style="display: flex;
    align-items: center;">
liushikai committed
62 63
        <el-checkbox-group v-model="columnGroup" @change="handleColumnChange">
          <el-checkbox
Wuyuqiu committed
64 65
            class="selectColumnClass checkbox-left"
            style="margin-right:45px;"
liushikai committed
66 67 68 69 70 71 72
            v-for="item in tableColumnsOrigin"
            :key="item.label"
            :label="parseInt(item.index)"
            >{{ item.label }}</el-checkbox
          >
        </el-checkbox-group>
      </el-form-item>
Wuyuqiu committed
73 74
      <span slot="footer" style="display: flex;
    align-items: center;">
liushikai committed
75
        <el-checkbox
Wuyuqiu committed
76
          class="selectAllClass checkbox-left"
taojinrui committed
77 78 79 80
          :indeterminate="
            columnGroup.length > 0 &&
            columnGroup.length < tableColumnsOrigin.length
          "
liushikai committed
81 82 83 84
          v-model="selectAll"
          @change="setAll"
          >全选</el-checkbox
        >
Wuyuqiu committed
85
        <el-button type="primary" @click="saveColumnEvent" style="margin-left:10px;">保存</el-button>
liushikai committed
86 87
      </span>
    </el-dialog>
jianglong committed
88
    <c-col :span="16" v-if="tableData.length>10">
taojinrui committed
89 90 91 92 93 94 95 96 97 98 99
      <el-pagination
        v-if="paginationShow"
        class="eContainer-pagination"
        layout="prev, pager, next, jumper"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        :current-page="currentPage"
        :total="tableData.length"
        @size-change="sizeChange"
        @current-change="currentChange"
      ></el-pagination>
liushikai committed
100
    </c-col>
jianglong committed
101
    <c-col :span="8" style="float:right">
taojinrui committed
102 103 104 105 106 107 108 109
      <div class="paginationLable" v-if="paginationShow">
        当前显示第 {{ (currentPage - 1) * pageSize + 1 }}-{{
          currentPage * pageSize > tableData.length
            ? tableData.length
            : currentPage * pageSize
        }}
        条,共 {{ tableData.length }} 条
      </div>
liushikai committed
110
    </c-col>
111 112 113
  </div>
</template>

zhujiazhan committed
114

115
<script>
zhujiazhan committed
116 117
import CodeTable from "~/config/CodeTable";

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

    //补充自定义列处理函数
liushikai committed
334
    //去掉日期的时分秒毫秒
taojinrui committed
335 336 337 338
    date(item, scope) {
      let value = scope.row[item.prop];
      if (!value) {
        return "";
339
      }
taojinrui committed
340 341 342
      let idx = value.indexOf(" ");
      if (idx > 0) return value.substring(0, idx);
      return value;
343
    },
liushikai committed
344
    //code映射
taojinrui committed
345 346 347 348 349
    code(item, scope) {
      let value = scope.row[item.prop];
      let code = item.code;
      if (typeof code == "string") {
        code = this.codes[code];
350
      }
taojinrui committed
351 352
      if (!value || !code) {
        return "";
wangren committed
353
      }
taojinrui committed
354 355 356
      let em = code.find((item) => item.value.trim() == value.trim());
      if (!em) {
        return value;
357
      }
taojinrui committed
358
      return em.label;
359
    },
liushikai committed
360
    //去掉时间的毫秒
taojinrui committed
361
    time(item, scope) {
362
      let value = scope.row[item.prop];
taojinrui committed
363 364
      if (!value) {
        return "";
365 366
      }
      let idx = value.indexOf(".");
taojinrui committed
367 368
      if (idx > 0) return value.substring(0, idx);
      return value;
369
    },
liushikai committed
370
    //日期格式化
taojinrui committed
371
    dateFormat(item, scope) {
372
      let value = scope.row[item.prop];
taojinrui committed
373 374
      if (!value) {
        return "";
375
      }
taojinrui committed
376 377 378 379 380 381 382
      return (
        value.substring(0, 4) +
        "-" +
        value.substring(4, 6) +
        "-" +
        value.substring(6)
      );
liushikai committed
383
    },
taojinrui committed
384
  },
385 386 387
};
</script>

潘际乾 committed
388
<style scoped>
zhengxiaokui committed
389
.eContainer-table-block {
潘际乾 committed
390
  margin-top: 15px;
潘际乾 committed
391
  position: relative;
潘际乾 committed
392
}
zhengxiaokui committed
393 394 395 396 397 398
.eContainer-table-block .paginationLable {
  font-size: 12px;
  color: #808080;
  height: 26px;
  line-height: 26px;
  float: right;
liushikai committed
399
  /* margin-top: 20px; */
400 401 402 403 404 405 406 407 408 409
}
.eContainer-table-block .el-table__body-wrapper {
  overflow: auto;
}
.el-table .warning-row {
  background: oldlace;
}
.el-table .success-row {
  background: #f0f9eb;
}
liushikai committed
410
.selectColumnClass .el-checkbox__label {
411
  width: 67px;
liushikai committed
412 413 414 415 416 417 418
  font-size: 13px;
}
.selectAllClass .el-checkbox__label {
  width: 30px;
  font-size: 13px;
  padding-left: 5px;
}
wangren committed
419
.eContainer-table-block >>> .el-table .cell {
zhengxiaokui committed
420
  white-space: pre-wrap;
潘际乾 committed
421
}
Wuyuqiu committed
422 423 424 425
.eContainer-table-block >>> .el-dialog .el-dialog__footer {
  display: flex;
    justify-content: center;
}
taojinrui committed
426
.buttonDiv {
liushikai committed
427
  text-align: left;
taojinrui committed
428 429
  display: inline-block;
  margin: 0;
430
  margin-top: -3px;
taojinrui committed
431 432 433 434
  position: absolute;
  z-index: 999;
  right: 5px;
  padding-top: 3px;
liushikai committed
435
}
436
</style>