菜单项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;
}
}
?>
分享到:
相关推荐
2. **定义树形数据**:下拉树的数据通常以JSON格式存储,包含节点ID、文本、子节点等信息。例如: ```javascript var treeData = [ { id: '1', text: '父节点1', children: [ { id: '1.1', text: '子节点1.1' }, ...
1. **下拉树组件**:在ExtJS中,树形组件(TreePanel)用于展示层次结构的数据。它支持动态加载、拖放操作、节点展开和折叠等功能。而下拉树是将树形结构嵌入到下拉框中,通常用于选择一组相关的项目,比如文件夹...
使用ExtJs获取后台json格式的数据必须的七个jar包,commons-beanuti-1s-1.7.0.jar,commons-collections-3.1.jar,commons-lang-2.5.jar,commons-logging-1.0.4.jar,ezmorph-1.0.4.jar,json-lib-2.1.jar,...
ExtJS是一款强大的JavaScript框架,...综上所述,ExtJS的下拉树组件是通过组合ComboBox和TreePanel的特性,为用户提供了一种高效的数据选择方式。了解其工作原理和关键API,有助于开发者在实际项目中灵活运用和定制。
ExtJS 4 下拉树(TreeCombo)是一种组合控件,它将传统的下拉框与树形结构结合在一起,提供了一种在有限空间内展示层级数据的高效方式。这种控件在很多场合都非常实用,例如在需要用户选择分类或者层级结构的场景中...
总的来说,"Extjs Json树封装"涉及的核心知识点有:JSON数据格式、ExtJS树形组件、树加载器(`JsonTreeLoader`)、可能的自定义插件(`JsonPluginTreeLoader`)以及如何在实际应用中配置和使用它们。理解并熟练运用...
首先,JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它基于JavaScript的一个子集,易于人阅读和编写,同时也易于机器解析和生成。在后端服务器上,开发者通常会将数据处理成JSON格式,以便于前端...
这段代码展示了如何根据业务逻辑动态构建JSON格式的树形数据。`Dep`对象代表数据库中的部门信息,`checkHasChild`方法用于判断部门是否有子部门,从而确定节点是否为叶子节点。 #### 五、总结 Extjs自定义组件—...
数据源通常是JSON格式,包含了树节点的信息,包括父节点和子节点的关系。然后,我们需要定义ComboBox Tree的配置项,例如`store`用于设置数据源,`displayField`用于显示节点的文本,`valueField`用于保存选中节点的...
在这个特定的情况下,我们讨论的是一个定制的下拉树菜单控件——ComboBoxTree,它在ExtJS4中实现了单选和多选功能,并且具备展开选中指定节点的能力。这个控件在实际项目中已经被广泛使用并证明了其稳定性和实用性,...
"Extjs 无限树菜单 后台拼接json"这个主题涉及到如何在后端生成无限级别的树形菜单数据,并通过JSON格式传输到前端进行渲染。 首先,我们要理解树形菜单的基本概念。树形菜单是一种以节点和层级关系展示数据的控件...
在本篇文章中,我们将深入探讨如何使用Extjs4中的FormPanel组件从后台加载JSON数据,并将其映射到表单的各个字段中进行显示。这是一项非常实用的技术,尤其是在需要动态填充表单的情况下。 ### 一、Extjs4 ...
通过这种方式,我们解决了 Struts2 JSON 插件返回数据格式与 ExtJS Tree 期待格式不匹配的问题。现在,树形菜单应该能够正确地动态加载并显示从服务器获取的数据。这个例子展示了如何在实际开发中灵活应对数据格式...
ExtJs4.2没有直接提供下拉树这个组件,但是有例子可以用,文件位置:ext-4.2.1.883\examples\ux\TreePicker.js 但是它有点小毛病吧:默认显示了根节点;达到最小高度时再展开节点,高度不能自动调整。 所以我做了一...
ExtJs3下拉树 分两种方式调用:第一种: xtype : 'combotree', name : 'dm', fieldLabel : 'dm', tree : this.ct this.ct = new Ext.tree.TreePanel({ autoScroll : true, height : 250, border : false, ...
extjs异步树,多选树,json数据机构,集成spring,struts例子,extjs异步树,多选树,json数据机构,集成spring,struts例子,extjs异步树,多选树,json数据机构,集成spring,struts例子,extjs异步树,多选树,json...
`JsonReader`是ExtJs中的数据读取器,它解析JSON格式的数据,并将其转化为可以供GridPanel使用的Record对象。关键配置包括: 1. `id`:指定数据行对象中包含数据行ID的属性名。 2. `totalProperty`:用于获取数据集...
在ExtJS动态树中,Struts需要将后端数据转化为JSON格式,以便前端解析。JSON对象通常包含键值对,数组和嵌套的对象,可以方便地表示复杂的数据结构。 3. **Struts2 Action和Result**: 在Struts2框架中,Action类是...
4. **Json Store**: 数据存储在JSON格式中,这是ExtJS常用的存储方式。我们需要创建一个`Ext.data.TreeStore`,并指定它的`root`、`proxy`(通常是`AjaxProxy`)以及`model`,确保数据能正确加载到TreePanel中。 5....
标题 "Extjs 从远程获取json类型的list数据" 涉及的是在Web开发中使用Extjs框架与服务器进行交互,获取JSON格式的数据并展示在列表(List)组件中的技术。Extjs是一款强大的JavaScript库,用于构建富客户端的桌面级...