_tableOptions(); $columns = $options['columns']; $operator = $options['operator']; $tpl = 'list'; return $this->render($tpl, [ 'columns' => json_encode($columns,JSON_UNESCAPED_UNICODE), 'operator' => json_encode($operator,JSON_UNESCAPED_UNICODE) ]); } private function _tableOptions(){ $columns = []; $columns['id'] = [ 'field'=>'id', 'title'=>'ID', 'footerFormatter'=>'CountTotalFooter', 'searchable'=>false ]; $columns['title'] = [ 'field'=>'title', 'title'=>'标题', 'class'=>"resize_able", 'editable'=>'textEditor', 'formatter'=>'textFormatter', 'searchFormatter'=>true, 'searchHighlightFormatter'=>'customSearchFmt', 'filterControl'=>"input", 'searchable'=>true ]; $columns['content'] = [ 'field'=>'content', 'title'=>'内容', 'editable'=>'textEditor', 'formatter'=>'textFormatter', 'searchFormatter'=>true, 'searchHighlightFormatter'=>'customSearchFmt', 'searchable'=>true ]; $columns['isShow'] = [ 'field'=>'is_show', 'title'=>'显示', 'editable'=>'textEditor', 'formatter'=>'yesnoFormatter', 'filterControl'=>"select", 'sortable'=>true, 'searchable'=>false ]; $columns['create_at'] = [ 'field'=>'create_at', 'title'=>'创建日期', 'formatter'=>'dateFormatter', 'sortable'=>true, 'searchable'=>false ]; $columns['update_at'] = [ 'field'=>'update_at', 'title'=>'更新时间', 'formatter'=>'timeFormatter', 'sortable'=>true, 'searchable'=>false ]; $operator = []; $operator['showDetail'] = [ 'class'=>'view', 'title'=>'概览', 'icon'=>'eye', 'func'=>false ]; $operator['save'] = [ 'class'=>'save', 'title'=>'保存', 'icon'=>'save', 'func'=>false ]; $operator['remove'] = [ 'class'=>'remove', 'title'=>'移除', 'icon'=>'trash', 'func'=>false ]; return [ 'columns' => $columns, 'operator' => $operator ]; } public function actionSelect() { $pid = $this->pid; $id = Yii::$app->request->get('id'); $callback = !empty(Yii::$app->request->get('callback')) ? Yii::$app->request->get('callback') : 'news'; if($id) $callback .= '-'.$id; $options = $this->_tableOptions(); $columns = $options['columns']; $operator = $options['operator']; //不使用模板框架 // $this->layout = '@app/views/layouts/table'; $tpl = 'select'; return $this->render($tpl, [ 'id' => $id, 'callback'=>$callback, 'columns' => json_encode($columns,JSON_UNESCAPED_UNICODE), 'operator' => json_encode($operator,JSON_UNESCAPED_UNICODE) ]); } public function actionNew() { $pid = $this->pid; $tpl = 'modify'; return $this->render( $tpl, [ 'detail'=>[], 'id'=>0 ] ); } public function actionEdit() { $pid = $this->pid; $post = $this->postdata; $id = Yii::$app->request->get('id'); $AcNotice = new AcNotice(); $notice = $AcNotice->findOne($id); $detail = $notice->toArray(); $tpl = 'modify'; return $this->render( $tpl, [ 'detail'=>$detail, 'id'=>$id ] ); } public function actionSave(){ $post = $this->postdata; $id = isset($post['id']) ? (int)$post['id'] : 0; $pid = Yii::$app->session->get('pid'); //格式化数据 //目标键=》POST键 $cols = ['title'=>'title','content'=>'content']; foreach($cols as $col=>$key){ if(isset($post[$key])){ $data[$col] = trim($post[$key]); }elseif(!$id){ $data[$col] = ''; } } $cols = ['deleted'=>'deleted']; foreach($cols as $col=>$key){ if(isset($post[$key])){ $data[$col] = (int)$post[$key]; }elseif(!$id){ $data[$col] = 0; } } $cols = ['is_show'=>'is_show']; foreach($cols as $col=>$key){ if(!$id){ $data[$col] = isset($post[$key]) ? (int)$post[$key] : 1; }else{ if(isset($post[$col])){ $data[$col] = (int)$post[$key]; } } } if(!$id){ $data['create_at'] = time(); } $data['update_at'] = time(); $data['pid'] = $pid; //保存 $msg = '数据保存失败'; $res = false; $cpde = 0; $model = new AcNotice(); if($id){ $_rec = $model::findOne($id); if($_rec){ foreach($data as $key=>$val){ $_rec->$key = $val; } $res = $_rec->save(); if($res){ $msg = '数据保存成功'; $code = 200; } } }else{ foreach($data as $key=>$val){ $model->$key = $val; } $res = $model->save(); if($res){ $msg = '数据新增成功'; $id = $model->attributes['id']; //获取插入后id $code = 200; } } $data['id'] = $id; $op = Yii::$app->request->get('op'); if($op){ return $this->redirect(['edit','id'=>$id]); } $this->result($msg,$data,200); } public function actionAjax(){ $op = $_GET['op']; //操作标识,只允许客户端GET传入 $post = $this->postdata; $res = []; $msg = '删除失败'; if(!isset($post['id'])){ $this->result($msg,$post,200); } $id = $post['id']; $model = new AcNotice(); $data = $model->findOne($id); if(!$data){ $msg = '数据不存在,操作失败'; $this->result($msg,$post,200); } if($data->pid != $this->pid){ $msg = '归属平台不匹配,禁止操作'; $this->result($msg,$post,200); } switch($op){ case 'del': if($data->deleted >0){ $msg = '数据已被删除过,操作无效'; $this->result($msg,$post,200); } $data->deleted += 1; $_res = $data->save(); $msg = $_res ? '删除成功' : '操作失败,数据更新没有成功!'; $res = $data; break; default: break; } $this->result($msg,$res,200); } public function actionJson() { $res = $this->search(); $list = $res['list']; $ids = $res['ids']; $newList = []; foreach($ids as $id){ $row = $list[$id]; $newList[] = $row; } $data = []; $data['total'] = $res['total']; $data['totalNotFiltered'] = $res['total']; $data['rows'] = $newList; header('Content-Type:application/json'); exit(json_encode($data,JSON_UNESCAPED_UNICODE)); //注意不要加强制object参数 } private function search(){ $pid = $this->pid; $session = Yii::$app->session; $model = new AcNotice(); $where = []; $where[]='and'; $where[] = ['=','pid',$pid]; $post = $this->postdata; $get = Yii::$app->request->get(); $searchSession = 'notice::search'; if (isset($_GET['reset']) && $_GET['reset']==1) { $post['search'] = []; } if (isset($post['search'])) { $search = $post['search']; $session->set($searchSession, $search); } elseif ($session->get($searchSession)) { $search = $session->get($searchSession); } $search['title'] = isset($search['title']) ? trim($search['title']) : ''; if (!empty($search['title'])) { $where[] = ['LIKE','title',$search['title']]; } $deleted = 0; if (isset($_GET['deleted'])) { $deleted = isset($_GET['deleted']) ? ((int)$_GET['deleted']>=0 ? 1 : 0) : 0; } $where[]=['=','deleted',$deleted]; $data = $model->find()->where($where); $totalCount = $data->count(); $_orderby = 'id ASC'; $res = $data->orderby($_orderby)->all(); $list = $ids = []; if ($res) { foreach ($res as $r) { $ids[] = $r->id; $list[$r->id] = $r->toArray(); //内容按ID列出,以便后用 } } $data = []; $data['total'] = $totalCount; $data['totalNotFiltered'] = $totalCount; $data['list'] = $list; $data['ids'] = $ids; return $data; } }