IStreamTable.vue 10.3 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 32 33
      <el-table-column
        v-for="(item, key) in tableColumns"
        :key="key"
zhengxiaokui committed
34
        :prop="item.prop"
35 36
        :label="item.label"
        :width="item.width"
denyu committed
37
      >
38
        <template slot-scope="scope">
fukai committed
39
          <span>{{!item.render?scope.row[item.prop]:item.render(item,scope)}}</span>
40
        </template>
denyu committed
41 42
      </el-table-column>
      <slot></slot>
43
    </el-table>
liushikai committed
44
    
liushikai committed
45 46 47 48 49
    <el-dialog
      class="showColumnDialog"
      :visible.sync="setColumnFlg"
      :title="'自定义列属性'"
      appenD-to-body
liushikai committed
50
      v-if="showButtonFlg"
liushikai committed
51 52 53 54
    >
      <el-form-item label-width="0">
        <el-checkbox-group v-model="columnGroup" @change="handleColumnChange">
          <el-checkbox
liushikai committed
55
            class="selectColumnClass"
liushikai committed
56 57 58 59 60 61 62 63 64
            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
65
          class="selectAllClass"
liushikai committed
66 67 68 69 70 71 72 73
          :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
74
    <c-col :span="16">
zhengxiaokui committed
75
    <el-pagination
liushikai committed
76
      v-if="paginationShow"
77 78 79 80 81 82
      class="eContainer-pagination"
      layout="prev, pager, next, jumper"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="tableData.length"
zhengxiaokui committed
83 84
      @size-change="sizeChange"
      @current-change="currentChange"
85
    ></el-pagination>
liushikai committed
86 87
    </c-col>
    <c-col :span="8">
liushikai committed
88
    <div class="paginationLable" v-if="paginationShow">
zhengxiaokui committed
89 90 91 92 93 94 95
      当前显示第 {{ (currentPage - 1) * pageSize + 1 }}-{{
        currentPage * pageSize > tableData.length
          ? tableData.length
          : currentPage * pageSize
      }}
      条,共 {{ tableData.length }} 条
    </div>
liushikai committed
96
    </c-col>
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  </div>
</template>

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

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

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