<volist name="testing_cat" id="vo">
<tr cate-id='{$vo.id}' fid='{$vo.pid}' >
<td>
<div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id='2'><i class="layui-icon">i>div>
td>
<td>{$vo.pid}-{$vo.id}td>
<td>
<i class="layui-icon x-show" status='true'>i>
{$vo.name}
td>
<td><input type="text" class="layui-input x-sort" name="order" value="1">td>
<td>
<input type="checkbox" name="switch" lay-text="开|停" checked="" lay-skin="switch">
td>
<td class="td-manage">
<button class="layui-btn layui-btn layui-btn-xs" onclick="x_admin_show('编辑','admin_edit.html?id={$vo.id}&name={$vo.name}&action=1')" ><i class="layui-icon">i>编辑button>
<button class="layui-btn layui-btn-warm layui-btn-xs" onclick="x_admin_show('编辑','admin_edit.html?id={$vo.id}&name={$vo.name}&level=1&action=2')" ><i class="layui-icon">i>添加子栏目button>
<button class="layui-btn-danger layui-btn layui-btn-xs" onclick="member_del(this,{$vo.id})" href="javascript:;" ><i class="layui-icon">i>删除button>
td>
tr>
<volist name="vo.child" id="voo">
<tr cate-id='{$voo.id}' fid='{$voo.pid}' >
<td>
<div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id='2'><i class="layui-icon">i>div>
td>
<td>{$voo.id}td>
<td>
<i class="layui-icon x-show" status='true'>i>
{$voo.name}
td>
<td><input type="text" class="layui-input x-sort" name="order" value="1">td>
<td>
<input type="checkbox" name="switch" lay-text="开|停" checked="" lay-skin="switch">
td>
<td class="td-manage">
<button class="layui-btn layui-btn layui-btn-xs" onclick="x_admin_show('编辑','admin_edit.html?id={$voo.id}&name={$voo.name}&action=1&pname={$vo.name}')" ><i class="layui-icon">i>编辑button>
<button class="layui-btn layui-btn-warm layui-btn-xs" onclick="x_admin_show('编辑','admin_edit.html?id={$voo.id}&name={$voo.name}&level=2&action=2&pname={$vo.name}')" ><i class="layui-icon">i>添加子栏目button>
<button class="layui-btn-danger layui-btn layui-btn-xs" onclick="member_del(this,{$voo.id})" href="javascript:;" ><i class="layui-icon">i>删除button>
td>
tr>
<volist name="voo.child" id="vooo">
<tr cate-id='{$vooo.id}' fid='{$vooo.pid}' >
<td>
<div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id='2'><i class="layui-icon">i>div>
td>
<td>{$vooo.id}td>
<td>
├{$vooo.name}
td>
<td><input type="text" class="layui-input x-sort" name="order" value="1">td>
<td>
<input type="checkbox" name="switch" lay-text="开|停" checked="" lay-skin="switch">
td>
<td class="td-manage">
<button class="layui-btn layui-btn layui-btn-xs" onclick="x_admin_show('编辑','admin_edit.html?id={$vooo.id}&name={$vooo.name}&action=1&pname={$voo.name}')" ><i class="layui-icon">i>编辑button>
<button class="layui-btn layui-btn-warm layui-btn-xs" onclick="x_admin_show('编辑','admin_edit.html?id={$vooo.id}&name={$vooo.name}&level=3&action=2&pname={$voo.name}')" ><i class="layui-icon">i>添加子栏目button>
<button class="layui-btn-danger layui-btn layui-btn-xs" onclick="member_del(this,{$vooo.id})" href="javascript:;" ><i class="layui-icon">i>删除button>
td>
tr>
volist>
volist>
volist>
弹窗页面内容,用于编辑与添加子栏目
<form id="cate_form" class="layui-form">
<input type="hidden" name="id" value="{$id}">
<input type="hidden" name="type" value="{$type}">
<div class="layui-form-item">
<label for="username" class="layui-form-label">
<span class="x-red">*span>名称
label>
<div class="layui-input-inline">
<input type="text" id="username" name="catename" required="" lay-verify="required"
autocomplete="off" value="{$name}" class="layui-input">
div>
<div class="layui-form-mid layui-word-aux">
<span class="x-red">*span>父级:{$pname}
div>
div>
<div class="layui-form-item">
<label class="layui-form-label"><span class="x-red">*span>状态label>
<div class="layui-input-block">
<input type="checkbox" name="switch" lay-text="开启|停用" checked="" lay-skin="switch">
div>
div>
<div class="layui-form-item">
<label for="L_repass" class="layui-form-label">
label>
<button id="btnButton" class="layui-btn" lay-filter="add" lay-submit="">
增加
button>
div>
form>
/*用户-删除*/
function member_del(obj,id){
layer.confirm('确认要删除吗?',function(index){
//发异步删除数据
$.post("__URL__/del",{id:id},function(data){
//console.log(data);
$(obj).parents("tr").remove();
layer.msg('已删除!',{icon:1,time:1000});
})
});
}
弹窗页面js内容,用于提交编辑与添加子栏目,
$("#btnButton").on("click",function(){
$('#cate_form').ajaxSubmit( //ajax方式提交表单
{
url: '__URL__/add',
type: 'post',
dataType: 'json',
beforeSubmit: function () {},
success: function (data) {
console.log(data);
},
//clearForm: false,//禁止清楚表单
//resetForm: false //禁止重置表单
});
});
tp的S方法用于缓存分类数据 (Category.php)
/**
* 分类
*/
namespace Testing\Service;
class Category {
public $mCategory = null;
static public $mInstance = null;
static public $mKey = 'CATEGORY';
static public function getInstance()
{
if (!is_object(self::$mInstance)) {
self::$mInstance = new Category;
//dump(S(self::$mKey));die();
if (S(self::$mKey)) {
self::$mInstance->loadFromCache();
} else {
self::$mInstance->loadFromDb();
}
}
return self::$mInstance;
}
public function loadFromCache()
{
self::$mInstance->mCategory = S(self::$mKey);
}
public function loadFromDb()
{
self::$mInstance->mCategory = $this->getCates();
// 设置缓存
S(self::$mKey, self::$mInstance->mCategory);
}
public function cleanCache(){
S(self::$mKey,null);
}
//递归所有分类
public function getCates($pid=0){
$cate = M('testing_cat');
$cates = $cate->where(array('pid'=>$pid,'display'=>'1'))->select();
//遍历分类
$arr = array();
if($cates){
foreach ($cates as $key => $value) {
$temp['id'] = $value['id'];
$temp['name'] = $value['name'];
$temp['pid']=$value['pid'];
$temp['html_id']=$value['html_id'];
//递归获取子集分类
$temp['child'] = $this->getCates($temp['id']);
$arr[] = $temp;
}
return $arr;
}
}
}
分类表页面渲染 ,缓存分类文件的位置 use \Testing\Service\Category;
public function cate(){
$res_cate=category::getInstance();//调用缓存数据\Service\Category
$res_cate = get_object_vars($res_cate);
$this->assign('testing_cat',$res_cate['mCategory']);
$res_cat = M()->query('select id,name,pid from js_testing_cat where pid = 0');
$this->assign('top_cat',$res_cat);
$this->display();
}
弹窗页面的渲染编辑与添加,目前的暂不支持添加四级分类
public function admin_edit(){
$id = I('id');
$name = I('name');
//$level = I('level');
$action = I('action');
$pname = I('pname');
if($level==3){
die("暂不支持第四级添加!");
}
$pname==""?$pname="顶级":$pname=$pname;
if($action==1){//编辑
$this->assign('name',$name);
$this->assign('type','edit');
}else{
$this->assign('type','add');
}
//category::cleanCache();
$this->assign('id',$id);
$this->assign('pname',$pname);
$this->display();
}
编辑与添加
//编辑与添加
public function add(){
$id = I('id');
$type = I('type');
$catename = I('catename');
if($type=='edit'){
$res = M()->execute('update js_testing_cat SET name = "'.$catename.'" where id = '.$id.'');
if($res){
category::cleanCache();
}
}else{
if(!empty($catename)){
$res = M()->execute('INSERT INTO js_testing_cat SET name = "'.$catename.'", pid = '.$id.'');
category::cleanCache();
$this->ajaxReturn($res);
}
}
}
这里的删除为假删除display = “0”
//删除
public function del(){
$id = I('id');
if(!empty($id)){
$res = M()->execute('update js_testing_cat SET display = "0" where id = '.$id.'');
category::cleanCache();
$this->ajaxReturn($res);
}
}
~~***方法不够好,下次还需要改进。***~~