基于antd-vue的简易穿梭框


<template>
  <div>
    <a-modal
      :visible="true"
      title="选择部门"
      @cancel="closeModal"
      width="560px"
    >
      <div class="gutter-example">
        <a-row :gutter="16">
          <a-col class="gutter-row" :span="12">
            <!-- 
              :infinite-scroll-disabled="busy"
              :infinite-scroll-distance="0"
              @scroll="scroll" -->
            <div
              class="gutter-box"
              ref="leftList"
            >
              <a-input
                allow-clear
                style="width: 220px; margin-bottom: 8px; padding-left: 8px"
                v-model="searchParmas.keyword"
                placeholder="请输入搜索内容"
                @change="onSearch"
              >
                <a-icon slot="prefix" type="search" />
              </a-input>
              <a-checkbox-group @change="onChooseOrg" v-model="chooseArray">
                <a-checkbox
                  class="item-style"
                  v-for="item in departmentList"
                  :key="item.id"
                  :value="item.id"
                  :disabled="item.disabled"
                  >{{ item.name }}
                </a-checkbox>
              </a-checkbox-group>
            </div>
          </a-col>
          <a-col class="gutter-row" :span="12">
            <div class="gutter-box">
              <div class="choose-title">
                已选部门({{ chooseList.length }}/10)
              </div>
              <div
                class="choose-item"
                v-for="item in chooseList"
                :key="item.id"
              >
                {{ item.name }}
                <span class="close-style" @click="deleteItem(item)">
                  <a-icon type="close" />
                </span>
              </div>
            </div>
          </a-col>
        </a-row>
      </div>
      <div slot="footer">
        <a-button @click="closeModal()">取消</a-button>
        <a-button type="primary" @click="handelOk()">确定</a-button>
      </div>
    </a-modal>
  </div>
</template>

<script>
import { organizationList } from "@/api/staff.js";
export default {
  props: {
    selectArray:{
      type:Array,
      default:()=> []
    }
  },
  data() {
    return {
      loading: false,
      departmentList: [],
      copyList: [],
      total: 0,
      searchParmas: {
        page: 1,
        pageSize: 500,
        keyword: "",
      },
      chooseList: [],
      busy: false,
      chooseArray:[]
    };
  },
  mounted() {
    this.getOrganizationList();
    this.initData()
  },
  methods: {
    // 初始化数据
    initData() {
      let arr = [],choose = []
      arr = this.selectArray
      if(arr.length > 0){
        arr.forEach( item => {
          choose.push(item.id)
        })
        this.chooseArray = choose
        this.chooseList = arr
      }
    },
    // 选择部门
    onChooseOrg(e) {
      // 是否在查询列表选择
      if(!this.searchParmas.keyword){
        this.chooseList = []
      }
      if (e.length > 0) {
        if(this.chooseList.length < 10){
          e.forEach( item => {
            let chooseResult = []
            let flag = this.chooseList.filter( (ite) => ite.id == item)
            // 表示已选数组里面不包含此数据
            if(flag.length == 0){
              chooseResult = this.departmentList.filter((ite) => ite.id == item);
            }
            let resultArr = [...chooseResult,...this.chooseList]
            this.chooseList = resultArr.filter((item, index, arr) => arr.indexOf(item, 0) === index)
          })
        }
        // 最多选择十个部门
        if (this.chooseList.length == 10) {
          this.disabledList();
        }else{
          this.departmentList.map((ele) => {
            this.$set(ele, "disabled", false);
          });
        }
      }
    },
    // 禁用可选列表
    disabledList() {
      this.searchParmas.keyword = null
      this.departmentList = this.copyList
      this.chooseArray = []
      this.chooseList.forEach( item => {
        this.chooseArray.push(item.id)
      })
      this.departmentList.map((ele) => {
        if (this.chooseArray.includes(ele.id)) {
          this.$set(ele, "disabled", false);
        } else {
          this.$set(ele, "disabled", true);
        }
      });
      
    },
    // 删除部门
    deleteItem(data) {
      this.chooseArray = this.chooseArray.filter( item => item != data.id)
      this.chooseList = this.chooseList.filter( item => item.id != data.id)
      this.handelChooseArray()
    },
    // 搜索后选中数据处理
    handelChooseArray() {
      this.searchParmas.keyword = null
      this.departmentList = this.copyList
      this.chooseArray = []
      this.chooseList.forEach( item => {
        this.chooseArray.push(item.id)
      })
      if(this.chooseList.length == 10){
        this.disabledList()
      }else{
        this.departmentList.map((ele) => {
          this.$set(ele, "disabled", false);
        });
      }
      
    },
    // 左侧部门树滑动加载
    // scroll() {
    //   this.$nextTick(() => {
    //     const el = this.$refs.leftList;
    //     const offsetHeight = el.offsetHeight;
    //     const scrollTop = el.scrollTop;
    //     const scrollHeight = el.scrollHeight;
    //     if (offsetHeight + scrollTop - scrollHeight >= 0) {
    //       this.searchParmas.page += 1;
    //       this.getOrganizationList();
    //     }
    //   });
    // },
    // 获取部门列表
    getOrganizationList(option) {
      if (this.loading) {
        return;
      }
      this.loading = true;
      organizationList(this.searchParmas)
        .then((res) => {
          if (res.errorCode == "00000") {
            if (res.data.records.length == 0) {
              this.loading = false;
              return;
            }
            if(this.chooseList.length == 10){
              res.data.records.forEach((item) => {
                item.disabled = true;
              });
            }else{
              res.data.records.forEach((item) => {
                item.disabled = false;
              });
            }
            if (option) {
              this.departmentList = res.data.records;
            } else {
              this.departmentList = res.data.records;
              this.copyList = this.departmentList;
            }
            this.total = res.data.total;
            this.loading = false;
          }
        })
        .catch((err) => {
          if (err) {
            this.loading = false;
          }
        });
    },
    // 搜索数据
    onSearch(e) {
      if (!e.target.value) {
        this.handelChooseArray()
      } else {
        this.searchParmas.page = 1;
        this.searchParmas.keyword = e.target.value;
        this.getOrganizationList("search");
      }
    },
    closeModal() {
      this.chooseList = []
      this.chooseArray = []
      this.$emit('closeModal')
    },
    handelOk() {
      this.$emit('getChooseData',this.chooseList)
    }
  },
  watch:{}
};
</script>

<style lang="less" scoped>
.gutter-example > .ant-row > div {
  background: transparent;
  border: 0;
}
.gutter-box {
  width: 100%;
  background: #00a0e9;
  padding: 8px 0;
  height: 349px;
  background: #ffffff;
  border-radius: 4px;
  opacity: 1;
  border: 1px solid #d9dadd;
  overflow-y: auto;
  overflow-x: hidden;
  color: #272e3b;
  .item-style {
    padding: 0 8px;
    margin: 8px 0 0;
    width: 100%;
    font-weight: 600;
  }
  .choose-title {
    font-weight: 600;
    height: 42px;
    line-height: 42px;
    padding: 0 8px;
  }
  .choose-item {
    line-height: 30px;
    font-weight: 600;
    padding: 0 8px;
    .close-style {
      color: #d9dadd;
      float: right;
      cursor: pointer;
      visibility: hidden;
    }
    &:hover {
      background: #f6f9ff;
      .close-style {
        visibility: inherit;
      }
    }
  }
}
</style>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

左撇子没秃头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值