From 96f1e074a542699c2d392bd03c81d92a2dad66cb Mon Sep 17 00:00:00 2001 From: blobt <380255922@qq.com> Date: Mon, 5 Aug 2019 21:17:50 +0800 Subject: [PATCH] =?UTF-8?q?gridview=20=E7=AC=AC=E4=B8=80=E9=83=A8=E5=88=86?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/models/Category.php | 53 ++++++++ common/models/CategorySearch.php | 74 ++++++++++ console/controllers/InitController.php | 15 ++- .../m190802_072830_add_category.php | 32 +---- console/migrations/sql/add_category.sql | 11 ++ kcadmin/assets/AppAsset.php | 3 +- kcadmin/controllers/CategoryController.php | 127 ++++++++++++++++++ kcadmin/views/category/_form.php | 35 +++++ kcadmin/views/category/_search.php | 41 ++++++ kcadmin/views/category/create.php | 20 +++ kcadmin/views/category/index.php | 30 +++++ kcadmin/views/category/update.php | 21 +++ kcadmin/views/category/view.php | 43 ++++++ kcadmin/views/layouts/sidebar.php | 6 +- kcadmin/web/css/site.css | 37 +++++ 15 files changed, 517 insertions(+), 31 deletions(-) create mode 100644 common/models/Category.php create mode 100644 common/models/CategorySearch.php create mode 100644 console/migrations/sql/add_category.sql create mode 100644 kcadmin/controllers/CategoryController.php create mode 100644 kcadmin/views/category/_form.php create mode 100644 kcadmin/views/category/_search.php create mode 100644 kcadmin/views/category/create.php create mode 100644 kcadmin/views/category/index.php create mode 100644 kcadmin/views/category/update.php create mode 100644 kcadmin/views/category/view.php diff --git a/common/models/Category.php b/common/models/Category.php new file mode 100644 index 0000000..7b5feff --- /dev/null +++ b/common/models/Category.php @@ -0,0 +1,53 @@ + 'ID', + 'cat_name' => 'Cat Name', + 'icon' => 'Icon', + 'icon_type' => 'Icon Type', + 'description' => 'Description', + 'sort_order' => 'Sort Order', + 'created_at' => 'Created At', + 'updated_at' => 'Updated At', + ]; + } + + public function behaviors() { + return [ + TimestampBehavior::className() + ]; + } + +} diff --git a/common/models/CategorySearch.php b/common/models/CategorySearch.php new file mode 100644 index 0000000..4b2e392 --- /dev/null +++ b/common/models/CategorySearch.php @@ -0,0 +1,74 @@ + $query, + ]); + + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + // grid filtering conditions + $query->andFilterWhere([ + 'id' => $this->id, + 'icon_type' => $this->icon_type, + 'sort_order' => $this->sort_order, + 'created_at' => $this->created_at, + 'updated_at' => $this->updated_at, + ]); + + $query->andFilterWhere(['like', 'cat_name', $this->cat_name]) + ->andFilterWhere(['like', 'icon', $this->icon]) + ->andFilterWhere(['like', 'description', $this->description]); + + return $dataProvider; + } +} diff --git a/console/controllers/InitController.php b/console/controllers/InitController.php index a296398..ec08ad0 100644 --- a/console/controllers/InitController.php +++ b/console/controllers/InitController.php @@ -8,7 +8,7 @@ use yii\helpers\Console; use yii\db\Query; use yii\helpers\ArrayHelper; use common\models\User; -//use common\models\ars\AdminUser; +use common\models\Category; /** * Description of RbacsetController @@ -19,6 +19,17 @@ class InitController extends Controller { public function actionIndex() { echo "index\n"; + Category::deleteAll(); + for ($i = 0; $i < 100; $i++) { + $model = new Category(); + $model->attributes = [ + "cat_name" => "这是一个测试分类{$i}", + "icon" => 'fa', + "icon_type" => 1, + "sort_order" => $i + ]; + $model->save(); + } } public function actionCreateAdmin($password = NULL) { @@ -83,7 +94,7 @@ class InitController extends Controller { if ($model->save()) { //$oRole = $auth->getRole("超级管理员"); //if ($auth->assign($oRole, $model->id)) { - $this->stdout("Create admin success!\n", Console::FG_GREEN); + $this->stdout("Create admin success!\n", Console::FG_GREEN); //} } } diff --git a/console/migrations/m190802_072830_add_category.php b/console/migrations/m190802_072830_add_category.php index a76f38f..05dad5e 100644 --- a/console/migrations/m190802_072830_add_category.php +++ b/console/migrations/m190802_072830_add_category.php @@ -5,38 +5,20 @@ use yii\db\Migration; /** * Class m190802_072830_add_category */ -class m190802_072830_add_category extends Migration -{ - /** - * {@inheritdoc} - */ - public function safeUp() - { - - } +class m190802_072830_add_category extends Migration { /** * {@inheritdoc} */ - public function safeDown() - { - echo "m190802_072830_add_category cannot be reverted.\n"; - - return false; + public function up() { + $sql = file_get_contents(__DIR__ . "/sql/add_category.sql"); + $this->execute($sql); } - /* - // Use up()/down() to run migration code without a transaction. - public function up() - { + public function down() { + $this->dropTable("category"); + return true; } - public function down() - { - echo "m190802_072830_add_category cannot be reverted.\n"; - - return false; - } - */ } diff --git a/console/migrations/sql/add_category.sql b/console/migrations/sql/add_category.sql new file mode 100644 index 0000000..e801049 --- /dev/null +++ b/console/migrations/sql/add_category.sql @@ -0,0 +1,11 @@ +DROP TABLE IF EXISTS `category`; +CREATE TABLE `category` ( + `id` int(11) AUTO_INCREMENT PRIMARY KEY, + `cat_name` varchar(64) NOT NULL, + `icon` varchar(64) DEFAULT NULL, + `icon_type` tinyint(1) NOT NULL DEFAULT 1, + `description` text NOT NULL DEFAULT '', + `sort_order` smallint(3) NOT NULL DEFAULT 100, + `created_at` int(11) NOT NULL DEFAULT 0, + `updated_at` int(11) NOT NULL DEFAULT 0 +)ENGINE=INNODB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; diff --git a/kcadmin/assets/AppAsset.php b/kcadmin/assets/AppAsset.php index 63b8e8d..7c22300 100644 --- a/kcadmin/assets/AppAsset.php +++ b/kcadmin/assets/AppAsset.php @@ -20,6 +20,7 @@ class AppAsset extends AssetBundle 'yii\web\YiiAsset', 'yii\bootstrap\BootstrapAsset', 'yii\bootstrap\BootstrapPluginAsset', - 'blobt\web\AdminlteAsset' + 'blobt\web\AdminlteAsset', + 'blobt\web\DatatableBootstrap' ]; } diff --git a/kcadmin/controllers/CategoryController.php b/kcadmin/controllers/CategoryController.php new file mode 100644 index 0000000..0bbc472 --- /dev/null +++ b/kcadmin/controllers/CategoryController.php @@ -0,0 +1,127 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all Category models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new CategorySearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Category model. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Category model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Category(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + + return $this->render('create', [ + 'model' => $model, + ]); + } + + /** + * Updates an existing Category model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + + return $this->render('update', [ + 'model' => $model, + ]); + } + + /** + * Deletes an existing Category model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the Category model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Category the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Category::findOne($id)) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } +} diff --git a/kcadmin/views/category/_form.php b/kcadmin/views/category/_form.php new file mode 100644 index 0000000..a26664e --- /dev/null +++ b/kcadmin/views/category/_form.php @@ -0,0 +1,35 @@ + + +
+ + + + field($model, 'cat_name')->textInput(['maxlength' => true]) ?> + + field($model, 'icon')->textInput(['maxlength' => true]) ?> + + field($model, 'icon_type')->textInput() ?> + + field($model, 'description')->textarea(['rows' => 6]) ?> + + field($model, 'sort_order')->textInput() ?> + + field($model, 'created_at')->textInput() ?> + + field($model, 'updated_at')->textInput() ?> + +
+ 'btn btn-success']) ?> +
+ + + +
diff --git a/kcadmin/views/category/_search.php b/kcadmin/views/category/_search.php new file mode 100644 index 0000000..77ac48e --- /dev/null +++ b/kcadmin/views/category/_search.php @@ -0,0 +1,41 @@ + + + diff --git a/kcadmin/views/category/create.php b/kcadmin/views/category/create.php new file mode 100644 index 0000000..a727e42 --- /dev/null +++ b/kcadmin/views/category/create.php @@ -0,0 +1,20 @@ +title = 'Create Category'; +$this->params['breadcrumbs'][] = ['label' => 'Categories', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/kcadmin/views/category/index.php b/kcadmin/views/category/index.php new file mode 100644 index 0000000..212d070 --- /dev/null +++ b/kcadmin/views/category/index.php @@ -0,0 +1,30 @@ +title = 'Categories'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+
+ $dataProvider, + 'columns' => [ + 'id', + 'cat_name', + 'icon', + 'icon_type', + 'description:ntext', + ['class' => 'yii\grid\ActionColumn'], + ], + ]); + ?> +
+
\ No newline at end of file diff --git a/kcadmin/views/category/update.php b/kcadmin/views/category/update.php new file mode 100644 index 0000000..09a7500 --- /dev/null +++ b/kcadmin/views/category/update.php @@ -0,0 +1,21 @@ +title = 'Update Category: ' . $model->id; +$this->params['breadcrumbs'][] = ['label' => 'Categories', 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]]; +$this->params['breadcrumbs'][] = 'Update'; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/kcadmin/views/category/view.php b/kcadmin/views/category/view.php new file mode 100644 index 0000000..7832b6b --- /dev/null +++ b/kcadmin/views/category/view.php @@ -0,0 +1,43 @@ +title = $model->id; +$this->params['breadcrumbs'][] = ['label' => 'Categories', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +\yii\web\YiiAsset::register($this); +?> +
+ +

title) ?>

+ +

+ $model->id], ['class' => 'btn btn-primary']) ?> + $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +

+ + $model, + 'attributes' => [ + 'id', + 'cat_name', + 'icon', + 'icon_type', + 'description:ntext', + 'sort_order', + 'created_at', + 'updated_at', + ], + ]) ?> + +
diff --git a/kcadmin/views/layouts/sidebar.php b/kcadmin/views/layouts/sidebar.php index e96425b..a164ca5 100644 --- a/kcadmin/views/layouts/sidebar.php +++ b/kcadmin/views/layouts/sidebar.php @@ -14,9 +14,9 @@ use blobt\widgets\Menu; ['label' => 'Test', 'url' => ['site/test']], ] ], - ['label' => 'GoodsController', 'url' => '#', 'icon' => 'fa-hand-o-right', 'items' => [ - ['label' => 'sasa', 'url' => ['goods/sasa', 'tag' => 'new']], - ['label' => 'dada', 'url' => ['goods/dada', 'tag' => 'popular']], + ['label' => 'Category', 'url' => '#', 'icon' => 'fa-barcode', 'items' => [ + ['label' => 'List', 'url' => ['category/index', 'tag' => 'new']], + ['label' => 'Create', 'url' => ['category/create', 'tag' => 'popular']], ] ] ] diff --git a/kcadmin/web/css/site.css b/kcadmin/web/css/site.css index 96eade8..b6e133e 100644 --- a/kcadmin/web/css/site.css +++ b/kcadmin/web/css/site.css @@ -31,3 +31,40 @@ .login-form h6 { text-align: center; } + + +/*datatable修改*/ +table.dataTable thead > tr > th.sorting_asc, table.dataTable thead > tr > th.sorting_desc, table.dataTable thead > tr > th.sorting, +table.dataTable thead > tr > td.sorting_asc, +table.dataTable thead > tr > td.sorting_desc, +table.dataTable thead > tr > td.sorting { + padding-right: 8px; +} + +table.dataTable thead > tr > th > a{ + position: relative; + display: block; + color:#333333; + font-family: 'Glyphicons Halflings'; +} + +table.dataTable thead > tr > th > a:after{ + position: absolute; + right: 0px; + opacity: 0.2; + content: "\e150"; +} + +table.dataTable thead > tr > th > a.asc:after{ + position: absolute; + right: 0px; + content: "\e155"; + opacity: 0.6; +} + +table.dataTable thead > tr > th > a.desc:after{ + position: absolute; + right: 0px; + content: "\e156"; + opacity: 0.6; +} \ No newline at end of file