Skip to content

Commit

Permalink
展开已选择项的所有父节点,并且居中显示,方便修改操作;搜索模式下展开已选项
Browse files Browse the repository at this point in the history
   @varlamov88 [Expand parent nodes of selected item. Scroll menu to selected item](riophae#310)

   使用选项: scrollPositionOnCenter = true

```
   <treeselect
            v-model="orgCode"
            :clearable="true"
            :scrollPositionOnCenter="true"
            :default-expand-level="0"
          />
```
  • Loading branch information
emacle committed Jul 12, 2020
1 parent c3a31a4 commit 7f168f4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
# clone @riophae/vue-treeselect
解决大数据集时,首字母为空格或者中文输入法搜索时卡顿现象
1. 解决大数据集时,首字母为空格或者中文输入法搜索时卡顿现象

https://github.com/riophae/vue-treeselect/issues/369
https://github.com/riophae/vue-treeselect/issues/369

2. 展开已选择项的所有父节点,并且居中显示,方便修改操作;搜索模式下展开已选项

@varlamov88 [Expand parent nodes of selected item. Scroll menu to selected item](https://github.com/riophae/vue-treeselect/pull/310)

使用选项: scrollPositionOnCenter = true

```
<treeselect
v-model="orgCode"
:clearable="true"
:scrollPositionOnCenter="true"
:default-expand-level="0"
/>
```

# vue-treeselect
[![npm](https://badgen.now.sh/npm/v/@riophae/vue-treeselect)](https://www.npmjs.com/package/@riophae/vue-treeselect) [![Build](https://badgen.now.sh/circleci/github/riophae/vue-treeselect)](https://circleci.com/gh/riophae/vue-treeselect/tree/master) [![Coverage](https://badgen.net/codecov/c/github/riophae/vue-treeselect)](https://codecov.io/gh/riophae/vue-treeselect?branch=master)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@emacle/vue-treeselect",
"version": "0.4.3",
"version": "0.4.6",
"description": "A multi-select component with nested options support for Vue.js",
"author": "Riophae Lee <[email protected]>",
"license": "MIT",
Expand Down
44 changes: 44 additions & 0 deletions src/mixins/treeselectMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,15 @@ export default {
type: [ Number, String ],
default: 999,
},

/**
* Set selected option to the center of the menu, if possible
* https://github.com/riophae/vue-treeselect/pull/310
*/
scrollPositionOnCenter: {
type: Boolean,
default: false
}
},

data() {
Expand Down Expand Up @@ -1453,6 +1462,10 @@ export default {
this.$nextTick(this.restoreMenuScrollPosition)
if (!this.options && !this.async) this.loadRootOptions()
this.toggleClickOutsideEvent(true)
if (this.scrollPositionOnCenter) {
this.expandParentNodes() // must before scrollMenuOnCenter
this.$nextTick(this.scrollMenuOnCenter);
}
this.$emit('open', this.getInstanceId())
},

Expand Down Expand Up @@ -1771,6 +1784,7 @@ export default {
this.buildForestState()

if (nextState) {
this.expandParentNodes(); // Expand parents in search mode then chidren is selected
this.$emit('select', node.raw, this.getInstanceId())
} else {
this.$emit('deselect', node.raw, this.getInstanceId())
Expand Down Expand Up @@ -1918,16 +1932,46 @@ export default {
},

saveMenuScrollPosition() {
if (this.scrollPositionOnCenter) {
// console.log('dont saveMenuScrollPosition...')
return;
}
const $menu = this.getMenu()
// istanbul ignore else
if ($menu) this.menu.lastScrollPosition = $menu.scrollTop
},

restoreMenuScrollPosition() {
if (this.scrollPositionOnCenter) {
// console.log('dont restoreMenuScrollPosition...')
return;
}
const $menu = this.getMenu()
// istanbul ignore else
if ($menu) $menu.scrollTop = this.menu.lastScrollPosition
},

scrollMenuOnCenter() {
const $option = document.querySelector(".vue-treeselect__option--selected");
// console.log('$option',$option);
const $menu = this.getMenu();
// console.log('$menu',$menu);

if ($option && $menu) {
const position = Math.max($option.offsetTop - (($menu.offsetHeight - $option.offsetHeight) / 2), 0);
$menu.scrollTop = position;
}
},

expandParentNodes() {
// console.log('this.forest.selectedNodeIds',this.forest.selectedNodeIds)
for (const id of this.forest.selectedNodeIds) {
for (const ancestor of this.forest.nodeMap[id].ancestors) {
// console.log('ancestor',ancestor);
ancestor.isExpanded = true;
}
}
}
},

created() {
Expand Down

0 comments on commit 7f168f4

Please sign in to comment.