mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-05-20 21:49:14 +08:00
[bugfix] TreeSelect: fix error when type of activeId is string or number
fix #1912
This commit is contained in:
parent
55cf16ab6e
commit
3691cc409c
@ -3,27 +3,25 @@ import config from './config';
|
|||||||
|
|
||||||
Page({
|
Page({
|
||||||
data: {
|
data: {
|
||||||
items: [
|
items: [{
|
||||||
{
|
text: '所有城市',
|
||||||
// 导航名称
|
children: [...config.pro1, ...config.pro2]
|
||||||
text: '所有城市',
|
}, {
|
||||||
// 该导航下所有的可选项
|
text: config.pro1Name,
|
||||||
children: [...config.pro1, ...config.pro2]
|
children: config.pro1
|
||||||
}, {
|
}, {
|
||||||
// 导航名称
|
text: config.pro2Name,
|
||||||
text: config.pro1Name,
|
children: config.pro2
|
||||||
// 该导航下所有的可选项
|
}, {
|
||||||
children: config.pro1
|
text: config.pro3Name,
|
||||||
}, {
|
disabled: true,
|
||||||
text: config.pro2Name,
|
children: config.pro3
|
||||||
children: config.pro2
|
}
|
||||||
}, {
|
|
||||||
text: config.pro3Name,
|
|
||||||
disabled: true,
|
|
||||||
children: config.pro3
|
|
||||||
}
|
|
||||||
],
|
],
|
||||||
mainActiveIndex: 0
|
mainActiveIndex: 0,
|
||||||
|
activeId: 0,
|
||||||
|
mainActiveIndexMulti: 0,
|
||||||
|
activeIdMulti: []
|
||||||
},
|
},
|
||||||
|
|
||||||
onClickNav({ detail }) {
|
onClickNav({ detail }) {
|
||||||
@ -33,23 +31,26 @@ Page({
|
|||||||
},
|
},
|
||||||
|
|
||||||
onClickItem({ detail }) {
|
onClickItem({ detail }) {
|
||||||
// 多选
|
const activeId = this.data.activeId === detail.id ? null : detail.id;
|
||||||
if (!this.data.activeId) this.data.activeId = [];
|
|
||||||
|
|
||||||
const idx = this.data.activeId.indexOf(detail.id);
|
this.setData({ activeId });
|
||||||
|
},
|
||||||
|
|
||||||
|
onClickNavMulti({ detail }) {
|
||||||
|
this.setData({
|
||||||
|
mainActiveIndexMulti: detail.index || 0
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
onClickItemMulti({ detail }) {
|
||||||
|
const { activeIdMulti } = this.data;
|
||||||
|
const idx = activeIdMulti.indexOf(detail.id);
|
||||||
if (idx > -1) {
|
if (idx > -1) {
|
||||||
this.data.activeId.splice(idx, 1);
|
activeIdMulti.splice(idx, 1);
|
||||||
} else {
|
} else {
|
||||||
this.data.activeId.push(detail.id);
|
activeIdMulti.push(detail.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
this.setData({ activeIdMulti });
|
||||||
// 单选
|
|
||||||
this.data.activeId = this.data.activeId === detail.id ? null : detail.id;
|
|
||||||
*/
|
|
||||||
|
|
||||||
this.setData({
|
|
||||||
activeId: this.data.activeId
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -8,3 +8,14 @@
|
|||||||
content-item-class="content-item-class"
|
content-item-class="content-item-class"
|
||||||
></van-tree-select>
|
></van-tree-select>
|
||||||
</demo-block>
|
</demo-block>
|
||||||
|
|
||||||
|
<demo-block title="多选">
|
||||||
|
<van-tree-select
|
||||||
|
items="{{ items }}"
|
||||||
|
main-active-index="{{ mainActiveIndexMulti }}"
|
||||||
|
active-id="{{ activeIdMulti }}"
|
||||||
|
bind:click-item="onClickItemMulti"
|
||||||
|
bind:click-nav="onClickNavMulti"
|
||||||
|
content-item-class="content-item-class"
|
||||||
|
></van-tree-select>
|
||||||
|
</demo-block>
|
||||||
|
@ -29,7 +29,8 @@
|
|||||||
```javascript
|
```javascript
|
||||||
Page({
|
Page({
|
||||||
data: {
|
data: {
|
||||||
// ...
|
mainActiveIndex: 0,
|
||||||
|
activeId: null
|
||||||
},
|
},
|
||||||
|
|
||||||
onClickNav({ detail = {} }) {
|
onClickNav({ detail = {} }) {
|
||||||
@ -39,35 +40,60 @@ Page({
|
|||||||
},
|
},
|
||||||
|
|
||||||
onClickItem({ detail = {} }) {
|
onClickItem({ detail = {} }) {
|
||||||
// 多选
|
const activeId = this.data.activeId === detail.id ? null : detail.id;
|
||||||
if (!this.data.activeId) this.data.activeId = [];
|
|
||||||
|
|
||||||
const idx = this.data.activeId.indexOf(detail.id);
|
this.setData({ activeId });
|
||||||
if (idx > -1) {
|
}
|
||||||
this.data.activeId.splice(idx, 1);
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### 多选
|
||||||
|
|
||||||
|
```html
|
||||||
|
<van-tree-select
|
||||||
|
items="{{ items }}"
|
||||||
|
main-active-index="{{ mainActiveIndex }}"
|
||||||
|
active-id="{{ activeId }}"
|
||||||
|
bind:click-nav="onClickNav"
|
||||||
|
bind:click-item="onClickItem"
|
||||||
|
/>
|
||||||
|
```
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
Page({
|
||||||
|
data: {
|
||||||
|
mainActiveIndex: 0,
|
||||||
|
activeId: []
|
||||||
|
},
|
||||||
|
|
||||||
|
onClickNav({ detail = {} }) {
|
||||||
|
this.setData({
|
||||||
|
mainActiveIndex: detail.index || 0
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
onClickItem({ detail = {} }) {
|
||||||
|
const { activeId } = this.data;
|
||||||
|
|
||||||
|
const index = activeId.indexOf(detail.id);
|
||||||
|
if (index > -1) {
|
||||||
|
activeId.splice(index, 1);
|
||||||
} else {
|
} else {
|
||||||
this.data.activeId.push(detail.id);
|
activeId.push(detail.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
this.setData({ activeId });
|
||||||
// 单选
|
|
||||||
this.data.activeId = this.data.activeId === detail.id ? null : detail.id;
|
|
||||||
*/
|
|
||||||
|
|
||||||
this.setData({
|
|
||||||
activeId: this.data.activeId
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
### Props
|
### Props
|
||||||
|
|
||||||
| 参数 | 说明 | 类型 | 默认值 | 必须 |
|
| 参数 | 说明 | 类型 | 默认值 |
|
||||||
|-----------|-----------|-----------|-------------|-------------|
|
|-----------|-----------|-----------|-------------|
|
||||||
| items | 分类显示所需的数据,具体数据结构可看 数据结构 | Array | [] | |
|
| items | 分类显示所需的数据,具体数据结构可看 数据结构 | `Array` | [] |
|
||||||
| main-active-index | 左侧导航高亮的索引 | Number | 0 | |
|
| main-active-index | 左侧导航高亮的索引 | `Number` | 0 | |
|
||||||
| active-id | 右侧选择项,高亮的数据id | String / Number / Array | 0 | |
|
| active-id | 右侧选择项,高亮的数据id | `String | Number | Array` | 0 |
|
||||||
|
|
||||||
### Events
|
### Events
|
||||||
|
|
||||||
|
@ -1,22 +1,5 @@
|
|||||||
<wxs src="../wxs/utils.wxs" module="utils" />
|
<wxs src="../wxs/utils.wxs" module="utils" />
|
||||||
|
<wxs src="./index.wxs" module="wxs" />
|
||||||
<wxs module="tsUtil">
|
|
||||||
// 判断子项是否被选中
|
|
||||||
function isArray (array) {
|
|
||||||
return array instanceof Array;
|
|
||||||
}
|
|
||||||
function isActive (activeList, itemId) {
|
|
||||||
if (typeof activeList === 'undefined' || activeList == null) return false;
|
|
||||||
if (isArray(activeList)) {
|
|
||||||
return activeList.indexOf(itemId) > -1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return activeList === itemId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports.isActive = isActive;
|
|
||||||
</wxs>
|
|
||||||
|
|
||||||
<view
|
<view
|
||||||
class="van-tree-select"
|
class="van-tree-select"
|
||||||
@ -41,13 +24,13 @@ module.exports.isActive = isActive;
|
|||||||
<view
|
<view
|
||||||
wx:for="{{ subItems }}"
|
wx:for="{{ subItems }}"
|
||||||
wx:key="id"
|
wx:key="id"
|
||||||
class="van-ellipsis van-hairline--bottom content-item-class {{ utils.bem('tree-select__item', { active: tsUtil.isActive(activeId, item.id), disabled: item.disabled }) }} {{ tsUtil.isActive(activeId, item.id) ? 'content-active-class' : '' }} {{ item.disabled ? 'content-disabled-class' : '' }}"
|
class="van-ellipsis van-hairline--bottom content-item-class {{ utils.bem('tree-select__item', { active: wxs.isActive(activeId, item.id), disabled: item.disabled }) }} {{ wxs.isActive(activeId, item.id) ? 'content-active-class' : '' }} {{ item.disabled ? 'content-disabled-class' : '' }}"
|
||||||
data-item="{{ item }}"
|
data-item="{{ item }}"
|
||||||
bind:tap="onSelectItem"
|
bind:tap="onSelectItem"
|
||||||
>
|
>
|
||||||
{{ item.text }}
|
{{ item.text }}
|
||||||
<van-icon
|
<van-icon
|
||||||
wx:if="{{ tsUtil.isActive(activeId, item.id) }}"
|
wx:if="{{ wxs.isActive(activeId, item.id) }}"
|
||||||
name="checked"
|
name="checked"
|
||||||
size="16px"
|
size="16px"
|
||||||
class="van-tree-select__selected"
|
class="van-tree-select__selected"
|
||||||
|
12
packages/tree-select/index.wxs
Normal file
12
packages/tree-select/index.wxs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
/* eslint-disable */
|
||||||
|
var array = require('../wxs/array.wxs');
|
||||||
|
|
||||||
|
function isActive (activeList, itemId) {
|
||||||
|
if (array.isArray(activeList)) {
|
||||||
|
return activeList.indexOf(itemId) > -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return activeList === itemId;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.isActive = isActive;
|
Loading…
x
Reference in New Issue
Block a user