前端调用三级联动插件,提供对应数组

最近做一个足球赛事的功能,查询赛事需要更具 洲 - 国 - 赛事几级联动查询所以前端给的数据格式是:

[{
        value: '3', #洲id
        label: '亚洲联赛',  #洲名称
        children: [{
                value: '45',   #国id
                label: '中国', #国昵称
                children: [{
                        value: '53438',   #赛事id
                        label: '中女超杯' #赛事昵称
                }, {
                        value: '534389',
                        label: '中女超'
                }]
        }, {
                value: '47',
                label: '南韩',
                children: [
                  {
                        value: '53333',
                        label: '韩K2联'

                  }, {
                        value: '54085',
                        label: '韩K4联'
                }]
        }],
          value: '1',  #洲id
          label: '欧洲联赛',  #洲名称
          children: [{
                  value: '45',   #国id
                  label: '中国', #国昵称
                  children: [{
                          value: '53438',   #赛事id
                          label: '中女超杯' #赛事昵称
                  }, {
                          value: '534389',
                          label: '中女超'
                  }]
          }, {
                  value: '47',
                  label: '南韩',
                  children: [
                        {
                          value: '53333',
                          label: '韩K2联'

                        }, {
                          value: '54085',
                          label: '韩K4联'
                  }]
          }]
}]

最后大费周折还请教别人(之前搞过很久没搞忘记了) 才搞定,上实现代码:


            $result = [
                ['value' =>0,'label' =>'国际联赛'],
                ['value' =>1,'label' =>'欧洲联赛'],
                ['value' =>2,'label' =>'美洲联赛'],
                ['value' =>3,'label' =>'亚洲联赛'],
                ['value' =>4,'label' =>'大洋洲联赛'],
                ['value' =>5,'label' =>'非洲联赛'],
            ];


            foreach ($result as $k => &$v) {
                    $v['children'] = array_map('get_object_vars',DB::select("select countryId as value,countryCn as label from `league` where (`areaId` = {$v['value']}) group by `countryCn`"));
            }

            foreach ($result as $k => &$v) {
                foreach ($v['children'] as $k1 => &$v1) {
                    $v1['children'] = array_map('get_object_vars',DB::select("select id as value,nameChsShort as label from `league` where (`areaId` = {$v['value']}) and (`countryId` = {$v1['value']})"));

            }
        }
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 3

应该先读取你需要的数据,然后根据条件做递归不就行了,无限级联动都可以

1年前 评论
Pinkerman (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!