李林
2024-03-22 28f428180885b4718c676f4815c8d8f96c6add43
src/components/tool/value-table.vue
@@ -16,6 +16,59 @@
      text-align: right;
      margin-top: 10px;
   }
   .down-left {
      width: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
   }
   .down-right {
      width: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
   }
   .body {
      overflow-x: hidden;
   }
</style>
<style>
   /* .value-table .highlight-warning-row-border td {
      border-top: 2px solid #ffcd29;
      border-bottom: 2px solid #ffcd29;
   } */
   .value-table .highlight-warning-row-border td:first-child {
      border-left: 4px solid #ffcd29;
   }
   .value-table .highlight-warning-row-border td:last-child {
      border-right: 4px solid #ffcd29;
   }
   /* .value-table .highlight-danger-row-border td {
      border-top: 2px solid red;
      border-bottom: 2px solid red;
   } */
   .value-table .highlight-danger-row-border td:first-child {
      border-left: 4px solid red;
   }
   .value-table .highlight-danger-row-border td:last-child {
      border-right: 4px solid red;
   }
   .value-table .el-upload {
      width: 100%;
   }
   .value-table .el-upload-dragger {
      width: 100%;
   }
</style>
<template>
@@ -23,25 +76,37 @@
      <div class="table">
         <el-table ref="eltable" :data="tableData" style="width: 100%;" height="100%" tooltip-effect="dark" border
            @selection-change="selectChange" @select="select" v-loading="loading" @sort-change="sortChange"
            @row-click="rowClick">
            <el-table-column type="selection" width="50" v-if="data.showSelect">
            @row-click="rowClick" :show-summary="data.countFleid!=undefined && data.countFleid.length > 0"
            :summary-method="getSummaries" :row-class-name="tableRowClassName">
            <el-table-column type="selection" width="65" v-if="data.showSelect" :key="Math.random()">
            </el-table-column>
            <el-table-column align="center" type="index" label="序号" width="70" v-if="data.isIndex">
            <el-table-column type="index" align="center" label="序号" width="70" v-if="data.isIndex" :key="Math.random()">
            </el-table-column>
            <el-table-column :prop="a.label" :label="a.value" sortable="custom" v-for="(a, ai) in tableHead" :key="ai"
               show-overflow-tooltip min-width="145">
            <el-table-column :prop="a.label" :label="a.value" :sortable="data.sort==false?false:'custom'"
               v-for="(a, ai) in tableHead" :key="ai" show-overflow-tooltip min-width="160">
               <template slot-scope="scope">
                  <div v-if="showType(a.label, data.tagField) != null">
                     <el-tag v-for="(b, bi) in data.tagField[a.label].select" :key="bi" v-if="b.value == scope.row[a.label]"
                        :type="b.type" size="medium">{{b.label}}</el-tag>
                     <template v-if="typeof(scope.row[a.label]) == 'object'">
                        <template v-for="(param, i) in scope.row[a.label]">
                           <el-tag v-for="(b, bi) in data.tagField[a.label].select" :key="bi" v-if="b.value == param"
                              :type="b.type" size="medium" style="margin-right: 5px;">{{b.label}}</el-tag>
                        </template>
                     </template>
                     <template v-else>
                        <el-tag v-for="(b, bi) in data.tagField[a.label].select" :key="bi" v-if="b.value == scope.row[a.label]"
                           :type="b.type" size="medium">{{b.label}}</el-tag>
                     </template>
                  </div>
                  <!-- 可操作行进行点击事件 -->
                  <span v-else-if="data.linkEvent&&showLink(a.label, data.linkEvent)" style="color:#409EFF;cursor: pointer;"
                     @click="handleLinkEvent(scope.row,data.linkEvent[a.label])">{{scope.row[a.label]}}</span>
                  <span v-else>{{scope.row[a.label]}}</span>
               </template>
            </el-table-column>
            <el-table-column fixed="right" align="center" label="操作" :width="50 + data.do.length * 30"
               v-if="data.do.length > 0">
            <el-table-column fixed="right" align="center" label="操作" :width="getWidth()" v-if="data.do.length > 0">
               <template slot-scope="scope">
                  <el-button v-for="(a, ai) in data.do" :key="ai" :type="a.type"
                     :disabled="a.disabFun ? a.disabFun(scope.row,scope.$index) : false"
                     @click="main(scope.row, a)">{{a.font}}</el-button>
               </template>
            </el-table-column>
@@ -53,44 +118,110 @@
            :total="total">
         </el-pagination>
      </div>
      <el-dialog title="编辑" :visible.sync="upDia" width="500px">
         <div class="body" v-if="upDia">
            <el-row v-for="(a, ai) in upHead" :key="ai" style="line-height: 50px;">
               <el-col :span="5" style="text-align: right;">{{a.value}}:</el-col>
               <el-col :span="17" :offset="1">
                  <el-input v-model="upData[a.label]" size="small" clearable
                     v-if="showType(a.label, data.tagField) == null"></el-input>
                  <el-select v-model="upData[a.label]" size="small" v-if="showType(a.label, data.tagField) != null"
                     style="width: 100%;">
                     <el-option v-for="(b, bi) in data.tagField[a.label].select" :key="bi" :value="b.value"
                        :label="b.label"></el-option>
                  </el-select>
               </el-col>
            </el-row>
      <el-dialog title="编辑" :visible.sync="upDia" :width="data.row==1?'500px':500+data.row * 200 + 'px'">
         <div class="body" v-if="upDia" style="max-height: 550px;overflow-y: auto;padding: 5px 10px 5px 0;">
            <div v-if="data.row > 1">
               <div v-for="(a, ai) in upHead" :key="ai" style="line-height: 50px;">
                  <el-col :span="6/data.row" style="text-align: right;" :offset="1">
                     <span class="required-span" v-if="showUpReq(a.label)">* </span>{{a.value}}:
                  </el-col>
                  <el-col :span="16/data.row">
                     <el-input v-model="upData[a.label]" size="small" clearable :placeholder="`请输入${a.value}`"
                        v-if="showType(a.label, data.selectField) == null" :disabled="isDisabled(a.label)"></el-input>
                     <el-select v-model="upData[a.label]" :multiple="data.selectField[a.label].choose" size="small"
                        v-if="showType(a.label, data.selectField) != null" style="width: 100%;" :disabled="isDisabled(a.label)"
                        clearable>
                        <el-option v-for="(b, bi) in data.selectField[a.label].select" :key="bi" :value="b.value"
                           :label="b.label">
                        </el-option>
                     </el-select>
                  </el-col>
               </div>
            </div>
            <div v-else>
               <el-row v-for="(a, ai) in upHead" :key="ai" style="line-height: 50px;">
                  <el-col :span="6" style="text-align: right;">
                     <span class="required-span" v-if="showUpReq(a.label)">* </span>{{a.value}}:
                  </el-col>
                  <el-col :span="16" :offset="1">
                     <el-input v-model="upData[a.label]" size="small" clearable :placeholder="`请输入${a.value}`"
                        v-if="showType(a.label, data.selectField) == null" :disabled="isDisabled(a.label)"></el-input>
                     <el-select v-model="upData[a.label]" :multiple="data.selectField[a.label].choose" size="small"
                        v-if="showType(a.label, data.selectField) != null" style="width: 100%;" :disabled="isDisabled(a.label)"
                        clearable>
                        <el-option v-for="(b, bi) in data.selectField[a.label].select" :key="bi" :value="b.value"
                           :label="b.label"></el-option>
                     </el-select>
                  </el-col>
               </el-row>
            </div>
         </div>
         <span slot="footer" class="dialog-footer">
            <el-button @click="upDia = false">取 消</el-button>
            <el-button type="primary" @click="saveUpData" :loading="upLoad">确 定</el-button>
         </span>
      </el-dialog>
      <el-dialog title="新增" :visible.sync="addDia" width="500px">
         <div class="body" v-if="addDia">
            <el-row v-for="(a, ai) in upHead" :key="ai" style="line-height: 50px;">
               <el-col :span="5" style="text-align: right;">{{a.value}}:</el-col>
               <el-col :span="17" :offset="1">
                  <el-input v-model="upData[a.label]" size="small" clearable :placeholder="`请输入${a.value}`"
                     v-if="showType(a.label, data.tagField) == null"></el-input>
                  <el-select v-model="upData[a.label]" size="small" v-if="showType(a.label, data.tagField) != null"
                     style="width: 100%;" :placeholder="`请选择${a.value}`">
                     <el-option v-for="(b, bi) in data.tagField[a.label].select" :key="bi" :value="b.value"
                        :label="b.label"></el-option>
                  </el-select>
               </el-col>
            </el-row>
      <el-dialog title="新增" :visible.sync="addDia" :width="data.row==1?'500px':500+data.row * 200 + 'px'">
         <div class="body" v-if="addDia" style="max-height: 550px;overflow-y: auto;padding: 5px 0;">
            <div v-if="data.row > 1">
               <div v-for="(a, ai) in upHead" :key="ai" style="line-height: 50px;">
                  <el-col :span="6/data.row" style="text-align: right;"><span class="required-span"
                        v-if="showAddReq(a.label)">*
                     </span>{{a.value}}:</el-col>
                  <el-col :span="16/data.row" :offset="1">
                     <el-input v-model="upData[a.label]" size="small" clearable :placeholder="`请输入${a.value}`"
                        v-if="showType(a.label, data.selectField) == null"></el-input>
                     <el-select v-model="upData[a.label]" size="small" v-if="showType(a.label, data.selectField) != null"
                        style="width: 100%;" :placeholder="`请选择${a.value}`" clearable>
                        <el-option v-for="(b, bi) in data.selectField[a.label].select" :key="bi" :value="b.value"
                           :label="b.label"></el-option>
                     </el-select>
                  </el-col>
               </div>
            </div>
            <div v-else>
               <el-row v-for="(a, ai) in upHead" :key="ai" style="line-height: 50px;">
                  <el-col :span="6" style="text-align: right;"><span class="required-span" v-if="showAddReq(a.label)">*
                     </span>{{a.value}}:</el-col>
                  <el-col :span="16" :offset="1">
                     <el-input v-model="upData[a.label]" size="small" clearable :placeholder="`请输入${a.value}`"
                        v-if="showType(a.label, data.selectField) == null"></el-input>
                     <el-select v-model="upData[a.label]" size="small" v-if="showType(a.label, data.selectField) != null"
                        style="width: 100%;" :placeholder="`请选择${a.value}`" clearable>
                        <el-option v-for="(b, bi) in data.selectField[a.label].select" :key="bi" :value="b.value"
                           :label="b.label"></el-option>
                     </el-select>
                  </el-col>
               </el-row>
            </div>
         </div>
         <span slot="footer" class="dialog-footer">
            <el-button @click="addDia = false">取 消</el-button>
            <el-button type="primary" @click="saveAddData" :loading="addLoad">确 定</el-button>
         </span>
      </el-dialog>
      <el-dialog title="导出" :visible.sync="downDia" width="400px">
         <div style="display: flex;width: 100%;height: 100px;">
            <div class="down-left" @click="downFile(true)">
               <el-button>本页导出</el-button>
            </div>
            <div class="down-right" @click="downFile(false)">
               <el-button>全部导出</el-button>
            </div>
         </div>
      </el-dialog>
      <el-dialog title="数据导入" :visible.sync="uploadDia" width="500px">
         <div style="margin: 0 auto;">
            <el-upload ref="upload" drag :action="javaApi + inputUrl" :headers="token" :file-list="fileList" name="file"
               :auto-upload="false" accept=".csv" :limit="1" :on-change="beforeUpload" :on-success="onSuccess"
               :on-error="onError" :data="{param: data.uploadStr}">
               <i class="el-icon-upload"></i>
               <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
            </el-upload>
         </div>
         <span slot="footer" class="dialog-footer">
            <el-button @click="uploadDia = false">取 消</el-button>
            <el-button type="primary" @click="submitUpload()">上 传</el-button>
         </span>
      </el-dialog>
   </div>
@@ -102,6 +233,9 @@
   } from 'iview';
   export default {
      props: {
         tableRowClassName: {
            type: Function
         },
         url: {
            type: String,
            default: () => null
@@ -111,6 +245,14 @@
            default: () => null
         },
         delUrl: {
            type: String,
            default: () => null
         },
         downUrl: {
            type: String,
            default: () => null
         },
         inputUrl: {
            type: String,
            default: () => null
         },
@@ -124,9 +266,10 @@
                        order: 'asc'
                     }
                  },
                  isIndex: true,
                  showSelect: true,
                  isIndex: false,
                  showSelect: false,
                  select: true,
                  row: 1,
                  do: [{
                     id: 'update',
                     font: '编辑',
@@ -164,14 +307,16 @@
                     order: 'asc'
                  }
               },
               isIndex: true,
               showSelect: true,
               isIndex: false,
               showSelect: false,
               select: true,
               row: 1,
               do: [{
                  font: '删除',
                  type: 'text'
               }],
               type: []
               type: [],
               uploadStr: ""
            },
            tableHead: [],
            tableData: [],
@@ -190,7 +335,12 @@
            addDia: false,
            addUrl: null,
            addLoad: false,
            dataCopy: {}
            dataCopy: {},
            downDia: false,
            uploadDia: false,
            token: null,
            fileList: [],
            init: true
         }
      },
      watch: {
@@ -206,7 +356,12 @@
      mounted() {
         this.data = this.componentData
         this.dataCopy = this.HaveJson(this.componentData)
         this.selectList()
         if(this.data.init===undefined||this.data.init===true){
            this.selectList()
         }
         this.token = {
            'token': sessionStorage.getItem('token')
         }
      },
      methods: {
         selectChange(val) {
@@ -255,6 +410,15 @@
               this.total = res.data.body.total
               this.tableHead = res.data.head
               this.tableData = res.data.body.records
               for (var a in this.data.selectField) {
                  if (this.data.selectField[a].choose == true) {
                     this.tableData.map(b => {
                        try {
                           b[a] = JSON.parse(b[a])
                        } catch (e) {}
                     })
                  }
               }
               this.loading = false
            }).catch(e => {
               this.loading = false
@@ -279,6 +443,32 @@
            var str = ob[val]
            return str == undefined ? null : ob[val].select
         },
         showLink(val, ob) {
            var str = ob[val]
            return str == undefined ? null : ob[val].method
         },
         showAddReq(label) {
            if (this.data.requiredAdd == undefined) {
               return
            }
            for (var i = 0; i < this.data.requiredAdd.length; i++) {
               if (label == this.data.requiredAdd[i]) {
                  return true
               }
            }
            return false
         },
         showUpReq(label) {
            if (this.data.requiredUp == undefined) {
               return
            }
            for (var i = 0; i < this.data.requiredUp.length; i++) {
               if (label == this.data.requiredUp[i]) {
                  return true
               }
            }
            return false
         },
         main(row, val) {
            if (val.method == undefined) return
            else if (val.method == 'doDiy') {
@@ -293,7 +483,7 @@
                  delete this.upData.createUser
                  delete this.upData.updateUser
                  val.field.forEach(a => {
                     if (this.upData[a] == undefined) {
                     if (this.upData[a] === undefined) {
                        this.upHead.push({
                           value: a.split('=')[0],
                           label: a.split('=')[1]
@@ -334,7 +524,7 @@
               }
            } else {
               delete row.orderBy
               this.$emit(val.method, row)
               this.$parent[val.method](row)
            }
         },
         saveUpData() {
@@ -342,7 +532,22 @@
               this.$message.error('请给修改请求地址')
               return
            }
            for (var i = 0; i < this.data.requiredUp.length; i++) {
               if (this.upData[this.data.requiredUp[i]] === null || this.upData[this.data.requiredUp[i]] === '') {
                  var list = this.upHead.filter(a => {
                     if (a.label == this.data.requiredUp[i]) return a
                  })
                  if (list[0].value == undefined) continue
                  this.$message.error(list[0].value + '是必填项')
                  return
               }
            }
            this.upLoad = true
            for (var a in this.data.selectField) {
               if (this.data.selectField[a].choose == true) {
                  this.upData[a] = JSON.stringify(this.upData[a])
               }
            }
            this.$axios.post(this.upUrl, this.upData, {
               headers: {
                  'Content-Type': 'application/json'
@@ -375,8 +580,13 @@
            this.upHead = this.HaveJson(this.tableHead)
            this.upHead = this.upHead.filter(a => a.label != 'createTime' && a.label != 'updateTime')
            var val = this.data.do.filter(a => a.id == 'update')[0]
            if (val == undefined) {
               val = {
                  field: []
               }
            }
            val.field.forEach(a => {
               if (JSON.stringify(this.upData[a]) == undefined) {
               if (JSON.stringify(this.upData[a]) === undefined) {
                  this.upHead.push({
                     value: a.split('=')[0],
                     label: a.split('=')[1]
@@ -398,6 +608,16 @@
               this.$message.error('请给添加请求地址')
               return
            }
            for (var i = 0; i < this.data.requiredAdd.length; i++) {
               if (this.upData[this.data.requiredAdd[i]] === null || this.upData[this.data.requiredAdd[i]] === '') {
                  var list = this.upHead.filter(a => {
                     if (a.label == this.data.requiredAdd[i]) return a
                  })
                  if (list[0].value == undefined) continue
                  this.$message.error(list[0].value + '是必填项')
                  return
               }
            }
            this.addLoad = true
            this.$axios.post(this.addUrl, this.upData, {
               headers: {
@@ -416,7 +636,146 @@
               this.addDia = false
               this.addLoad = false
            })
         },
         openDownDia() {
            this.downDia = true
         },
         downFile(type) {
            if (this.downUrl == null) {
               this.$message.error('请给导出请求地址')
               return
            }
            var page = this.HaveJson(this.page)
            if (!type) {
               page.current = 0
               page.size = this.total
            }
            this.loading = true
            this.$axios.post(this.downUrl, {
               page: page,
               entity: this.data.entity
            }, {
               headers: {
                  'Content-Type': 'application/json'
               }
            }).then(res => {
               if (res.code === 201) {
                  this.loading = false
                  return
               }
               this.downDia = false
               this.loading = false
               this.$message.success('导出成功')
               let eleLink = document.createElement('a')
               eleLink.style.display = 'none'
               eleLink.download = res.data
               eleLink.href = this.javaApi + '/outPath/' + res.data
               document.body.appendChild(eleLink)
               eleLink.click()
               document.body.removeChild(eleLink)
            }).catch(e => {
               console.log(e);
               this.loading = false
               this.$message.error('请刷新页面再尝试')
            })
         },
         openUpload() {
            if (this.inputUrl == null) {
               this.$message.error('请给导入请求地址')
               return
            }
            this.uploadDia = true
         },
         beforeUpload(file, fileList) {
            if (file.raw.type != 'text/csv') {
               this.$message.error('上传文件格式不正确');
               this.$refs.upload.clearFiles()
               return false;
            }
         },
         submitUpload() {
            if (this.$refs.upload.uploadFiles.length == 0) {
               this.$message.error('未选择文件')
               return
            }
            this.uploading = true
            this.$refs.upload.submit();
         },
         onSuccess(response, file, fileList) {
            this.$refs.upload.clearFiles()
            this.uploadDia = false
            this.uploading = false
            if (response.code == 201) {
               this.$message.error(response.message)
               return
            }
            this.$message.success('上传成功')
            this.selectList()
         },
         onError(err, file, fileList) {
            this.$message.error('上传失败')
            this.$refs.upload.clearFiles()
            this.uploading = false
         },
         isDisabled(label) {
            if (this.data.disabledUp == undefined) {
               return
            }
            for (var i = 0; i < this.data.disabledUp.length; i++) {
               if (label == this.data.disabledUp[i]) {
                  return true
               }
            }
            return false
         },
         getSummaries(param) {
            const {
               columns,
               data
            } = param;
            const sums = [];
            columns.forEach((column, index) => {
               if (index === 0) {
                  sums[index] = '合计';
                  return;
               }
               const values = data.map(item => Number(item[column.property]));
               if (this.data.countFleid == undefined) {
                  sums[index] = ''
                  return
               }
               if (this.data.countFleid.filter(str => {
                     return str === column.property
                  }).length > 0) {
                  sums[index] = values.reduce((prev, curr) => {
                     const value = Number(curr);
                     if (!isNaN(value)) {
                        return prev + curr;
                     } else {
                        return prev;
                     }
                  }, 0);
                  sums[index] += '';
               } else {
                  sums[index] = '';
               }
            });
            return sums;
         },
         handleLinkEvent(row, val) {
            if (val.method == 'doDiy') {
               this.main(row, val)
            } else {
               this.$parent[val.method](row)
            }
         },
         getWidth() {
            let count = 0
            this.data.do.forEach(a => {
               count += a.font.length
            })
            return count * 15 + 60 + 'px'
         }
      }
   }
</script>
</script>