PointerConfig.vue 8.36 KB
<template>
  <baseDialog
    :is-show.sync="dialogShow"
    :config="dialogConfig.setting"
    :dialog-config="dialogConfig.config"
    class="dialog"
    @handleClick="handleClick"
  >
    <div
      class="dialog-content"
      v-loading="loading"
    >
      <div class="search-box">
        <div class="label">指标名称:</div>
        <el-input
          class="input"
          size="small"
          maxlength="10"
          v-model="searchValue"
          clearable
        ></el-input>
        <el-button
          class="btn"
          type="primary"
          size="small"
          @click="getDataConfig"
        >查询</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        ref="table"
        :show-header="false"
        row-key="id"
        current-row-key="id"
      >
        <el-table-column
          class-name="draggable-icon"
          width="40"
          align="center"
          v-if="!searchFlag"
        >
          <div class="icon"></div>
        </el-table-column>
        <el-table-column
          type="index"
          width="60"
          align="center"
          v-if="!searchFlag"
        >
        </el-table-column>
        <template v-for="(item,index) in  tableParams.tableColumns">
          <el-table-column
            :label="item.label"
            :prop="item.prop"
            :width="item.width"
            :min-width="item.minWidth"
            align="center"
            :key="index"
          >
            <template slot-scope="scope">
              <template v-if="item.formatter">
                <span
                  v-if="item.html"
                  v-html="item.formatter(scope.row)"
                />
                <template v-if="!item.html">
                  <span :class="item.className ? item.className(scope.row) : ''" />
                  <span v-text="item.formatter(scope.row)" />
                </template>
              </template>
              <span v-else>{{ scope.row[item.prop] || "-" }}</span>
            </template>
          </el-table-column>
        </template>
        <el-table-column
          :align="tableParams.tableOption.align || 'center'"
          :width="tableParams.tableOption.width || 'auto'"
          class-name="table-option"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="hide(scope)"
              :key="btnKey"
            >{{scope.row.hiddenFlag == 1?'显示':'隐藏'}}</el-button>
            <template v-if="tableData.length > 1 && !searchFlag">
              <span
                class="icon el-icon-top"
                @click="changeSort(scope.$index,0)"
                v-if="scope.$index != 0"
              ></span>
              <span
                class="icon el-icon-bottom"
                @click="changeSort(scope.$index,tableData.length - 1)"
                v-if="scope.$index != tableData.length - 1"
              ></span>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </baseDialog>
</template>

<script type='text/ecmascript-6'>
import sortablejs from "sortablejs"
import { getKpiMetric, updateKpiMetric, updateOneKpiMetric} from '@/api/earlyWarningManager'

export default {
  name: '',
  props: {
    show: Boolean,
    scope: Number
  },
  data () {
    return {
      btnKey: Date.now(),
      loading: true,
      searchValue: '',
      dialogShow: false,
      dialogConfig: {
        setting: {
          showClose: true,
          appendToBody: true,
          btnList: [],
          isShowFooter: true,
          confirm_name: '确定',
          cancel_name: '取消'
        },
        config: {
          title: "编辑指标",
          width: "1080px"
        }
      },
      tableData: [],
      tableParams: {
        tableOption: {
          width: '100'
        },
        tableColumns: [
          {
            minWidth: 110,
            prop: 'name'
          },
          {
            minWidth: 135,
            prop: 'definition'
          },
        ]
      },
      searchFlag: false
    }
  },
  watch: {
    show (n) {
      this.dialogShow = n
      if (n) {
        this.tableData = []
        this.searchValue = ''
        this.init()
      }
    },
    dialogShow (n) {
      this.$emit('update:show', n)
    }
  },
  computed: {
    uuid () {
      return this.$store.state.d2admin.user.info.detail.uuid
    }
  },
  mounted () {
  },
  methods: {
    init () {
      this.getDataConfig()
    },
    async getDataConfig () {
      try {
        const { searchValue, uuid, scope } = this,
          res = await getKpiMetric({
            scope,
            name: searchValue,
            uuid
          })
        this.searchFlag = searchValue ? true : false
        this.tableData = res || []
        console.log(res, '===')
        this.loading = false
        this.$nextTick(() => {

          let el = this.$refs.table.$el.querySelector(`.el-table__body-wrapper > table > tbody`)
          this.$nextTick(() => {
            new sortablejs(el, {
              handle: '.draggable-icon',
              ghostClass: "sortable-drag",
              onEnd: (e) => {
                const { oldIndex, newIndex } = e
                this.changeSort(oldIndex, newIndex)
              }
            })
          })
        })
      } catch (error) {
        this.loading = false
      }
    },
    async handleClick () {
      try {
        let userKpiMetricAddRequestDTOS = [],
          { scope, tableData, uuid, searchFlag } = this
        tableData.map((e, index) => {
          userKpiMetricAddRequestDTOS.push({
            id: e.id,
            loginUuid: uuid,
            sort: searchFlag ? e.defaultSortOrder : index + 1,
            hiddenFlag: e.hiddenFlag
          })
        })
        console.log(userKpiMetricAddRequestDTOS, '-------')
        const apiFun = searchFlag ? updateOneKpiMetric : updateKpiMetric
        const res = await apiFun({
          userKpiMetricAddRequestDTOS,
          scope
        })
        if (res) {
          this.$message.success('保存成功')
          this.dialogShow = false
          this.$emit('update')
        }
      } catch (error) {
        if (typeof error === 'string') this.$message.error(error)
      }
    },
    hide (scope) {
      scope.row.hiddenFlag = scope.row.hiddenFlag == 1 ? 0 : 1
      this.btnKey = Date.now()
    },
    changeSort (oldIndex, newIndex) {
      this.tableData.splice(newIndex, 0, this.tableData.splice(oldIndex, 1)[0])
    }
  }
}
</script>

<style scoped lang='scss'>
.dialog {
  ::v-deep.el-dialog__body {
    max-height: 60vh !important;
    overflow-y: auto;
  }
  &-content {
    // padding: 5px 0;

    .search-box {
      display: flex;
      align-items: center;
      margin-bottom: 32px;
      .label {
        color: #333333;
        font-size: 14px;
        display: flex;
        align-items: center;
        justify-content: flex-end;
      }
      .input {
        width: 155px;
      }
      .btn {
        margin-left: auto;
      }
    }
  }
  .input-box {
    padding: 4px 10px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  ::v-deep.el-table {
    .cell {
      padding-left: 0;
    }
    .sortable-drag {
      box-shadow: 0 6px 20px 0 #00000040;
      background: #fff;
    }
    .table-option {
      span {
        margin-right: 8px;
      }
      .icon {
        position: relative;
        color: #2a58ad;
        font-size: 15px;
        font-weight: 700;
        cursor: pointer;
        &:hover {
          opacity: 0.7;
        }
        &::before {
          font-size: 14px;
        }
        &::after {
          width: 8px;
          height: 1.33px;
          content: "";
          position: absolute;
          background: #2a58ad;
          left: 50%;
          transform: translateX(-50%);
        }
      }
      .el-icon-top {
        transform: translateY(3px);
        padding-top: 1px;
        &::after {
          top: 0;
        }
      }
      .el-icon-bottom {
        padding-bottom: 1px;
        &::after {
          bottom: 0;
        }
      }
    }
  }
  ::v-deep.draggable-icon {
    cursor: grab;
    .icon {
      width: 10.67px;
      height: 7px;
      border-top: 1px solid #333;
      border-bottom: 1px solid #333;
      margin: 0 auto;
      display: flex;
      align-items: center;
      &::after {
        content: "";
        width: 100%;
        height: 1px;
        background: #333;
        display: block;
      }
    }
  }
}
</style>