$(function(){ /* 商品分类树形表格 */ $("#category").treegrid({ url:ThinkPHP['MODULE']+'/Category/getJsonTree', idField:'cid', treeField:'cname', border:false, fitColumns:true, striped:true, fit:true, columns:[[ {field:'sort',title:'排序',width:10}, {field:'cid',title:'ID',width:10}, {field:'cname',title:'商品名称',width:100}, {field:'dw',title:'商品单位',width:10}, { field:'isshow', title:'是否显示', width:100, formatter:function(value,row,index){ if(row.isshow ==1){ return '显示'; }else{ return '隐藏'; } }, }, ]], toolbar:[ { iconCls: 'icon-arrow_refresh', text:'刷新数据', handler: function(){ $("#category").treegrid('reload'); } },'-',{ iconCls: 'icon-add', text:'添加分类', handler: function(){alert('帮助按钮')} },'-',{ iconCls: 'icon-building_edit', text:'修改分类', handler: function(){alert('帮助按钮')} },'-',{ iconCls: 'icon-cross', text:'删除分类', handler: function(){alert('帮助按钮')} }, ], }); /* 商品分类树形表格结束 */ /* 格式化时间戳 */ function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' '); }
});
/* PHP后端 */
namespace Admin\Model;
use Think\Model;
class CategoryModel extends Model
{
/* 获得给定cid分类 */
public function getSelf($cid){
return $this->find($cid);
}
/* 获得所属父级分类 */
public function getParent($cid){
$parent = $this->getSelf($cid);
if($parent['pid']==0){ return null; }
return $this->where(array('cid'=>$parent['pid']))->find();
}
/* 获得所有子类 没有子分类返回空数组 */
public function getChild($cid){
return $this->where(array('pid'=>$cid))->select();
}
/* 判断是否有子分类 */
public function hasChild($cid){
return $this->where(array('pid'=>$cid))->count() ? true : false;
}
/* 递归获得树形表格数据 */
public function getTree($cid,&$arr){
//判断是否有子类 没有子类返回false
if( !$this->hasChild($cid) ){ return false; }
//得到子分类数据 是一个二维数组
$childs = $this->getChild($cid);
foreach($childs as $v){
if($this->hasChild($v['cid'])){ //再次判断是否有子分类
$this->getTree($v['cid'],$v['children']);
}
$arr[] = $v;
}
}
}