thinkphp关联查询实现动态加载easyUI组合树ComboTree

easyUI官方组合树ComboTree代码:

  1.                             

 

 

    1. !DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Basic ComboTree - jQuery EasyUI Demo</title>
    6. <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css">
    7. <link rel="stylesheet" type="text/css" href="../../themes/icon.css">
    8. <link rel="stylesheet" type="text/css" href="../demo.css">
    9. <script type="text/javascript" src="../../jquery.min.js"></script>
    10. <script type="text/javascript" src="../../jquery.easyui.min.js"></script>
    11. </head>
    12. <body>
    13. <h2>Basic ComboTree</h2>
    14. <p>Click the right arrow button to show the tree panel.</p>
    15. <div style="margin:20px 0"></div>
    16. <input class="easyui-combotree" data-options="url:'tree_data1.json',method:'get',required:true" style="width:200px;">
    17.  
    18. </body>
    19. </html>

tree_data1.json:

[{
	"id":1,
	"text":"My Documents",
	"children":[{
		"id":11,
		"text":"Photos",
		"state":"closed",
		"children":[{
			"id":111,
			"text":"Friend"
		},{
			"id":112,
			"text":"Wife"
		},{
			"id":113,
			"text":"Company"
		}]
	},{
		"id":12,
		"text":"Program Files",
		"children":[{
			"id":121,
			"text":"Intel"
		},{
			"id":122,
			"text":"Java",
			"attributes":{
				"p1":"Custom Attribute1",
				"p2":"Custom Attribute2"
			}
		},{
			"id":123,
			"text":"Microsoft Office"
		},{
			"id":124,
			"text":"Games",
			"checked":true
		}]
	},{
		"id":13,
		"text":"index.html"
	},{
		"id":14,
		"text":"about.html"
	},{
		"id":15,
		"text":"welcome.html"
	}]
}]

思路:想要实现动态加载,必须从数据库返回具有父子层级关系的数据格式,(涉及到三级菜单以上的话要使用多级关联查询,)才能正确显示。

 

数据表Xzq:id,name,parentId  (菜单为自关联)

      1,中国,0  

      2,安徽,1

      3,浙江,1

Thinkphp代码实现:

模型层:

  <?php
namespace Home\Model;
use Think\Model\RelationModel;
class XzqModel extends RelationModel{
    protected $_link = array(
        'children' =>array(
            'mapping_type'  => self::HAS_MANY,         //关联关系
            'class_name'    => 'Category',          //要关联的模型类名
            'parent_key'   => 'parentId',          //自引用关联的关联字段
            'mapping_fields'=>'id,name as text,parentId',   //关联要查询的字段
            'relation_deep'=>true,               //支持多级查询设置
        )
    );

 控制器:

   $result=$category->field('id,name as text')->relation(true)
       ->where('companyUserId='.session('id').' and parentId=0')->select();

这样得到的关联数组即是一个符合easyUI组合树ComboTree要求的数据格式。

posted @ 2016-01-01 16:41  AHAU航哥  阅读(989)  评论(0编辑  收藏  举报