IStreamTable.vue 10.7 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 7 8 9
      <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"
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 26 27 28 29 30 31 32
      :border="true"
    >
      <el-table-column
        type="selection"
        width="55"
        v-if="showSelection"
        :reserve-selection="true"
      ></el-table-column>
taojinrui committed
33

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

liushikai committed
52 53 54 55 56
    <el-dialog
      class="showColumnDialog"
      :visible.sync="setColumnFlg"
      :title="'自定义列属性'"
      appenD-to-body
liushikai committed
57
      v-if="showButtonFlg"
liushikai committed
58 59 60 61
    >
      <el-form-item label-width="0">
        <el-checkbox-group v-model="columnGroup" @change="handleColumnChange">
          <el-checkbox
liushikai committed
62
            class="selectColumnClass"
liushikai committed
63 64 65 66 67 68 69 70 71
            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
72
          class="selectAllClass"
taojinrui committed
73 74 75 76
          :indeterminate="
            columnGroup.length > 0 &&
            columnGroup.length < tableColumnsOrigin.length
          "
liushikai committed
77 78 79 80 81 82 83
          v-model="selectAll"
          @change="setAll"
          >全选</el-checkbox
        >
        <el-button type="primary" @click="saveColumnEvent">保存</el-button>
      </span>
    </el-dialog>
liushikai committed
84
    <c-col :span="16">
taojinrui committed
85 86 87 88 89 90 91 92 93 94 95
      <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
96 97
    </c-col>
    <c-col :span="8">
taojinrui committed
98 99 100 101 102 103 104 105
      <div class="paginationLable" v-if="paginationShow">
        当前显示第 {{ (currentPage - 1) * pageSize + 1 }}-{{
          currentPage * pageSize > tableData.length
            ? tableData.length
            : currentPage * pageSize
        }}
        条,共 {{ tableData.length }} 条
      </div>
liushikai committed
106
    </c-col>
107 108 109
  </div>
</template>

zhujiazhan committed
110

111
<script>
zhujiazhan committed
112 113
import CodeTable from "~/config/CodeTable";

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

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

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