数据库
建表
DROP TABLE IF EXISTS `yr_cate`;
CREATE TABLE `yr_cate` (
`cateid` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '栏目id',
`catename` varchar(50) NOT NULL COMMENT '栏目名称',
`pid` int(10) NOT NULL,
`path` varchar(200) NOT NULL COMMENT '路径',
`level` int(10) NOT NULL COMMENT '级别',
`model` enum('5','4','3','2','1') NOT NULL COMMENT '1文章,2行情,3公告,4招标,5资料',
PRIMARY KEY (`cateid`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8;


插入几条数据
-- ----------------------------
-- Records of yr_cate
-- ----------------------------
INSERT INTO `yr_cate` VALUES ('1', '行业动态', '0', '0,1', '1', '1');
INSERT INTO `yr_cate` VALUES ('2', '产业观察', '0', '0,2', '1', '1');
INSERT INTO `yr_cate` VALUES ('3', '行业新闻', '1', '0,1,3', '2', '1');
INSERT INTO `yr_cate` VALUES ('4', '协会新闻', '1', '0,1,4', '2', '1');
INSERT INTO `yr_cate` VALUES ('5', '名家专栏', '1', '0,1,5', '2', '1');
INSERT INTO `yr_cate` VALUES ('6', '企业新闻', '1', '0,1,6', '2', '1');
INSERT INTO `yr_cate` VALUES ('7', '行情价格', '0', '0,7', '1', '2');
INSERT INTO `yr_cate` VALUES ('8', '品种快讯', '7', '0,7,8', '2', '2');
INSERT INTO `yr_cate` VALUES ('17', '品种分析', '7', '0,7,17', '2', '1');
INSERT INTO `yr_cate` VALUES ('18', '价格指数', '7', '0,7,18', '2', '2');
INSERT INTO `yr_cate` VALUES ('20', '招标采购', '0', '0,20', '1', '4');
INSERT INTO `yr_cate` VALUES ('21', '中标公告', '20', '0,20,21', '2', '1');
INSERT INTO `yr_cate` VALUES ('22', '招标信息', '20', '0,20,22', '2', '4');
INSERT INTO `yr_cate` VALUES ('23', '资料库', '0', '0,23', '1', '5');
INSERT INTO `yr_cate` VALUES ('25', '名人名企', '0', '0,25', '1', '1');
INSERT INTO `yr_cate` VALUES ('27', '金融服务', '0', '0,27', '1', '1');
控制器
cate.php
<?php
namespace app\admin\controller;
use think\Db;
use app\admin\model\Cate as CateModel;
use app\admin\Controller\Base;
class Cate extends Base
{
public function cateslist(){
$cateslist = db('cate')->order('cateid asc')->select();
$this->assign('cateslist',$cateslist);
return $this->fetch();
}
//添加分类
public function cateadd()
{
//查询已添加的分类数据
$cateslist=db('cate')->field(['*','concat(path,",",cateid)'=>'paths'])->order('paths asc')->select();
foreach($cateslist as $k=>$v){
$cateslist[$k]['catename']=str_repeat("|------", $v['level']).$v['catename'];
}
$this->assign('cateslist',$cateslist);
//添加分类数据
if(request()->isPost()){
$m=model('cate');
$data = [
'catename' => input('catename'),
'pid' => input('pid'),
'model' => input('model'),
];
if($data['pid'] == 0){
$data['level'] = 1;
$data['path'] = '0';
//验证数据
$validate = validate('Cate');
if(!$validate->scene('add')->check($data)){
$this->error($validate->getError());
}
if($m->save($data)){
$reId=$m->cateid;
if($reId){
$m->where('cateid',$reId)->update(['path'=> $data['path'].','.$reId]);
$this->success('更新成功','cate/cateslist');
}
}else {
$this->error('更新失败');
}
}else{
$cates=$m->find($data['pid']);
$data['path']=$cates['path'];
$data['level']= substr_count($data['path'],',') + 1;
//验证数据
$validate = validate('Cate');
if(!$validate->scene('add')->check($data)){
$this->error($validate->getError());
}
$re=$m->save($data);
if($re){
$reId=$m->cateid;
if($reId){
$m->where('cateid',$reId)->update(['path'=> $cates['path'].','.$reId]);
$this->success('更新成功','cate/cateslist');
}
}else {
$this->error('更新失败');
}
}
}
return $this->fetch();
}
public function cate_del(){
$id=$_GET['id'];
$m=model('cate');
$data=$m->where('pid','=',$id)->find();
if($data){
$str="该分类存在下级栏目,无法删除";
echo json_encode($str);
}else{
//$re=$m->delete($id);
$re=$m->destroy($id);
if($re){
echo 1;
}
}
}
//ajax获取分类信息
public function cate_ajax(){
$m=model('cate');
$data=$m->field(['cateid as id','catename as name','pid'])->select();
echo json_encode($data);
}
}
上文cateadd为添加分类处理方法,也有删除等方法。
模板文件
cate_add.html 添加分类view文件
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<!--[if lt IE 9]>
<script type="text/javascript" src="__LIB__/html5.js"></script>
<script type="text/javascript" src="__LIB__/respond.min.js"></script>
<![endif]-->
<link href="__STATIC__/h-ui/css/H-ui.min.css" rel="stylesheet" type="text/css" />
<link href="__STATIC__/h-ui.admin/css/H-ui.admin.css" rel="stylesheet" type="text/css" />
<!-- <link href="__LIB__/icheck/icheck.css" rel="stylesheet" type="text/css" />
--><link href="__LIB__/Hui-iconfont/1.0.8/iconfont.css" rel="stylesheet" type="text/css" />
<!--[if IE 6]>
<script type="text/javascript" src="http://__LIB__.h-ui.net/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script><![endif]-->
<title>添加产品分类</title>
</head>
<body>
<div class="pd-20">
<form action="{:url('cate/cateadd')}" method="post" class="form form-horizontal" id="form-user-add">
<div class="row cl">
<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>分类名称:</label>
<div class="formControls col-5">
<input type="text" class="input-text" value="" placeholder="" id="user-name" name="catename">
</div>
<div class="col-5"> </div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-2">选择分类</label>
<div class="formControls col-5">
<span class="select-box">
<select class="select" size="1" name="pid">
<option value="0" selected>顶级分类</option>
{volist name='cateslist' id='vo'}
<option value="{$vo.cateid}">{$vo.catename}</option>
{/volist}
</select>
</span>
</div>
<div class="col-5"> </div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-2">内容模型</label>
<div class="formControls col-5">
<span class="select-box">
<select class="select" size="1" name="model">
<option value="1" selected>文章</option>
<option value="2">行情</option>
<option value="3">公告</option>
<option value="4">招标</option>
<option value="5">资料</option>
</select>
</span>
</div>
<div class="col-5"> </div>
</div>
<div class="row cl">
<div class="col-9 col-offset-2">
<input class="btn btn-primary radius" type="submit" value=" 提交 ">
</div>
</div>
</form>
</div>
<!--_footer 作为公共模版分离出去-->
{include file="common/_footer" /}
<!--/_footer /作为公共模版分离出去-->
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="__LIB__/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="__LIB__/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="__LIB__/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript">
$(function(){
$('.skin-minimal input').iCheck({
checkboxClass: 'icheckbox-blue',
radioClass: 'iradio-blue',
increaseArea: '20%'
});
});
</script>
</body>
</html>
cateslist.html 分类列表view文件
<!--_meta 作为公共模版分离出去-->
{include file="common/_meta" /}
<!--/meta 作为公共模版分离出去-->
<title>产品分类</title>
<link rel="stylesheet" href="__LIB__/zTree/v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
</head>
<body>
<!--_header 作为公共模版分离出去-->
{include file="common/_header" /}
<!--/_header 作为公共模版分离出去-->
<!--_menu 作为公共模版分离出去-->
{include file="common/_menu" /}
<!--/_menu 作为公共模版分离出去-->
<section class="Hui-article-box">
<nav class="breadcrumb"><i class="Hui-iconfont"></i> 首页 <span class="c-gray en">></span> 产品管理 <span class="c-gray en">></span> 产品分类 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont"></i></a></nav>
<div class="Hui-article">
<article class="cl pd-20">
<span style="color:red;">*点击分类名称,可立即删除</span>
<table class="table">
<tr>
<td width="200" class="va-t"><ul id="treeDemo" class="ztree"></ul></td>
<td class="va-t"><iframe ID="testIframe" Name="testIframe" FRAMEBORDER=0 SCROLLING=AUTO width=100% height=390px SRC="cateadd.html"></iframe></td>
</tr>
</table>
</article>
</div>
</section>
<!--_footer 作为公共模版分离出去-->
{include file="common/_footer" /}
<!--/_footer /作为公共模版分离出去-->
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="__LIB__/My97DatePicker/4.8/WdatePicker.js"></script>
<script type="text/javascript" src="__LIB__/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="__LIB__/laypage/1.2/laypage.js"></script>
<script type="text/javascript" src="__LIB__/zTree/v3/js/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript">
var zNodes;
$.ajax({
//
url:"cate_ajax",
type:'get',
dataType:'json',
async: false,
success:function(data){
zNodes=data;
//console.log(data);
}
});
var setting = {
view: {
dblClickExpand: false,
showLine: false,
selectedMulti: false
},
data: {
simpleData: {
enable:true,
idKey: "id",
pIdKey: "pid",
rootPId: ""
}
},
callback: {
beforeClick: function(treeId, treeNode) {
//console.log(treeNode.id);
$.ajax({
//
url:"{:url('cate_del')}",
type:'get',
data:{id:treeNode.id},
dataType:'json',
//console.log(data);
async: false,
success:function(data){
if(data==1){
alert("删除成功");
parent.location.href="{:url('cateslist')}";
}else{
alert(data);
parent.location.href="{:url('cateslist')}";
}
}
})
}
}
};
var code;
function showCode(str) {
if (!code) code = $("#code");
code.empty();
code.append("<li>"+str+"</li>");
}
$(document).ready(function(){
var t = $("#treeDemo");
t = $.fn.zTree.init(t, setting, zNodes);
demoIframe = $("#testIframe");
demoIframe.bind("load", loadReady);
var zTree = $.fn.zTree.getZTreeObj("tree");
zTree.selectNode(zTree.getNodeByParam("cateid",'11'));
});
</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>
验证脚本
<?php
namespace app\admin\validate;
use think\Validate;
class Cate extends Validate
{
protected $rule = [
'catename' => 'require|max:25|unique:cate',
];
protected $message = [
'catename.require' => '名字必须填写',
'catename.max' => '名字最多不能超过25个字符',
'catename.unique' => '栏目名字已经存在',
'password.require' => '密码必填',
];
protected $scene=[
'add' => ['catename'],
'edit'=> ['catename'],
];
}
本文仅简书和本人博客www.912616.com发布,不定期整理,能力有限,存在诸多错误,谢绝转载。
网友评论