index.vue 4.21 KB
<template>
  <next-tree :changeVerify="changeVerify" :title="getTitle" ref="nextTreeRef" :checkStrictly="checkStrictly"
    :selectParent="selectParent" :multiple="multiple" :treeData="treeData" @cancel="close" @confirm="onconfirm">
    <!-- label插槽示意代码 -->
    <!-- <template #label="{data: {id, label, iconSrc, prev, post}}">
			<view class="line-block">
				<image class="img" v-if="iconSrc" :src="iconSrc"></image>
				<text space="nbsp" v-if="prev">{{prev}}&nbsp;</text><text>{{label}}</text><text space="nbsp" v-if="post">&nbsp;{{post}}</text>
			</view>
		</template> -->
    <!-- <template #topBar>
			<view style="color: #666;padding:5px;"><text style="font-size: 12px;">历史记录</text></view>
			<view style="display: flex;justify-content: space-between;padding-bottom: 10px;border-bottom: 1rpx solid #f0f0f0;">
				<button @click="checkedFunc('1-3-3-4')"  :style="'background-color:'+ (activeId === '1-3-3-4' ? '#f9ae3d' : '#ccc') + ';color:#fff;margin: 5px'" size="mini">北京区-4</button>
				<button @click="checkedFunc('3-1-2')"  :style="'background-color:'+ (activeId === '3-1-2' ? '#f9ae3d' : '#ccc') + ';color:#fff;margin: 5px'" size="mini">海珠区-2</button>
				<button @click="checkedFunc('3-1-6')"  :style="'background-color:'+ (activeId === '3-1-6' ? '#f9ae3d' : '#ccc') + ';color:#fff;margin: 5px'" size="mini">海珠区-5</button>
			</view>
		</template> -->
  </next-tree>
</template>

<script setup>
import { nextTick, ref, unref } from 'vue';
// @ts-ignore
import nextTree from '../next-tree/next-tree.vue';
const props = defineProps({
  dataList: {
    type: Array,
    default: []
  },
  valueKey: {
    type: String,
    default: 'id'
  },
  multiple: {
    type: Boolean,
    default: true
  },
  selectParent: {
    type: Boolean,
    default: false
  },
  checkStrictly: {
    type: Boolean,
    default: false
  },
  onconfirm: {
    type: Function,
    default: () => { }
  }

})
const treeData = ref([])
const nextTreeRef = ref()
function getTitle(checked) {
  return `已选:${checked.length}位驾驶员`
}
function changeVerify(current, chooseList) {
  // 注意:返回非空字符串会阻止原有行为,并提示返回的字符串
  // 如果函数体不做return返回值,即验证通过,控件正常处理业务
  // 限制条件
  if (chooseList) {
    for (let index = 0; index < chooseList.length; index++) {
      const element = chooseList[index];
      if (current.id.indexOf(element.id) === -1 && element.label.indexOf(current.label) != -1) {
        return "该驾驶员已经被选中了"
      }
    }
  }
}
function open(dataList) {
  treeData.value = handlerTreeData(dataList)
  treeData.value = treeData.value.filter(item => {
    return item.children[0].id
  })
  setTimeout(() => {
    nextTick(() => {
      unref(nextTreeRef).showTree = true
    })
  }, 0)
}

function handlerTreeData(dataList) {
  return dataList
    .map((item, index) => {
      return {
        "id": (index + 1),
        "licensePlateNumber": item.licensePlateNumber,
        "containerVolume": item.containerVolume + "方车",
        "label": item.containerVolume + "方车" + '-' + item.licensePlateNumber,
        "children": item.personnelInfo.map((childrenItem, childrenIndex) => {
          return {
            "id": (index + 1) + '-' + (childrenIndex + 1),
            "garOrderContainerVolume": item.containerVolume,
            "tel": childrenItem.tel,
            "name": childrenItem.name,
            "label": childrenItem.name + '-' + childrenItem.tel,
            "checked": childrenItem.checked,
            "carCode": item.licensePlateNumber,
            "disabled": childrenItem.checked ? true : childrenItem.tel ? false : true
          }
        })
      }
    })
}
function cleanTreeData(treeData) {
  treeData.map(item => {
    item.checked = false
    if (item.children && item.children.length) {
      cleanTreeData(item.children)
    }
  })
}
function close() {
  // 清除treeData的选中状态
  cleanTreeData(unref(treeData))
}
defineExpose({
  open, close, nextTreeRef
})
</script>
<style lang="scss">
.line-block {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;

  .img {
    width: 40rpx;
    height: 40rpx;
    border-radius: 10rpx;
    margin: 0 20rpx;
  }
}
</style>