1.模型
<?php
namespace backend\models;
use Yii;
use yii\db\ActiveRecord;
class Shop_class extends ActiveRecord{
public static function tableName(){
return 'shop_class';
}
public function rules(){
return [
['title','required','message'=>'分类名不能为空','on'=>['add','detail']],
['pid','required','message'=>'分类不能为空','on'=>['add','detail']]
];
}
public function attributeLabels(){
return [
'title'=>'分类名',
'pid'=>'分类'
];
}
public function adds($data){
$this->scenario='add';
if($this->load($data) && $this->validate()){
$this->creatertime=time();
if($this->save(false)){
return true;
}else{
return false;
}
}else{
return false;
}
}
public function detail($data){
$this->scenario='detail';
if($this->load($data) && $this->validate()){
$this->creatertime=time();
if($this->save(false)){
return true;
}else{
return false;
}
}else{
return false;
}
}
}
?>
2.控制器
<?php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use backend\models\Shop_class;
use yii\helpers\Url;
class Shop_classController extends Controller{
public $layout="mains";
public function actionIndex(){
$model=Shop_class::find()->asArray()->all();
$data=$this->getlevel($model,0);
return $this->render('index',[
'model'=>$data
]);
}
public function actionAdd(){
$model=new Shop_class();
$req=Yii::$app->request;
$data=Shop_class::find()->all();
$list=$this->getdata($data,0);
if($req->isPost){
$post=$req->post();
$res=$model->adds($post);
if($res){
Yii::$app->session->setFlash('success','添加成功');
return $this->redirect(Url::toRoute(['index']));
}else{
Yii::$app->session->setFlash('error','添加失败');
}
}
return $this->render('add',[
'model'=>$model,
'list'=>$list
]);
}
public function actionDel(){
$req=Yii::$app->request;
$id=$req->get('id');
$one=Shop_class::find()->where(['pid'=>$id])->one();
$model=Shop_class::find()->where(['id'=>$id])->one();
if(!$one){
$res=$model::deleteAll(['id'=>$id]) ? true : false;
if($res){
Yii::$app->session->setFlash('success','删除成功');
return $this->redirect(['index']);
}else{
Yii::$app->session->setFlash('error','删除失败');
return $this->redirect(['index']);
}
}else{
Yii::$app->session->setFlash('error','先删除子分类');
return $this->redirect(['index']);
}
}
public function actionStatic(){
$req=Yii::$app->request;
$id=$req->get('id');
$model=Shop_class::find()->where(['id'=>$id])->one();
$static=$model['static']==1?0:1;
$model->static=$static;
$res=$model->save();
if($res){
return \json_encode(['res'=>1,'message'=>'切换成功','static'=>$static]);
}else{
return \json_encode(['res'=>0,'message'=>'切换失败','static'=>$static]);
}
}
public function actionDetail(){
$req=Yii::$app->request;
$id=$req->get('id');
$model=Shop_class::find()->where(['id'=>$id])->one();
$data=Shop_class::find()->all();
$list=$this->getdata($data,0);
if($req->isPost){
$post=$req->post();
$res=$model->detail($post);
if($res){
Yii::$app->session->setFlash('success','修改成功');
return $this->redirect(Url::toRoute(['index']));
}else{
Yii::$app->session->setFlash('error','修改失败');
}
}
return $this->render('add',[
'model'=>$model,
'list'=>$list
]);
}
public function getdata($data,$pid,$cover=''){
static $list=array();
$list['0']='顶级分类';
foreach($data as $val){
if($val['pid']==$pid){
$list[$val['id']]=$cover.$val['title'];
$this->getdata($data,$val['id'],$cover.'|--');
}
};
return $list;
}
public function getlevel($data,$pid,$level=''){
static $arr=array();
foreach($data as $val){
if($val['pid']==$pid){
$val['level']=$level;
$arr[]=$val;
$this->getlevel($data,$val['id'],$level.'|--');
}
}
return $arr;
}
}
?>
3.视图(添加视图和修改视图)
<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;
?>
<div class="content">
<div class="container-fluid">
<div id="pad-wrapper" class="new-user">
<div class="row-fluid header">
<h3>添加分类</h3>
</div>
<div class="row-fluid form-wrapper">
<!-- left column -->
<div class="span9 with-sidebar">
<div class="container">
<?$form=ActiveForm::begin([
'options'=>[
'class'=>'new_user_form inline-input'
],
'fieldConfig'=>[
'template'=>'{label}<div class="span12 field-box">{input}</div>{error}'
]
])?>
<?=$form->field($model,'title')->textInput(['class'=>'span9','placeholder'=>'请输入分类名'])?>
<?=$form->field($model,'pid')->dropDownList($list,['class'=>'ui-select span5'])?>
<?=Html::submitButton('提交',['class'=>'btn-glow primary'])?>
<?ActiveForm::end()?>
</div>
</div>
</div>
</div>
</div>
</div>
4.数据展示
<?php
use yii\helpers\Url;
?>
<!-- main container -->
<div class="content">
<div class="container-fluid">
<div id="pad-wrapper" class="users-list">
<div class="row-fluid header">
<h3>Users</h3>
<div class="span10 pull-right">
<input type="text" class="span5 search" placeholder="Type a user's name..." />
<!-- custom popup filter -->
<!-- styles are located in css/elements.css -->
<!-- script that enables this dropdown is located in js/theme.js -->
<div class="ui-dropdown">
<div class="head" data-toggle="tooltip" title="Click me!">
Filter users
<i class="arrow-down"></i>
</div>
<div class="dialog">
<div class="pointer">
<div class="arrow"></div>
<div class="arrow_border"></div>
</div>
<div class="body">
<p class="title">
Show users where:
</p>
<div class="form">
<select>
<option />Name
<option />Email
<option />Number of orders
<option />Signed up
<option />Last seen
</select>
<select>
<option />is equal to
<option />is not equal to
<option />is greater than
<option />starts with
<option />contains
</select>
<input type="text" />
<a class="btn-flat small">Add filter</a>
</div>
</div>
</div>
</div>
<a href="new-user.html" class="btn-flat success pull-right">
<span>+</span>
NEW USER
</a>
</div>
</div>
<!-- Users table -->
<div class="row-fluid table">
<table class="table table-hover">
<thead>
<tr>
<th class="span4 sortable">
分类
</th>
<th class="span3 sortable">
状态
</th>
<th class="span2 sortable">
操作
</th>
</tr>
</thead>
<tbody>
<!-- row -->
<?php
foreach($model as $val){
?>
<tr class="first">
<td>
<?=$val['level'].$val['title']?>
</td>
<td style="cursor: pointer;" id="fun<?=$val['id']?>" οnclick="fun(<?=$val['id']?>)">
<?=$val['static']==1?'正常':'禁止'?>
</td>
<td>
<a href="<?=Url::to(['detail','id'=>$val['id']])?>">修改</a>
<a href="<?=Url::to(['del','id'=>$val['id']])?>">删除</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<!-- <div class="pagination pull-right">
<ul>
<li><a href="#">‹</a></li>
<li><a class="active" href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">›</a></li>
</ul>
</div> -->
<!-- end users table -->
</div>
</div>
</div>
<script>
function fun($id){
$.get("<?=Url::to('/admin/shop_class/static')?>",{id:$id},function(data){
var data = JSON.parse(data);
if(data['res']==1){
if(data['static']==1){
$('#fun'+$id).html('正常');
}else{
$('#fun'+$id).html('禁止');
}
console.log(data['message']);
}else{
console.log(data['message']);
}
});
}
</script>
<!-- end main container -->