request->isAjax) { $data = []; $params = Yii::$app->request->get('SearchRecord',[]); $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,'content_model_id'=>$result->content_model_id?ContentModel::modelOptions($result->content_model_id):'','keyword'=>$result->keyword,'result_num'=>$result->result_num,'times'=>$result->times,'recommend'=>$result->recommend,'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'=>'content_model_id','title'=>SearchRecord::getAttributeName('content_model_id'),'align'=>'center','class'=>'col-md-2'), array('field'=>'keyword','title'=>SearchRecord::getAttributeName('keyword'),'align'=>'center','class'=>'col-md-2'), array('field'=>'result_num','title'=>SearchRecord::getAttributeName('result_num'),'align'=>'center','class'=>'col-md-1'), array('field'=>'times','title'=>SearchRecord::getAttributeName('times'),'align'=>'center','sortable'=>true,'formatter'=>'editFormatter','class'=>'col-md-1'), array('field'=>'recommend','title'=>SearchRecord::getAttributeName('recommend'),'align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-1'), array('field'=>'create_time','title'=>SearchRecord::getAttributeName('create_time'),'align'=>'center','sortable'=>true,'class'=>'col-md-2'), array('field'=>'operate','title'=>SearchRecord::getAttributeName('operate'),'align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter','class'=>'col-md-2'), ); $this->tableConfig = array('table'=>SearchRecord::shortTableName(),'url'=>$this->createRealUrl(['admin/searchwords/list']),'setFieldUrl'=>$this->createRealUrl(['admin/searchwords/setfield']),'idField'=>SearchRecord::modelPrimaryKey(),'checkbox'=>1,'dropmenu'=>1,'pagination'=>true,'pagesize'=>20,'refresh'=>true); return $this->render('list',array('model'=>new SearchRecord())); } //添加 function actionAdd() { $model = new SearchRecord(); if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){ $exist = SearchRecord::find()->where("keyword='".$model->keyword."' and content_model_id='".$model->content_model_id."'")->exists(); if($exist) { $msgdata = ['error' => 1,'msg' => '已存在相同搜索词,添加失败']; echo_json($msgdata); } $model->create_time = TIMESTAMP; $model->content_model_id = intval($model->content_model_id); if(!$model->validate()) { $msgdata = ['error' => 1,'msg' => $model->returnFirstError()]; } else { 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)) { SearchRecord::deleteAll("id in(".$ids.")"); $msgdata = ['error' => 0,'msg' => '操作成功']; } else { $msgdata = ['error' => 1,'msg' => '请选择操作记录']; } echo_json($msgdata); } //删除 public function actionDel() { $id = $this->getKeyId(); $model = SearchRecord::findOne($id); check_record_exists($model); if($model->delete()) { $msgdata = ['error' => 0,'msg' => '操作成功!']; } else { $msgdata = ['error' => 1,'msg' => '操作失败!']; } echo_json($msgdata); } }