| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- const tranverse = (e,itemListKey) => {
-
- for (let i in e) {
- if (e[i][itemListKey]) {
- e[i].open = false;
- tranverse(e[i][itemListKey],itemListKey)
- }
- e[i].selected = false;
- }
- }
- Component({
- properties: {
- treeList: Object,
- depth: {
- type: Number,
- value: 0
- },
- itemListKey: { //子list key名
- type: String,
- value: ""
- },
- parentIdKey: { //父级的key名
- type: String,
- value: ""
- },
- idKey: { // id 的key名
- type: String,
- value: ""
- },
- nameKey: { //显示的名字的key
- type: String,
- value: ""
- },
- ischekboxFlag: { //是否显示cheakbox
- type: Boolean,
- value: false
- },
- isNameImgFlag: { //是否显示名字前图片
- type: Boolean,
- value: true
- },
- },
- data: {},
- ready() {
- // const {treeList}=this.properties;
- const { treeList } = this.data;
- const { itemListKey } = this.data;
- const { parentIdKey } = this.data;
- for (let i in treeList) {
- if (treeList[i][itemListKey]) {
- if(treeList[i][parentIdKey]){
- treeList[i].open = false;
- }else{
- treeList[i].open = true;
- }
- tranverse(treeList[i][itemListKey],itemListKey);
- }
- treeList[i].selected = false;
- }
-
- this.setData({
- tree: treeList
- })
- },
- methods: {
- //设置控件信息
- setDataFromPage(data) {
- const { itemListKey } = this.data;
- const { parentIdKey } = this.data;
- let treeList = data
- for (let i in treeList) {
- if (treeList[i][itemListKey]) {
- if(treeList[i][parentIdKey]){
- treeList[i].open = false;
- }else{
- treeList[i].open = true;
- }
- tranverse(treeList[i][itemListKey],itemListKey);
- }
- treeList[i].selected = false;
- }
-
- this.setData({
- tree: treeList
- })
-
- },
- //修改折叠状态
- changeOpen(tree, id,itemListKey,idKey) {
- for (let i = 0; i < tree.length; i += 1) {
- if (tree[i][idKey] === id) {
- tree[i].open = !tree[i].open;
- }
- if (tree[i].hasOwnProperty(itemListKey) && tree[i][itemListKey].length !== 0) {
- this.changeOpen(tree[i][itemListKey], id, itemListKey);
- }
- }
- return;
- },
- onchange(e) {
- const { treeList } = this.data;
- const { itemListKey } = this.data;
- const { idKey } = this.data;
- const { id } = e.currentTarget.dataset;
- this.changeOpen(treeList, id, itemListKey,idKey);
- this.triggerEvent('change', id, { bubbles: true, composed: true });
- this.setData({
- tree: treeList
- })
- },
- change(e) {
- const id = e.detail;
- const { treeList } = this.data;
- const { itemListKey } = this.data;
- const { idKey } = this.data;
- this.changeOpen(treeList, id,itemListKey,idKey);
- this.setData({
- tree: treeList
- })
- },
- // 点击名字
- clickName(e){
- const { idKey } = this.data;
- const { nameKey } = this.data;
-
- this.triggerEvent('clickName', {id:e.currentTarget.dataset.item[idKey],name:e.currentTarget.dataset.item[nameKey]} );
-
- },
- //点击控件里的控件 回调
- subclickName(e){
- this.triggerEvent('clickName', {id:e.detail.id,name:e.detail.name} );
- },
- click(e) {
- const t = this.selectAllComponents('#treeSelect');
- t.forEach(item => {
- item.click(e);
- })
- const { idKey } = this.data;
- let id = '';
- if (e.detail) {
- id = e.detail[idKey];
- }
- const { treeList } = this.data;
- this.setStatus(treeList, id,idKey);
- this.setData({
- tree: treeList
- })
- },
- handleClick(e) {
- const t = this.selectAllComponents('#treeSelect');
- t.forEach(item => {
- item.click(e);
- })
- // const {id}=e.currentTarget.dataset;
- const { id, value } = e.currentTarget.dataset;
- const { treeList } = this.data;
- const { idKey } = this.data;
- // const value = this.getData(treeList, id)
- this.setStatus(treeList, id,idKey);
- this.triggerEvent('onclick', { id, value, treeList }, { composed: true, bubbles: true });
- this.setData({
- tree: treeList
- })
- },
- //切换选中状态
- setStatus(tree, id,idKey) {
- for (let i = 0; i < tree.length; i += 1) {
- if (tree[i][idKey] == id) {
- tree[i].selected = true;
- } else {
- tree[i].selected = false;
- }
- if (tree[i][itemListKey]) {
- this.setStatus(tree[i][itemListKey], id,idKey);
- }
- }
- return;
- },
- //获取选中项信息
- getData(tree, id,idKey) {
- for (let i = 0; i < tree.length; i += 1) {
- if (tree[i][idKey] === id) {
- return tree[i].name;
- }
- if (tree[i][itemListKey]) {
- this.getData(tree[i][itemListKey], id,idKey);
- }
- }
- return '';
- },
-
- }
- })
|