首页 > Web开发 > 详细

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

时间:2016-01-01 18:42:46      阅读:706      评论:0      收藏:0      [点我收藏+]

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要求的数据格式。

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

原文:http://www.cnblogs.com/ahguSH/p/5093432.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!