<template>
<gracePage :customHeader="false">
<view slot="gBody" class="grace-body">
<view class="grace-margin-top">
<text class="grace-title">属性切换</text>
</view>
<view>
<graceSegmentedControl :items="types" :current="current" @change="change"></graceSegmentedControl>
</view>
<view class="grace-margin-top">
<graceTree :trees="trees" :allCanCheck="false" v-if="trees != null" :type="types[current]"
:checkedId="checkedId" :checkedIds="checkedIds" @taped="taped"></graceTree>
</view>
</view>
</gracePage>
</template>
<script>
import gracePage from "../../graceUI/components/gracePage.vue";
import graceTree from "../../graceUI/components/graceTree.vue";
import graceSegmentedControl from '../../graceUI/components/graceSegmentedControl.vue';
export default{
data() {
return {
trees : null,
types : ['text','radio','checkbox'],
current : 1,
// 单选默认值
checkedId : 21,
// 多选默认值
checkedIds : [11,21]
}
},
onShow:function(){
setTimeout(()=>{
var trees = [
{
id : 1,
label : '台式机',
children : [
{id : 11, label : '华硕'},
{id : 12, label : '联想', children:[
{id : 121, label : '联想 01', children:[
{id : 1211, label : '联想 0101'},
{id : 1212, label : '联想 0102'}
]},
{id : 122, label : '联想 02'}
]}
]
},
{
id : 2,
label : '笔记本',
children : [
{id : 21, label : '苹果'},
{id : 22, label : '小米'}
]
}
]
this.trees = trees;
},1000);
},
methods:{
change : function(e){
this.current = e;
},
// 组件点击事件,真实场景会规划好类型,只需实现一种模式即可
taped : function(e){
//console.log(e);
switch(this.current){
// 0 文本
case 0 :
uni.showToast({title:"请观察控制台", icon:"none"});
var node = this.getNode(e);
console.log('被点击元素标签 : ' + node.allLabels.join(' > '));
console.log('被点击元素对象 : ' + JSON.stringify(node.node));
break;
// 1 单选
case 1 :
// node 格式见手册或者控制台打印一下console.log(node, this.checkedId );
// 获得 node 即可获得单选选项的全部数据
var node = this.getNode(e);
var nodeId = node.node.id;
this.checkedId = nodeId;
break;
// 2 多选
case 2 :
// node 格式见手册或者控制台打印一下console.log(node, this.checkedId );
// 获得 node 即可获得单选选项的全部数据
var node = this.getNode(e);
var nodeId = node.node.id;
this.pushIds(nodeId);
// 多选时系统最终记录了选项的 id [ 数组形式 ]
// 如果想获取节点数据可以使用 getNodesByIds 函数
var checkedNodes = this.getNodesByIds(this.trees);
console.log(checkedNodes);
break;
}
},
// 利用 ids 获取节点信息
getNodesByIds : function(nodes, res){
if(!res){res = [];}
for(let i = 0; i < nodes.length; i++){
if(this.isInArray(nodes[i].id, this.checkedIds)){
res.push(nodes[i]);
}
if(nodes[i].children){this.getNodesByIds(nodes[i].children, res);}
}
return res;
},
isInArray : function (val, arr){
for(let i = 0; i < arr.length; i++){
if(val == arr[i]){return true;}
}
return false;
},
// 记录或者反选
pushIds : function(checkedId){
for(let i = 0; i < this.checkedIds.length; i++){
if(this.checkedIds[i] == checkedId){
this.checkedIds.splice(i,1);
return true;
}
}
this.checkedIds.push(checkedId);
},
// 点击文本时获取节点信息
getNode : function(e){
var len = e.length;
var nodes = JSON.stringify(this.trees);
nodes = JSON.parse(nodes);
var reLabels = [];
var tmpVar = null;
var thisNode = null;
for(let i = 0; i < len; i++){
if(i == 0){
reLabels.push(nodes[e[i]].label);
tmpVar = JSON.stringify(nodes[e[i]].children);
tmpVar = JSON.parse(tmpVar);
thisNode = nodes[e[0]];
}else{
reLabels.push(tmpVar[e[i]].label);
if(i+1 >= len){thisNode = tmpVar[e[i]];}
tmpVar = tmpVar[e[i]].children;
}
}
return {allLabels : reLabels, node : thisNode};
},
},
components: {
gracePage, graceTree,graceSegmentedControl
}
}
</script>
<style>
</style>
<template>
<gracePage :customHeader="false">
<view slot="gBody" class="grace-body">
<view class="grace-margin-top">
<text class="grace-title">属性切换</text>
</view>
<view>
<graceSegmentedControl :items="types" :current="current" @change="change"></graceSegmentedControl>
</view>
<view class="grace-margin-top">
<graceTree :trees="trees" :allCanCheck="false" v-if="trees != null" :type="types[current]"
:checkedId="checkedId" :checkedIds="checkedIds" @taped="taped"></graceTree>
</view>
</view>
</gracePage>
</template>
<script>
import gracePage from "../../graceUI/weexComponents/gracePage.nvue";
import graceTree from "../../graceUI/weexComponents/graceTree.nvue";
import graceSegmentedControl from '../../graceUI/weexComponents/graceSegmentedControl.nvue';
export default{
data() {
return {
trees : null,
types : ['text','radio','checkbox'],
current : 1,
// 单选默认值
checkedId : 21,
// 多选默认值
checkedIds : [11,21]
}
},
onShow:function(){
setTimeout(()=>{
var trees = [
{
id : 1,
label : '台式机',
children : [
{id : 11, label : '华硕'},
{id : 12, label : '联想', children:[
{id : 121, label : '联想 01', children:[
{id : 1211, label : '联想 0101'},
{id : 1212, label : '联想 0102'}
]},
{id : 122, label : '联想 02'}
]}
]
},
{
id : 2,
label : '笔记本',
children : [
{id : 21, label : '苹果'},
{id : 22, label : '小米'}
]
}
]
this.trees = trees;
},1000);
},
methods:{
change : function(e){
this.current = e;
},
// 组件点击事件,真实场景会规划好类型,只需实现一种模式即可
taped : function(e){
//console.log(e);
switch(this.current){
// 0 文本
case 0 :
uni.showToast({title:"请观察控制台", icon:"none"});
var node = this.getNode(e);
console.log('被点击元素标签 : ' + node.allLabels.join(' > '));
console.log('被点击元素对象 : ' + JSON.stringify(node.node));
break;
// 1 单选
case 1 :
// node 格式见手册或者控制台打印一下console.log(node, this.checkedId );
// 获得 node 即可获得单选选项的全部数据
var node = this.getNode(e);
var nodeId = node.node.id;
this.checkedId = nodeId;
break;
// 2 多选
case 2 :
// node 格式见手册或者控制台打印一下console.log(node, this.checkedId );
// 获得 node 即可获得单选选项的全部数据
var node = this.getNode(e);
var nodeId = node.node.id;
this.pushIds(nodeId);
// 多选时系统最终记录了选项的 id [ 数组形式 ]
// 如果想获取节点数据可以使用 getNodesByIds 函数
var checkedNodes = this.getNodesByIds(this.trees);
console.log(checkedNodes);
break;
}
},
// 利用 ids 获取节点信息
getNodesByIds : function(nodes, res){
if(!res){res = [];}
for(let i = 0; i < nodes.length; i++){
if(this.isInArray(nodes[i].id, this.checkedIds)){
res.push(nodes[i]);
}
if(nodes[i].children){this.getNodesByIds(nodes[i].children, res);}
}
return res;
},
isInArray : function (val, arr){
for(let i = 0; i < arr.length; i++){
if(val == arr[i]){return true;}
}
return false;
},
// 记录或者反选
pushIds : function(checkedId){
for(let i = 0; i < this.checkedIds.length; i++){
if(this.checkedIds[i] == checkedId){
this.checkedIds.splice(i,1);
return true;
}
}
this.checkedIds.push(checkedId);
},
// 点击文本时获取节点信息
getNode : function(e){
var len = e.length;
var nodes = JSON.stringify(this.trees);
nodes = JSON.parse(nodes);
var reLabels = [];
var tmpVar = null;
var thisNode = null;
for(let i = 0; i < len; i++){
if(i == 0){
reLabels.push(nodes[e[i]].label);
tmpVar = JSON.stringify(nodes[e[i]].children);
tmpVar = JSON.parse(tmpVar);
thisNode = nodes[e[0]];
}else{
reLabels.push(tmpVar[e[i]].label);
if(i+1 >= len){thisNode = tmpVar[e[i]];}
tmpVar = tmpVar[e[i]].children;
}
}
return {allLabels : reLabels, node : thisNode};
},
},
components: {
gracePage, graceTree,graceSegmentedControl
}
}
</script>
<style>
</style>
.wxml
<gracePage customHeader="{{false}}">
<view slot="gBody" class="grace-body">
<view class="grace-margin-top">
<text class="grace-title">属性切换</text>
</view>
<view>
<graceSegmentedControl items="{{types}}" current="{{current}}" bind:change="change"></graceSegmentedControl>
</view>
<view class="grace-margin-top">
<graceTree trees="{{trees}}" allCanCheck="{{false}}" wx:if="{{trees != null}}" type="{{types[current]}}" isIcon="{{false}}"
checkedId="{{checkedId}}" checkedIds="{{checkedIds}}" bind:taped="taped"></graceTree>
</view>
</view>
</gracePage>.js
Page({
data:{
trees : null,
types : ['text','radio','checkbox'],
current : 1,
// 单选默认值
checkedId : 21,
// 多选默认值
checkedIds : [11,21]
},
onShow:function(){
setTimeout(()=>{
var trees = [
{
id : 1,
label : '台式机',
children : [
{id : 11, label : '华硕'},
{id : 12, label : '联想', children:[
{id : 121, label : '联想 01', children:[
{id : 1211, label : '联想 0101', checked : true},
{id : 1212, label : '联想 0102'}
]},
{id : 122, label : '联想 02'}
]}
]
},
{
id : 2,
label : '笔记本',
children : [
{id : 21, label : '苹果'},
{id : 22, label : '小米'}
]
}
]
this.setData({trees : trees});
},1000);
},
change : function(e){
this.setData({current : e.detail});
},
// 组件点击事件,真实场景会规划好类型,只需实现一种模式即可
taped : function(e){
e = e.detail;
switch(this.data.current){
// 0 文本
case 0 :
wx.showToast({title:"请观察控制台", icon:"none"});
var node = this.getNode(e);
console.log('被点击元素标签 : ' + node.allLabels.join(' > '));
console.log('被点击元素对象 : ' + JSON.stringify(node.node));
break;
// 1 单选
case 1 :
// node 格式见手册或者控制台打印一下console.log(node, this.checkedId );
// 获得 node 即可获得单选选项的全部数据
var node = this.getNode(e);
var nodeId = node.node.id;
this.setData({checkedId : nodeId});
break;
// 2 多选
case 2 :
// node 格式见手册或者控制台打印一下console.log(node, this.checkedId );
// 获得 node 即可获得单选选项的全部数据
var node = this.getNode(e);
var nodeId = node.node.id;
this.pushIds(nodeId);
// 多选时系统最终记录了选项的 id [ 数组形式 ]
// 如果想获取节点数据可以使用 getNodesByIds 函数
var checkedNodes = this.getNodesByIds(this.data.trees);
console.log(checkedNodes);
break;
}
},
// 利用 ids 获取节点信息
getNodesByIds : function(nodes, res){
if(!res){res = [];}
for(let i = 0; i < nodes.length; i++){
if(this.isInArray(nodes[i].id, this.data.checkedIds)){
res.push(nodes[i]);
}
if(nodes[i].children){this.getNodesByIds(nodes[i].children, res);}
}
return res;
},
isInArray : function (val, arr){
for(let i = 0; i < arr.length; i++){
if(val == arr[i]){return true;}
}
return false;
},
// 记录或者反选
pushIds : function(checkedId){
for(let i = 0; i < this.data.checkedIds.length; i++){
if(this.data.checkedIds[i] == checkedId){
this.data.checkedIds.splice(i,1);
this.setData({checkedIds:this.data.checkedIds});
return true;
}
}
this.data.checkedIds.push(checkedId);
this.setData({checkedIds:this.data.checkedIds});
},
// 点击文本时获取节点信息
getNode : function(e){
var len = e.length;
var nodes = JSON.stringify(this.data.trees);
nodes = JSON.parse(nodes);
var reLabels = [];
var tmpVar = null;
var thisNode = null;
for(let i = 0; i < len; i++){
if(i == 0){
reLabels.push(nodes[e[i]].label);
tmpVar = JSON.stringify(nodes[e[i]].children);
tmpVar = JSON.parse(tmpVar);
thisNode = nodes[e[0]];
}else{
reLabels.push(tmpVar[e[i]].label);
if(i+1 >= len){thisNode = tmpVar[e[i]];}
tmpVar = tmpVar[e[i]].children;
}
}
return {allLabels : reLabels, node : thisNode};
}
}).json
{
"usingComponents": {
"gracePage" : "../../graceUI/components/gracePage",
"graceTree" : "../../graceUI/components/graceTree",
"graceSegmentedControl" : "../../graceUI/components/graceSegmentedControl"
}
}