wxml
<view class='warp'>
<view class='list'>
<view class='style {{item.id==num?"active":""}}' catchtap='clickList' wx:for="{{message}}" wx:key="" id="{{index}}">{{item.name}}</view>
</view>
<view class='list2'>
<view class='content' wx:for="{{content}}" wx:key="index">{{item.name}}</view>
</view>
</view>
js
// pages/home/home.js
Page({
/**
* 页面的初始数据
*/
data: {
num:'',
content:'请选择',
message:''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var url=getApp().globalData.url
var url=url+"api/getall";
var _this=this;
wx.request({
url: url,
success:function(res){
console.log(res)
var str=res.data.data;
_this.setData({
message:str
})
}
})
},
clickList:function(e){
console.log(e)
let num = e.target.id
console.log(num)
let content = this.data.message[num].son
console.log(content)
this.setData({
num:num,
content:content
})
console.log(this)
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})
模型层递归处理
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Type extends Model
{
use HasFactory;
protected $table = 'type';
public static function getAll()
{
return self::all();
}
/**
* 递归处理
* @param $data
* @param int $pid
* @return array
*/
public static function getTree($data,$pid=0)
{
$arr=[];
foreach ($data as $v){
if ($v['pid']==$pid){
$arr[]=$v;
}
}
foreach ($arr as $k=>$item){
$arr[$k]['son']=self::getTree($data,$item['id']);
}
return $arr;
}
}