partAlternateLinkTable.vue 5.51 KB
Newer Older
wangdanlei's avatar
wangdanlei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
/**
* @Description: 全局替换件
* @author 李亦康
* @date 2020/10/26
*/
<template>
  <div class="dee-table">
    <dee-tools slot="header" :tools="tools" style="margin-bottom:10px" />
    <dee-table
      ref="multipleTable"
      :data="tableData"
      :columns="columns"
      :selection-row="[]"
      @selection-change="handleSelectionChange"
    />
  </div>
</template>

<script>
// import { getPart, removeLinks } from '../../../../api/parts'
// import { filterData } from '../../../../utils/util'

export default {
  props: {
    value: {
      required: true,
      type: Array,
      defualt() {
        return []
      }
    }
  },
  data() {
    return {
      tools: [
        {
          name: '添加',
          icon: '/icons/c-add.png',
          handler: {
            click: () => {
              this.$emit('add', {
                dialogShow: true,
                type: 'global',
                el: this
              })
            }
          }
        },
        {
          name: '删除',
          icon: '/icons/c-creatbackups.png',
          handler: {
            click: () => {
              if (this.selectedData.length < 1) {
                this.$utils.showMessageWarning('请选择要删除的项')
                return
              }
              this.removeLinks(this.selectedData)
            }
          }
        }
      ],
      selectedData: [],
      columns: [
        { title: '编号', key: 'target.number', headerAlign: 'center' },
        { title: '名称', key: 'target.name', headerAlign: 'center' },
        { title: '版本', key: 'target.displayVersion', headerAlign: 'center' },
        { title: '类型', key: 'target.subTypeDisplayName', headerAlign: 'center' },
        {
          title: '开始时间', key: 'startTime', headerAlign: 'center',
          component: {
            show: true,
            render: (h, data, column, index) => {
              return (<el-date-picker size='small' type = 'date' valueFormat='yyyy-MM-dd' placeholder='请输入开始时间' v-model={data.startTime}
                on-change={(e) => {
                  if (!this.checkDateRate(data, 'startTime', index)) {
                    return
                  }
                  data.startTime = this.setStartTime(data.startTime)
                  if (data.id) {
                    data.operator = 'MODIFY'
                  }
                  this.modifyLinks(data)
                }}/>)
            }
          }
        },
        {
          title: '结束时间', key: 'endTime', headerAlign: 'center',
          component: {
            show: true,
            render: (h, data, column, index) => {
              return (<el-date-picker size='small' type = 'date' valueFormat='yyyy-MM-dd' placeholder='请输入结束时间' v-model={data.endTime}
                on-change={(e) => {
                  if (!this.checkDateRate(data, 'endTime', index)) {
                    return
                  }
                  data.endTime = this.setEndTime(data.endTime)
                  if (data.id) {
                    data.operator = 'MODIFY'
                  }
                  this.modifyLinks(data)
                }}/>)
            }
          }
        },
        {
          title: '双向', key: 'bothway', headerAlign: 'center',
          component: {
            show: true,
            render: (h, data, column, index) => {
              return (<el-checkbox v-model={data.bothway}
                on-change={(e) => {
                  if (data.id) {
                    data.operator = 'MODIFY'
                  }
                  this.modifyLinks(data)
                }}/>)
            }
          }
        }
      ]
    }
  },
  computed: {
    tableData() {
      return this.value.filter(r => r.operator !== 'REMOVE')
    }
  },
  watch: {},
  mounted() {
  },
  methods: {
    checkDateRate(row, attr, index) {
      // 只设置一个值 不判断
      if (!row.startTime || !row.endTime) {
        return true
      }
      // 可以设置同一天
      if (row.startTime === row.endTime || this.$moment(row.startTime).isBefore(row.endTime)) {
        return true
      }
      this.$set(row, attr, '')
      this.$utils.showMessageWarning(attr === 'startTime' ? '开始时间必须小于结束时间' : '结束时间必须大于开始时间')
      return false
    },
    setEndTime(date) {
      return this.setTime(date, '23:59:59')
    },
    setStartTime(date) {
      return this.setTime(date, '00:00:00')
    },
    setTime(date, time) {
      if (!date) {
        return ''
      }
      if (date.indexOf(' ') > -1) {
        return date
      }
      return date + ' ' + time
    },
    toggleSelection() {
      this.$refs.multipleTable.clearSelection()
    },
    handRowDataChange(row) {
      if (row.operator !== 'ADD') {
        row.operator = 'MODIFY'
      }
    },
    handleSelectionChange(e) {
      this.selectedData = e
    },
    checkLinks(row, data) {
      if (row.id) {
        return row.id === data.id
      }
      if (row.target) {
        return row.target.id === data.target.id
      }
      return false
    },
    modifyLinks(data) {
      const idx = this.value.findIndex(r => this.checkLinks(r, data))
      if (idx !== -1) {
        this.value[idx] = data
        this.$emit('update', this.value)
      }
    },
    addLinks(items) {
      this.$emit('update', this.$utils.addObject2Links(items, this.value || [], 'target'))
    },
    removeLinks(items) {
      this.$emit('update', this.$utils.removeObject4Links(items, this.value, 'target'))
      this.toggleSelection()
    }
  }
}
</script>

<style>

</style>