UNPKG

@hzy1123581324/z-view-ui

Version:

z-view-ui是使用vue3开发的组件,开发中,有部分组件功能未实现,慎用

67 lines (64 loc) 1.46 kB
import { reactive } from "vue"; import { computed, watch, } from "vue"; // 是否全选钩子函数 export function useSelect(targetList,{ valueKey: 'id', }){ const selList = reactive([]); let isAll = computed({ get(){ if(selList.length==0){ return false; } for(let i =0;i<selList.length;i++){ if(!selList[i]){ return false; } } return true; }, set(val){ if(val == false){ selList.length = 0; } if(val == true){ for(let i=0;i<targetList.length;i++){ selList[i] = valueKey? targetList[i][valueKey]: targetList[i]; } } } }) /** * @description 切换选中的状态,选中改为未选中,未选中改为已选择 * @param {Number} 选中的下标 */ function changeSelect(index){ if(selList[index]){ selList[index] = null; } else { selList[index] = valueKey?targetList[i][valueKey]:targetList[i]; } } /** * @description 返回选择的值没有空值的数组 * @returns {Array} list */ function valueList(){ const list = []; for(let i=0;i<selList.length;i++){ if(selList[i]){ list.push(selList[i]); } } return list } return { isAll, selList, changeSelect, valueList } }