request->isAjax) { $data = []; $params = Yii::$app->request->get('Tag'); $query = mergeParams($query,$params); $countQuery = clone $query; //分页 if(isset($_GET['limit'])){ $query->limit(intval($_GET['limit']));} if(isset($_GET['offset'])){ $query->offset(intval($_GET['offset']));} //排序 if(isset($_GET['sort'])&&isset($_GET['sortOrder'])) { $resultList = $query->orderBy([$_GET['sort']=>($_GET['sortOrder']=='asc'?SORT_ASC:SORT_DESC)])->all(); } else { $resultList = $query->orderBy(['id'=>SORT_ASC])->all(); } foreach($resultList as $result) { $data[] = array('id'=>$result->id,'tag'=>$result->tag,'quote_num'=>$result->quote_num,'url'=>$result->url,'pinyin'=>$result->pinyin,'recommend'=>$result->recommend,'disabled'=>$result->disabled,'create_time'=>get_date($result->create_time,'Y-m-d H:i')); } $result = ["total"=>$countQuery->count(),"totalNotFiltered"=>$countQuery->count(),"rows"=>$data]; echo_json($result); } $this->tableTitle = array( array('field'=>'id','checkbox'=>false,'formatter'=>'checkboxFormatter','class'=>'col-md-1'), array('field'=>'tag','title'=>Tag::getAttributeName('tag'),'align'=>'center','class'=>'col-md-2'), array('field'=>'pinyin','title'=>Tag::getAttributeName('pinyin'),'align'=>'center','sortable'=>true,'formatter'=>'editFormatter','class'=>'col-md-1'), array('field'=>'quote_num','title'=>Tag::getAttributeName('quote_num'),'align'=>'center','sortable'=>true,'formatter'=>'editFormatter','class'=>'col-md-1'), array('field'=>'recommend','title'=>Tag::getAttributeName('recommend'),'align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-2'), array('field'=>'disabled','title'=>Tag::getAttributeName('disabled'),'align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-2'), array('field'=>'create_time','title'=>Tag::getAttributeName('create_time'),'align'=>'center','sortable'=>true,'class'=>'col-md-2'), array('field'=>'operate','title'=>Tag::getAttributeName('operate'),'align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter','class'=>'col-md-2'), ); $this->tableConfig = array('table'=>Tag::shortTableName(),'url'=>$this->createRealUrl(['admin/tag/list']),'setFieldUrl'=>$this->createRealUrl(['admin/tag/setfield']),'idField'=>Tag::modelPrimaryKey(),'checkbox'=>1,'dropmenu'=>1,'pagination'=>true,'pagesize'=>20,'refresh'=>true); return $this->render('list',array('model'=>new Tag())); } //添加 function actionAdd() { $model = new Tag(); if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){ $exist = Tag::find()->where("tag='".$model->tag."'")->exists(); if($exist) { $msgdata = ['error' => 1,'msg' => '已存在该标签,添加失败']; echo_json($msgdata); } $model->create_time = TIMESTAMP; if(!$model->validate()) { $msgdata = ['error' => 1,'msg' => $model->returnFirstError()]; } else { $tagList = explode("\r\n",$model->tag); if(count($tagList)>1) { foreach($tagList as $tag) { $exist = Tag::find()->where("tag='".$tag."'")->exists(); if($exist) { continue; } $newModel = new Tag(); $newModel->tag = $tag; $newModel->create_time = $model->create_time; $newModel->url = $model->url; $newModel->recommend = intval($model->recommend); $newModel->disabled = intval($model->disabled); $pinyin = new PinYin(); $newModel->pinyin = $pinyin->encode($newModel->tag); $newModel->save(); } $msgdata = ['error' => 0,'msg' => '操作成功']; } else { $pinyin = new PinYin(); $model->pinyin = $pinyin->encode($model->tag); if($model->save()) { $msgdata = ['error' => 0,'msg' => '操作成功']; } else { $msgdata = ['error' => 1,'msg' => '操作失败']; } } } echo_json($msgdata); } return $this->renderAjax('add',array('model'=>$model)); } //批量删除 public function actionMultidelete() { $ids = Yii::$app->request->get('ids'); if(!empty($ids)) { Tag::deleteAll("id in(".$ids.")"); $msgdata = ['error' => 0,'msg' => '操作成功']; } else { $msgdata = ['error' => 1,'msg' => '请选择操作记录']; } echo_json($msgdata); } function actionEdit() { $id = $this->getKeyId(); $model = Tag::findOne($id); check_record_exists($model); if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){ $post = Yii::$app->request->post(); $model->recommend = $post['Tag']['recommend']?$post['Tag']['recommend']:0; $model->disabled = $post['Tag']['disabled']?$post['Tag']['disabled']:0; if(!$model->validate()) { $msgdata = ['error' => 1,'msg' => $model->returnFirstError()]; } else { if(empty($model->pinyin)) { $pinyin = new PinYin(); $model->pinyin = $pinyin->encode($model->tag); } if($model->save()) { $msgdata = ['error' => 0,'msg' => '操作成功']; } else { $msgdata = ['error' => 1,'msg' => '操作失败']; } } echo_json($msgdata); } return $this->renderAjax('edit',array('model'=>$model)); } //删除 public function actionDel() { $id = $this->getKeyId(); $model = Tag::findOne($id); check_record_exists($model); if($model->delete()) { $msgdata = ['error' => 0,'msg' => '操作成功!']; } else { $msgdata = ['error' => 1,'msg' => '操作失败!']; } echo_json($msgdata); } }