当前位置:首页 > VUE

vue实现列表联动插件

2026-02-23 11:48:21VUE

Vue 列表联动插件实现方案

基于 v-model 的双向绑定

通过 Vue 的 v-model 实现父子组件数据同步,适用于简单联动场景。

<template>
  <div>
    <parent-list v-model="selectedItems" :items="parentItems" />
    <child-list :items="filteredChildItems" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedItems: [],
      parentItems: [...],
      allChildItems: [...]
    }
  },
  computed: {
    filteredChildItems() {
      return this.allChildItems.filter(item => 
        this.selectedItems.includes(item.parentId)
      )
    }
  }
}
</script>

使用事件总线通信

通过 Vue 的 EventBus 实现跨组件通信,适合复杂层级结构。

// event-bus.js
import Vue from 'vue'
export const EventBus = new Vue()

// ParentComponent.vue
EventBus.$emit('parent-selected', selectedIds)

// ChildComponent.vue
EventBus.$on('parent-selected', ids => {
  this.filteredItems = this.allItems.filter(item => ids.includes(item.parentId))
})

封装可复用插件

创建独立插件实现标准化列表联动功能。

// list-linkage.js
const ListLinkage = {
  install(Vue) {
    Vue.mixin({
      methods: {
        $updateLinkedList(sourceKey, targetKey, filterFn) {
          this.$watch(sourceKey, newVal => {
            this[targetKey] = this.allItems.filter(item => filterFn(item, newVal))
          }, { deep: true })
        }
      }
    })
  }
}

// main.js
import ListLinkage from './list-linkage'
Vue.use(ListLinkage)

// 组件内使用
this.$updateLinkedList('selectedParents', 'filteredChildren', (child, parents) => {
  return parents.some(p => p.id === child.parentId)
})

基于 Provide/Inject 的层级传递

适合深层嵌套组件结构,通过依赖注入实现数据共享。

// 祖先组件
export default {
  provide() {
    return {
      listLinkage: {
        selected: Vue.observable([]),
        update: this.updateSelection
      }
    }
  }
}

// 后代组件
export default {
  inject: ['listLinkage'],
  methods: {
    handleSelect(item) {
      this.listLinkage.update(item.id)
    }
  }
}

性能优化建议

对于大数据量场景应添加防抖处理和虚拟滚动。

import debounce from 'lodash/debounce'

export default {
  methods: {
    updateFilter: debounce(function(searchTerm) {
      this.filtered = this.items.filter(i => 
        i.name.includes(searchTerm)
      )
    }, 300)
  }
}

完整插件示例

以下是封装成 npm 包的完整插件结构示例:

// index.js
export default {
  install(Vue, options = {}) {
    const defaultOptions = {
      debounceTime: 200,
      linkageKey: 'linkageId'
    }

    const config = { ...defaultOptions, ...options }

    Vue.directive('list-linkage', {
      bind(el, binding, vnode) {
        const source = binding.value.source
        const target = binding.value.target
        const filterFn = binding.value.filter || defaultFilter

        vnode.context.$watch(
          source, 
          debounce(newVal => {
            vnode.context[target] = vnode.context.allItems
              .filter(item => filterFn(item, newVal))
          }, config.debounceTime),
          { deep: true }
        )
      }
    })

    function defaultFilter(item, sourceVal) {
      return sourceVal.includes(item[config.linkageKey])
    }
  }
}

使用时可全局注册或按需引入,通过指令方式实现列表联动:

vue实现列表联动插件

<template>
  <div v-list-linkage="{ source: 'selected', target: 'filtered' }">
    <!-- 联动列表内容 -->
  </div>
</template>

标签: 插件列表
分享给朋友:

相关文章

网页制作css 列表

网页制作css 列表

创建CSS列表样式 使用CSS可以自定义HTML列表(有序列表<ol>和无序列表<ul>)的外观,包括项目符号、缩进、颜色等。以下是一些常见的列表样式方法: 基础列表样式 通…

h5 实现列表

h5 实现列表

列表实现方法 在HTML5中,可以通过多种方式实现列表,包括无序列表、有序列表和自定义列表。以下是具体的实现方法。 无序列表 无序列表使用<ul>标签,列表项使用<li>标签…

vue实现列表权限

vue实现列表权限

实现列表权限控制的方法 在Vue中实现列表权限控制通常涉及前端逻辑与后端数据的配合。以下是几种常见方法: 基于角色或权限码的渲染控制 通过v-if或v-show指令根据用户权限动态渲染列表项: &…

vue实现日历插件

vue实现日历插件

Vue 日历插件实现方案 使用现成组件库 推荐直接使用成熟的 Vue 日历组件,如: V-Calendar:专为 Vue 设计的轻量级日历组件 FullCalendar:功能强大的日历库,有 Vue…

vue实现列表滚动

vue实现列表滚动

Vue 实现列表滚动的方法 使用 CSS 实现滚动 通过 CSS 的 overflow 属性可以轻松实现列表滚动。在 Vue 的模板中,为列表容器添加 overflow-y: auto 或 overf…

vue实现伸缩列表

vue实现伸缩列表

Vue 实现伸缩列表的方法 使用 Vue 实现伸缩列表可以通过动态绑定样式或类名,结合过渡效果实现平滑的展开和收起动画。以下是几种常见实现方式: 动态绑定高度 通过 v-if 或 v-show 控制…