relateStoreDialog.vue 4.14 KB
<!-- 关联门店弹窗 -->
<template>
  <d2-dialog
    title="关联门店"
    :isVisible.sync="relateStoreVisible"
    width="70%"
    btnOkLabel="关联"
    @confirmDialog="handleRelateStoreConfirm"
    @closeDialog="handleRelateStoreClose"
    class="relateStore"
  >
    <slot></slot>
    <el-row>
      <el-col :span="12">
        <el-table
          ref="seletingTable"
          :data="selectingData"
          height="500px"
          class="table"
          border
          @select="handleChange"
          @select-all="handleAll"
        >
          <el-table-column
            type="selection"
            width="55"
            align="center"
            :selectable="selectable"
          ></el-table-column>
          <!-- <el-table-column>
            <template slot-scope="scope">{{scope.row.beServed}}</template>
          </el-table-column> -->
          <el-table-column
            prop="merchantName"
            label="门店名称"
            align="center"
          ></el-table-column>
        </el-table>
        <el-pagination
          small
          background
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total"
        ></el-pagination>
      </el-col>
      <el-col :span="12">
        <el-table
          :data="selectedData"
          class="table"
          border
          height="500px"
        >
          <el-table-column
            prop="merchantName"
            align="center"
            label="门店名称"
          ></el-table-column>
        </el-table>
      </el-col>
    </el-row>
  </d2-dialog>
</template>

<script>
export default {
  name: 'relateStoreDialog',
  data() {
    return {
      selectedData: [],
      selectingData: [],
      // 分页
      total: 0,
      currentPage: 1,
      pageSize: 10
    }
  },

  props: {
    relateStoreVisible: {
      type: Boolean,
      default: false
    },
    storeApi: {
      type: Function,
      default: () => { }
    },
    storeSelected: {
      type: Array,
      default: () => []
    },
    storeSearchForm: {
      type: Object,
      default: () => { }
    }
  },

  mounted() {
    this.selectedData = this.storeSelected
    this.getSelectingList()
  },

  methods: {
    selectable(row, index) {
      return row.hasOwnProperty('beServed') ? !row.beServed : true
    },
    recoverySelection() {
      var exitIds = this.selectedData.map(item => item.merchantId)
      this.selectingData.forEach(item => {
        exitIds.includes(item.merchantId) && this.$refs.seletingTable.toggleRowSelection(item, true)
      })
    },
    getSelectingList() {
      this.storeApi({
        ...this.storeSearchForm,
        currentPage: this.currentPage,
        pageSize: this.pageSize
      }).then(res => {
        console.log(res)
        this.selectingData = res.list
        this.total = res.total
        this.$nextTick(() => {
          this.recoverySelection()
        })
      })
    },
    handleChange(selection, row) {
      var addOrDelete = !!selection.find(item => item.merchantId === row.merchantId)
      addOrDelete
        ? this.selectedData.push(row)
        : (this.selectedData = this.selectedData.filter(item => item.merchantId !== row.merchantId))
    },
    handleAll(selection) {
      this.selectingData.forEach(item => {
        var isExit = this.selectedData.findIndex(subItem => subItem.merchantId === item.merchantId)
        if (selection.length && (isExit === -1)) {
          this.selectedData.push(item)
        } else if (!selection.length && (isExit !== -1)) {
          this.selectedData.splice(isExit, 1)
        }
      })
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getSelectingList()
    },
    handleRelateStoreConfirm() {
      this.$emit('handleRelateStoreConfirm', this.selectedData)
    },
    handleRelateStoreClose() {
      this.$emit('handleRelateStoreClose')
    }
  }
}

</script>
<style>
.relateStore .table {
  margin: 0 auto 20px !important;
  width: 98%;
}
.relateStore .el-table .cell {
  line-height: 20px !important;
}
.relateStore .el-table .cell {
  padding: 0 8px !important;
}
</style>