当前位置: 首页 > wzjs >正文

在百度做橱柜网站wordpress上线

在百度做橱柜网站,wordpress上线,江苏省网站备案系统,软件设计模式最近遇到了在级联选择器上添加全选框的需求 ,但是项目使用的是Vue2 Element UI的架构,而我们都知道Element UI提供的级联选择器el-cascader是不支持全选框的,而我又没有在网上找到适合我项目的实现,索性自己实现一个组件&#xf…

最近遇到了在级联选择器上添加全选框的需求 ,但是项目使用的是Vue2 + Element UI的架构,而我们都知道Element UI提供的级联选择器el-cascader是不支持全选框的,而我又没有在网上找到适合我项目的实现,索性自己实现一个组件(源码附在文末,需要自取)

实现效果:

主要功能实现:

1. 全选按钮可选

通过属性 showAllNode 控制在多选模式下全选按钮的添加与否(注意,单选模式下没有全选按钮):

props: {showAllNode: {type: Boolean,default: false}
},
methods: {
async fetchDictTree() {let children = this.data.data[0].children || []// 添加全选节点this.treeData = this.showAllNode ? [this.allNode, ...children] : children// 获取所有节点值(一维数组)this.allNodeValues = this.getAllNodeValues()// 修改初始化逻辑if (this.showAllNode) {// 选中所有节点(包括全选节点)this.selectedValue = ['0', ...this.allNodeValues]this.lastSelectedValue = [...this.selectedValue]} else {this.selectedValue = []this.lastSelectedValue = []}this.$emit('input', this.selectedValue)this.$emit('ready', true)},
}
2. 全选按钮与其他节点的联动效果:
  • 点击全选按钮所有节点都被选中,非全选状态下全选按钮不被选中
    if (this.showAllNode) {// 检查是否包含全选节点const hasAll = value.includes('0')const prevHasAll = this.lastSelectedValue.includes('0')if (hasAll && !prevHasAll) {// 选中全选,同时选中所有节点const allValues = ['0', ...this.allNodeValues]this.selectedValue = allValuesthis.lastSelectedValue = [...allValues]// 只返回 ['0'] 表示全选this.$emit('input', ['0'])this.$emit('change', ['0'])return} else if (!hasAll && prevHasAll) {// 取消全选,清空所有选中setTimeout(() => {this.selectedValue = []this.lastSelectedValue = []this.$emit('input', [])this.$emit('change', [])}, 0)return}// 检查是否所有节点都被选中const allSelected = this.allNodeValues.every(nodeValue =>value.includes(nodeValue))// 如果所有节点都被选中,但全选节点没有被选中,则添加全选节点if (allSelected && !hasAll) {const newValue = ['0', ...value]this.selectedValue = newValuethis.lastSelectedValue = [...newValue]// 只返回 ['0'] 表示全选this.$emit('input', ['0'])this.$emit('change', ['0'])return}// 如果不是所有节点都被选中,但全选节点被选中了,则移除全选节点if (!allSelected && hasAll) {const newValue = value.filter(v => v !== '0')this.selectedValue = newValuethis.lastSelectedValue = [...newValue]this.$emit('input', newValue)this.$emit('change', newValue)return}}
3. 全选按钮选中时传回的节点数据(dictKey)的值为0而非所有节点key值
watch: {value: {handler(val) {// 如果传入的值是 ['0'],表示选中全选节点if (Array.isArray(val) && val.length === 1 && val[0] === '0' && this.showAllNode) {// 内部选中所有节点this.selectedValue = ['0', ...this.allNodeValues]} else if (!this.multiple && Array.isArray(val) && val.length === 1) {// 单选模式下,如果传入的是数组,取第一个元素this.selectedValue = val[0]} else {this.selectedValue = val}},immediate: true},},

组件使用:

<template><div><classify-cascader v-model="classify" placeholder="请选择试卷分类" multiplecheckStrictly show-all-node width="200px" /></div>
</template><script>
import classifyCascader from '@/components/classify-cascader/index.vue'export default {name: 'indexVue',components: {classifyCascader},data() {return {classify: []}}
}
</script>
  • 笔者已经分装好了一个组件,可以直接使用,如果下拉框的数据是从后端获取的话,改一下fetchDictTree() 方法中 children 的数据赋值代码就可以,类似于:
const { data } = await APIgetDictTree(params)
let children = data.data[0].children || []

组件实现:

<template><el-cascader v-model="selectedValue" :options="treeData" :props="cascaderProps" :placeholder="placeholder":disabled="disabled" :clearable="clearable" :style="{ width: width }" collapse-tags @change="handleChange" />
</template><script>export default {name: 'ClassifyCascader',props: {value: {type: [String, Array],default: () => []},checkStrictly: {type: Boolean,default: false},multiple: {type: Boolean,default: false},showAllNode: {type: Boolean,default: false},disabled: {type: Boolean,default: false},placeholder: {type: String,default: '请选择'},clearable: {type: Boolean,default: true},width: {type: String,default: '200px'}},data() {return {treeData: [],selectedValue: this.value,cascaderProps: {value: 'dictKey',label: 'dictValue',children: 'children',checkStrictly: this.checkStrictly,multiple: this.multiple,emitPath: false},allNode: {dictKey: '0',dictValue: '全选'},lastSelectedValue: [],allNodeValues: [], // 存储所有节点的值(一维数组)data: {data: [{children: [{dictKey: '1',dictValue: '分类1'},{dictKey: '2',dictValue: '分类2',children: [{dictKey: '2-1',dictValue: '子分类2-1',children: []},{dictKey: '2-2',dictValue: '子分类2-2',children: []}]}]}]}}},watch: {value: {handler(val) {// 如果传入的值是 ['0'],表示选中全选节点if (Array.isArray(val) && val.length === 1 && val[0] === '0' && this.showAllNode) {// 内部选中所有节点this.selectedValue = ['0', ...this.allNodeValues]} else if (!this.multiple && Array.isArray(val) && val.length === 1) {// 单选模式下,如果传入的是数组,取第一个元素this.selectedValue = val[0]} else {this.selectedValue = val}},immediate: true},},created() {this.fetchDictTree()},methods: {// 刷新数据refreshData() {return this.fetchDictTree()},async fetchDictTree() {let children = this.data.data[0].children || []// 添加全选节点this.treeData = this.showAllNode ? [this.allNode, ...children] : children// 获取所有节点值(一维数组)this.allNodeValues = this.getAllNodeValues()// 修改初始化逻辑if (this.showAllNode) {// 选中所有节点(包括全选节点)this.selectedValue = ['0', ...this.allNodeValues]this.lastSelectedValue = [...this.selectedValue]} else {this.selectedValue = []this.lastSelectedValue = []}this.$emit('input', this.selectedValue)this.$emit('ready', true)},handleChange(value) {if (this.showAllNode) {// 检查是否包含全选节点const hasAll = value.includes('0')const prevHasAll = this.lastSelectedValue.includes('0')if (hasAll && !prevHasAll) {// 选中全选,同时选中所有节点const allValues = ['0', ...this.allNodeValues]this.selectedValue = allValuesthis.lastSelectedValue = [...allValues]// 只返回 ['0'] 表示全选this.$emit('input', ['0'])this.$emit('change', ['0'])return} else if (!hasAll && prevHasAll) {// 取消全选,清空所有选中setTimeout(() => {this.selectedValue = []this.lastSelectedValue = []this.$emit('input', [])this.$emit('change', [])}, 0)return}// 检查是否所有节点都被选中const allSelected = this.allNodeValues.every(nodeValue =>value.includes(nodeValue))// 如果所有节点都被选中,但全选节点没有被选中,则添加全选节点if (allSelected && !hasAll) {const newValue = ['0', ...value]this.selectedValue = newValuethis.lastSelectedValue = [...newValue]// 只返回 ['0'] 表示全选this.$emit('input', ['0'])this.$emit('change', ['0'])return}// 如果不是所有节点都被选中,但全选节点被选中了,则移除全选节点if (!allSelected && hasAll) {const newValue = value.filter(v => v !== '0')this.selectedValue = newValuethis.lastSelectedValue = [...newValue]this.$emit('input', newValue)this.$emit('change', newValue)return}}// 正常情况下的处理this.lastSelectedValue = [...value]const outputValue = Array.isArray(value) ? value : [value]this.$emit('input', outputValue)this.$emit('change', outputValue)},// 获取所有节点的值(一维数组)getAllNodeValues() {const allValues = []const traverse = (nodes) => {nodes.forEach(node => {if (node.dictKey === '0') returnallValues.push(node.dictKey)if (node.children && node.children.length > 0) {traverse(node.children)}})}traverse(this.treeData)return allValues}}
}
</script>

大家有什么问题可以评论区交流一下,我看到了也会回复的。


文章转载自:

http://QbuFsTAu.gmyhq.cn
http://gbO0B93H.gmyhq.cn
http://k0DS7Q1z.gmyhq.cn
http://BbDZtTTQ.gmyhq.cn
http://yS1Bb3jP.gmyhq.cn
http://9YnkSpWQ.gmyhq.cn
http://ARLDWmAH.gmyhq.cn
http://0WFFsHlI.gmyhq.cn
http://EqShsrXW.gmyhq.cn
http://UFexrPL2.gmyhq.cn
http://rCPCs46I.gmyhq.cn
http://DAUDuSFy.gmyhq.cn
http://hFxxxfxa.gmyhq.cn
http://uNsGB52Z.gmyhq.cn
http://dqpTTeFh.gmyhq.cn
http://5T4W2IAl.gmyhq.cn
http://xde1TKPQ.gmyhq.cn
http://KAWsXLdV.gmyhq.cn
http://gDk9mZMv.gmyhq.cn
http://aAN7Ipdm.gmyhq.cn
http://lLk737Ou.gmyhq.cn
http://dXfnflj0.gmyhq.cn
http://AzFCSmyc.gmyhq.cn
http://BZwqyA3A.gmyhq.cn
http://43tHpEd0.gmyhq.cn
http://SvbxJ96A.gmyhq.cn
http://4RnHAii6.gmyhq.cn
http://4imNSus5.gmyhq.cn
http://izJKYszt.gmyhq.cn
http://gxnqRQsK.gmyhq.cn
http://www.dtcms.com/wzjs/725834.html

相关文章:

  • 网站上线做什么wordpress 文章调用
  • 临夏州住房和城乡建设厅网站网站无法下载视频 怎么做
  • 如何优化公司网站有创意的文创产品
  • 最近发生的重大军事新闻普通网站 seo 多少钱
  • json取数据做网站网站优化主旨
  • 电子商务网站建设试卷与答案网站建设合同 下载
  • 邢台网站建设最新报价嘉定专业做网站
  • 建怎么网站比较赚钱个人微信网站怎么做
  • 南昌建站方案外链管理
  • 重庆做网站changeke做外贸网站的好处
  • 青岛seo整站优化公司申请建设单位门户网站的请示
  • 上海官方网站建设网站美观界面
  • 阿里云建设网站教学wordpress如何仿站
  • 怎么创建网站合肥网站制作费用
  • 成都全网营销型网站二级域名做网站注意
  • 广州建设工程交易中心改版宜宾seo网站建设
  • 快飞建站网站建设公司网站
  • 如何让网站快速收录配置安装环境 wordpress 阿里云
  • 海北州公司网站建设网站建设企业排名
  • 在线做的网站杰恩设计网站是谁做的
  • 国外做旅游攻略的网站好青海西宁网站建设
  • 植物网站模板网站经营网络备案信息管理系统
  • 苏州画廊网站建设wordpress编辑代码
  • 东莞企网站建设网页制作软件免费版dw
  • 恢复被百度k网站 关键词收录上海专业做网站价格
  • 做网站贵不做视频网站 视频放在哪
  • 视频网站开发代码公司的网站 优帮云
  • 黑河北京网站建设网站集群建设通知
  • 做酒店网站设计新的网站平台如何做地推
  • 网站架构包含哪几个部分品牌建设概念