设为首页 收藏本站
查看: 342|回复: 0

[经验分享] php生成extjs下拉树json数据格式

[复制链接]

尚未签到

发表于 2017-4-10 09:49:32 | 显示全部楼层 |阅读模式
菜单项curd以及extjs下拉树json数据构造
<?php
// +----------------------------------------------------------------------
// | thinkphp+extjs +jquery CMS管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://blog.aigouw.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 殷志朋 <601046124@qq.com> 7391390
// +----------------------------------------------------------------------
// | Q&A: 群 7391390
// +-----
class MenuItemAction extends Action {
/**
* 取出所有菜单项
*/
function ListAll() {
$keyword = $_POST['searchstring'];
$limit = $_POST['limit'];
$start = $_POST['start'];
$limits = $start . "," . $limit;
if (!empty($keyword)) {
$where['name'] = array('like', '%' . $keyword . '%');
$_SESSION['keyword'] = $where;
} else {
if (empty($keyword)) {
unset($_SESSION['keyword']);
} else
if (!empty($_SESSION['keyword'])) {
$where = $_SESSION['keyword'];
}
}
$menuitem = new MenuItemModel();
$keyword = $_POST['keyword'];
$ftype = $_POST['ftype'];
if (!empty($keyword) && !empty($ftype)) {
$where[$ftype] = array('like', '%' . $keyword . '%');
$_SESSION['keyword'] = $where;
} else {
if (empty($keyword) && !empty($ftype)) {
unset($_SESSION['keyword']);
} else
if (!empty($_SESSION['keyword'])) {
$where = $_SESSION['keyword'];
}
}
if (!empty($_REQUEST['id'])) {
$_SESSION['menuid'] = $_REQUEST['id'];
$where['menuid'] = $_SESSION['menuid'];
} elseif ($_SESSION['menuid']) {
$where['menuid'] = $_SESSION['menuid'];
} else {
echo('请选择菜单');
}
$count = $menuitem->where($where)->count();

$list = $menuitem->field("id,name,pid,type,componentid,`order`,published,access,concat(path,'-',id) as bpath")->
order("bpath,id")->limit($limits)->where($where)->select();
$role = new Model('Role');
$rlist = $role->getField('id,name');
foreach ($list as $key => $value) {
if ($list[$key]['access'] != 0) {
$list[$key]['access'] = $rlist[$list[$key]['access']];
}
$list[$key]['signnum'] = count(explode('-', $value['bpath'])) - 1;
$list[$key]['marginnum'] = (count(explode('-', $value['bpath'])) - 1) * 20;
}
if ($list) {
$result = json_encode($list);
echo '{"totalCount":' . $count . ',"data":' . $result . '}';
} else {
echo '{"totalCount":' . $count .
',"data":[{"id":"0","name":"<font color=red><b>暂无记录</b></font>"}]}';
}
}
/**
* 构造路径
*/
function GetPath() {
$menu = new MenuItemModel();
$pid = $_POST['pid'];
$mi = $menu->field('id,path')->getById($pid);
$path = $pid != 0 ? $mi['path'] . '-' . $mi['id'] : 0;
return $path;
}
/**
* 添加
*/
function Add() {
$menu = new MenuItemModel();
switch ($_POST['fenlei']) {
case '1':$_POST['fenlei'] = 'Article';
case '2':$_POST['fenlei'] = 'Section';
case '3':$_POST['fenlei'] = 'Category';
}
$data['link'] = 'index.php/' . $_POST['fenlei'] . '/view/id/' . $_POST['ctype'];
$data['access'] = $_POST['access'];
$data['published'] = $_POST['published'];
$data['browserNav'] = $_POST['nav'];
$data['name'] = $_POST['name'];
$data['type'] = $_POST['fenlei'];
$data['menuid'] = $_POST['menuid'];
$data['path'] = $this->GetPath();
$data['pid'] = $_POST['pid'];
if (false !== $menu->data($data)->Add()) {
echo '{"success":true}';
} else {
echo '{"success":false}';
}
}
/**
* 保存
*/
function Save() {
$menu = new MenuItemModel();
switch ($_POST['fenlei']) {
case '1':$_POST['fenlei'] = 'Article';
case '2':$_POST['fenlei'] = 'Section';
case '3':$_POST['fenlei'] = 'Category';
}
$data['link'] = 'index.php/' . $_POST['fenlei'] . '/view/id/' . $_POST['ctype'];
$data['access'] = $_POST['access'];
$data['published'] = $_POST['published'];
$data['browserNav'] = $_POST['nav'];
$data['name'] = $_POST['name'];
$data['type'] = $_POST['fenlei'];
$data['menuid'] = $_POST['pid'];
$data['path'] = $this->getPath();
$data['id'] = $_POST['id'];
if (false !== $menu->data($data)->Save()) {
echo '{"success":true}';
} else {
echo '{"success":false}';
}
}
/**
* 移除
*/
function Remove() {
$did = $_POST['deleteIds'];
if (!empty($did)) {
$user = new MenuItemModel();
if (false !== $user->where('id in(' . $did . ')')->delete()) {
echo "{'success':true}";
} else {
echo "{'success':true}";
}
} else {
echo "{'success':true}";
}
}
/**
* 构造路径
*/
function ParentMenu() {
$menuitem = new MenuItemModel();
$result = $menuitem->field("id,name,pid,type,componentid,`order`,published,access,concat(path,'-',id) as bpath")->
order("bpath,id")->where('pid=' . $_SESSION['menuid'])->select();
$json = array();
$ids = array();
foreach ($result as $val) {
if (in_array($ids, $val['id'])) {
return;
}
;
array_puah($val['id'], $ids);
$json['id'] = $val["id"];
$json['text'] = $val["name"];
$json['leaf'] = 'true';
foreach ($resultc as $cval) {
if ($cval['pid'] = $val['id']) {
$json['children']['id'] = $cval['id'];
$json['children']['text'] = $cval['name'];
$json['children']['leaf'] = 'false';
}
}
}
if ($result) {
$result = json_encode($result);
echo $result;
} else {
echo "{'success':false}";
}
}
/**
* 构造extjs下拉树内层数据
*/
function InsideTree($list) {
$str = '';
foreach ($list as $key => $value) { //遍历列表
if ($str != '')
$str .= ',';
$in_str = '';
foreach ($value as $n => &$v) { //遍历数组
if ($in_str != '')
$in_str .= ',';
if ($n == 'leaf') {
$in_str .= $n . ":" . $v;
} else {
if ($n == 'children' && is_array($v)) {
$v = $this->InsideTree($v);
$in_str .= $n . ":" . $v;
} else {
$in_str .= $n . ":'" . $v . "'";
}
}
}
$str .= '{' . $in_str . '}';
}
return $str = '[' . $str . ']';
}
/**
* 完成extjs下拉树内层数据构造
*/
function Tree() {
$menuitem = new MenuItemModel();
$result = $menuitem->field("id,name as text,pid,type")->select();
$tree = new TreeAction();
$tree->input_arr = $result;
$data = $tree->GetTreeData(0);
echo "[{id:'0',text:'顶层菜单',children:" . $this->InsideTree($data) . "}]";
}
function Art() {
$limit = $_POST['limit'];
$start = $_POST['start'];
$limits = $start . "," . $limit;
$role = new ArticleViewModel();
$where['Article.published'] = array('gt', 0);
$count = $role->where($where)->count();
$result = $role->order('id')->where($where)->limit($limits)->select();
if ($result) {
$result = json_encode($result);
echo '{"totalCount":' . $count . ',"root":' . $result . '}';
} else {
echo '{"totalCount":' . $count .
',"root":[{"id":"0","atitle":"<font color=red><b>暂无记录</b></font>"}]}';
}
}
function Car() {
$limit = $_POST['limit'];
$start = $_POST['start'];
$limits = $start . "," . $limit;
$role = new CategoryViewModel();
$where['Category.published'] = array('gt', 0);
$count = $role->where($where)->count();
$result = $role->order('id')->where($where)->limit($limits)->select();
foreach ($result as & $val) {
$val['atitle'] = $val['ctitle'];
}
if ($result) {
$result = json_encode($result);
echo '{"totalCount":' . $count . ',"root":' . $result . '}';
} else {
echo '{"totalCount":' . $count .
',"root":[{"id":"0","atitle":"<font color=red><b>暂无记录</b></font>"}]}';
}
}
function Sec() {
$limit = $_POST['limit'];
$start = $_POST['start'];
$limits = $start . "," . $limit;
$role = new SectionModel();
$where['published'] = array('gt', 0);
$count = $role->where($where)->count();
$result = $role->order('id')->where($where)->limit($limits)->select();
foreach ($result as & $val) {
$val['atitle'] = $val['title'];
}
if ($result) {
$result = json_encode($result);
echo '{"totalCount":' . $count . ',"root":' . $result . '}';
} else {
echo '{"totalCount":' . $count .
',"root":[{"id":"0","atitle":"<font color=red><b>暂无记录</b></font>"}]}';
}
}
}
?>


<?php
// +----------------------------------------------------------------------
// | thinkphp+extjs +jquery CMS管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://blog.aigouw.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 殷志朋 <601046124@qq.com> 7391390
// +----------------------------------------------------------------------
// | Q&A:群 7391390
// +----------------------------------------------------------------------
//
class TreeAction {
/**
* 生成数所需要的二维数组
*/
public $input_arr;

/**
* 数组父键值
*/
public $parent_tag = 'pid';

public function __construct($arr = array()) {
$this->input_arr = $arr;
return is_array($this->input_arr);
}
/**
* 递归取树形纯数组
* @param  int    $start_id 从哪个id开始遍历,0为根节点
* @return array  排序好的数组
*/
public function GetTreeData($start_id = 0, $leaf = false) {
$tree_arr = array();
$child = $this->GetChild($start_id, $leaf);
if (is_array($child) && $child) {
foreach ($child as &$child_value) {
$tree_arr[$child_value['id']] = $child_value;
$data = $this->GetChild($child_value['id']);
if (is_array($data) && count($data) >= 1) {
$leaf = 'false';
} else {
$leaf = 'true';
}
$tree_arr[$child_value['id']]['leaf'] = $leaf;
$tree_arr[$child_value['id']]['children'] = $this->GetTreeData($child_value['id']);
}
}
return $tree_arr;
}
/**
* 获取子元素
* @param  string $my_id 其id
* @return array  $data  子元素数组
*/
private function GetChild($my_id, $leaf) {
$data = array();
foreach ($this->input_arr as $key => $value) {
if ($value[$this->parent_tag] == $my_id) {
$data[$key] = $value;
}
}

return $data;
}

}
?>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362742-1-1.html 上篇帖子: php 无限分类 树形数据 格式化 下篇帖子: [AD] Shopilex v2.0
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表