update Cascader

update Cascader
This commit is contained in:
梁灏 2016-11-15 23:23:16 +08:00
parent c463ab8757
commit bd4e3b9b0a
4 changed files with 85 additions and 29 deletions

View file

@ -12,11 +12,12 @@
<Dropdown v-show="visible" transition="slide-up"> <Dropdown v-show="visible" transition="slide-up">
<div> <div>
<Caspanel <Caspanel
v-ref:caspanel
:prefix-cls="prefixCls" :prefix-cls="prefixCls"
:data.sync="data" :data.sync="data"
:disabled="disabled" :disabled="disabled"
:trigger="trigger" :change-on-select="changeOnSelect"
@on-update-result="updateResult"></Caspanel> :trigger="trigger"></Caspanel>
</div> </div>
</Dropdown> </Dropdown>
</div> </div>
@ -118,12 +119,46 @@
this.visible = true; this.visible = true;
}, },
updateResult (result) { updateResult (result) {
console.log(JSON.stringify(result)) this.tmpSelected = result;
this.selected = result; },
updateSelected () {
this.$broadcast('on-find-selected', this.value);
} }
}, },
ready () { ready () {
this.updateSelected();
},
events: {
// lastValue: is click the final val
// fromInit: is this emit from update value
'on-result-change' (lastValue, changeOnSelect, fromInit) {
if (lastValue || changeOnSelect) {
const oldVal = JSON.stringify(this.value);
this.selected = this.tmpSelected;
let newVal = [];
this.selected.forEach((item) => {
newVal.push(item.value);
});
this.value = newVal;
if (JSON.stringify(this.value) !== oldVal) {
this.$emit('on-change', this.value);
}
}
if (lastValue && !fromInit) {
this.handleClose();
}
}
},
watch: {
visible (val) {
if (val) {
if (this.value.length) {
this.updateSelected();
}
}
}
} }
} }
</script> </script>

View file

@ -13,7 +13,8 @@
return [ return [
`${this.prefixCls}-menu-item`, `${this.prefixCls}-menu-item`,
{ {
[`${this.prefixCls}-menu-item-active`]: this.tmpItem.value === this.data.value [`${this.prefixCls}-menu-item-active`]: this.tmpItem.value === this.data.value,
[`${this.prefixCls}-menu-item-disabled`]: this.data.disabled
} }
] ]
} }

View file

@ -7,7 +7,7 @@
:tmp-item="tmpItem" :tmp-item="tmpItem"
@click.stop="handleClickItem(item)" @click.stop="handleClickItem(item)"
@mouseenter.stop="handleHoverItem(item)"></Casitem> @mouseenter.stop="handleHoverItem(item)"></Casitem>
</ul><Caspanel v-if="sublist && sublist.length" :prefix-cls="prefixCls" :data.sync="sublist" :disabled="disabled" :trigger="trigger" @on-update-result="updateResult"></Caspanel> </ul><Caspanel v-if="sublist && sublist.length" :prefix-cls="prefixCls" :data.sync="sublist" :disabled="disabled" :trigger="trigger" :change-on-select="changeOnSelect"></Caspanel>
</template> </template>
<script> <script>
import Casitem from './casitem.vue'; import Casitem from './casitem.vue';
@ -31,11 +31,7 @@
}, },
disabled: Boolean, disabled: Boolean,
changeOnSelect: Boolean, changeOnSelect: Boolean,
trigger: { trigger: String,
validator (value) {
return oneOf(value, ['click', 'hover']);
}
},
prefixCls: String prefixCls: String
}, },
data () { data () {
@ -46,33 +42,32 @@
}, },
methods: { methods: {
handleClickItem (item) { handleClickItem (item) {
if (this.trigger !== 'click') return; if (this.trigger !== 'click' && item.children) return;
this.handleTriggerItem(item); this.handleTriggerItem(item);
}, },
handleHoverItem (item) { handleHoverItem (item) {
if (this.trigger !== 'hover') return; if (this.trigger !== 'hover' || !item.children) return;
this.handleTriggerItem(item); this.handleTriggerItem(item);
}, },
handleTriggerItem (item) { handleTriggerItem (item, fromInit = false) {
if (item.disabled) return; if (item.disabled) return;
// return value back recursion
const backItem = this.getBaseItem(item);
this.tmpItem = backItem;
this.emitUpdate([backItem]);
if (item.children && item.children.length){ if (item.children && item.children.length){
this.sublist = item.children; this.sublist = item.children;
// todo this.$dispatch('on-result-change', false, this.changeOnSelect, fromInit);
} else { } else {
this.sublist = []; this.sublist = [];
// todo this.$dispatch('on-result-change', true, this.changeOnSelect, fromInit);
} }
// return value back
const backItem = this.getBaseItem(item);
this.tmpItem = backItem;
this.$emit('on-update-result', [backItem]);
}, },
updateResult (item) { updateResult (item) {
this.result = [this.tmpItem].concat(item); this.result = [this.tmpItem].concat(item);
this.$emit('on-update-result', this.result); this.emitUpdate(this.result);
}, },
getBaseItem (item) { getBaseItem (item) {
let backItem = Object.assign({}, item); let backItem = Object.assign({}, item);
@ -81,6 +76,13 @@
} }
return backItem; return backItem;
},
emitUpdate (result) {
if (this.$parent.$options.name === 'Caspanel') {
this.$parent.updateResult(result);
} else {
this.$parent.$parent.updateResult(result);
}
} }
}, },
watch: { watch: {
@ -88,8 +90,22 @@
this.sublist = []; this.sublist = [];
} }
}, },
ready () { events: {
// todo 'on-find-selected' (val) {
let value = [...val];
for (let i = 0; i < value.length; i++) {
for (let j = 0; j < this.data.length; j++) {
if (value[i] === this.data[j].value) {
this.handleTriggerItem(this.data[j], true);
value.splice(0, 1);
this.$nextTick(() => {
this.$broadcast('on-find-selected', value);
});
return false;
}
}
}
}
} }
} }
</script> </script>

View file

@ -1,6 +1,6 @@
<template> <template>
<div style="margin: 50px;width:300px"> <div style="margin: 50px;width:300px">
<Cascader :data="data" :value="value"></Cascader> <Cascader :data="data" :value.sync="value" @on-change="change" trigger="click"></Cascader>
</div> </div>
</template> </template>
<script> <script>
@ -11,7 +11,8 @@
}, },
data () { data () {
return { return {
value: [], value: ['jiangsu', 'hhh', 'ddd'],
// value: [],
data: [{ data: [{
value: 'zhejiang', value: 'zhejiang',
label: 'Zhejiang', label: 'Zhejiang',
@ -25,6 +26,7 @@
children: [{ children: [{
value: 'nanjing', value: 'nanjing',
label: 'Nanjing', label: 'Nanjing',
// disabled: true,
children: [{ children: [{
value: 'zhonghuamen', value: 'zhonghuamen',
label: 'Zhong Hua Men', label: 'Zhong Hua Men',
@ -48,7 +50,9 @@
}, },
methods: { methods: {
change (data) {
console.log(data)
}
} }
} }
</script> </script>