Browse Source

gridview 第一部分完成

wechat_public_accounts
blobt 5 years ago
parent
commit
96f1e074a5
  1. 53
      common/models/Category.php
  2. 74
      common/models/CategorySearch.php
  3. 13
      console/controllers/InitController.php
  4. 32
      console/migrations/m190802_072830_add_category.php
  5. 11
      console/migrations/sql/add_category.sql
  6. 3
      kcadmin/assets/AppAsset.php
  7. 127
      kcadmin/controllers/CategoryController.php
  8. 35
      kcadmin/views/category/_form.php
  9. 41
      kcadmin/views/category/_search.php
  10. 20
      kcadmin/views/category/create.php
  11. 30
      kcadmin/views/category/index.php
  12. 21
      kcadmin/views/category/update.php
  13. 43
      kcadmin/views/category/view.php
  14. 6
      kcadmin/views/layouts/sidebar.php
  15. 37
      kcadmin/web/css/site.css

53
common/models/Category.php

@ -0,0 +1,53 @@
<?php
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
/**
* This is the model class for table "category".
*
* @property int $id
* @property string $cat_name
* @property string $icon
* @property int $icon_type
* @property string $description
* @property int $sort_order
* @property int $created_at
* @property int $updated_at
*/
class Category extends \yii\db\ActiveRecord {
/**
* {@inheritdoc}
*/
public static function tableName() {
return 'category';
}
/**
* {@inheritdoc}
*/
public function attributeLabels() {
return [
'id' => '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()
];
}
}

74
common/models/CategorySearch.php

@ -0,0 +1,74 @@
<?php
namespace common\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Category;
/**
* CategorySearch represents the model behind the search form of `\common\models\Category`.
*/
class CategorySearch extends Category
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'icon_type', 'sort_order', 'created_at', 'updated_at'], 'integer'],
[['cat_name', 'icon', 'description'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Category::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $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;
}
}

13
console/controllers/InitController.php

@ -8,7 +8,7 @@ use yii\helpers\Console;
use yii\db\Query; use yii\db\Query;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use common\models\User; use common\models\User;
//use common\models\ars\AdminUser;
use common\models\Category;
/** /**
* Description of RbacsetController * Description of RbacsetController
@ -19,6 +19,17 @@ class InitController extends Controller {
public function actionIndex() { public function actionIndex() {
echo "index\n"; 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) { public function actionCreateAdmin($password = NULL) {

32
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
*/ */
class m190802_072830_add_category extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
}
class m190802_072830_add_category extends Migration {
/** /**
* {@inheritdoc} * {@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;
}
*/
} }

11
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;

3
kcadmin/assets/AppAsset.php

@ -20,6 +20,7 @@ class AppAsset extends AssetBundle
'yii\web\YiiAsset', 'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset', 'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset', 'yii\bootstrap\BootstrapPluginAsset',
'blobt\web\AdminlteAsset'
'blobt\web\AdminlteAsset',
'blobt\web\DatatableBootstrap'
]; ];
} }

127
kcadmin/controllers/CategoryController.php

@ -0,0 +1,127 @@
<?php
namespace kcadmin\controllers;
use Yii;
use common\models\Category;
use common\models\CategorySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* CategoryController implements the CRUD actions for Category model.
*/
class CategoryController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'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.');
}
}

35
kcadmin/views/category/_form.php

@ -0,0 +1,35 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model common\models\Category */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="category-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'cat_name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'icon')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'icon_type')->textInput() ?>
<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'sort_order')->textInput() ?>
<?= $form->field($model, 'created_at')->textInput() ?>
<?= $form->field($model, 'updated_at')->textInput() ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

41
kcadmin/views/category/_search.php

@ -0,0 +1,41 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model common\models\CategorySearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="category-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'cat_name') ?>
<?= $form->field($model, 'icon') ?>
<?= $form->field($model, 'icon_type') ?>
<?= $form->field($model, 'description') ?>
<?php // echo $form->field($model, 'sort_order') ?>
<?php // echo $form->field($model, 'created_at') ?>
<?php // echo $form->field($model, 'updated_at') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-outline-secondary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

20
kcadmin/views/category/create.php

@ -0,0 +1,20 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\models\Category */
$this->title = 'Create Category';
$this->params['breadcrumbs'][] = ['label' => 'Categories', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="category-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

30
kcadmin/views/category/index.php

@ -0,0 +1,30 @@
<?php
use yii\helpers\Html;
use blobt\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel common\models\CategorySearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Categories';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="row">
<div class="col-xs-12">
<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
'cat_name',
'icon',
'icon_type',
'description:ntext',
['class' => 'yii\grid\ActionColumn'],
],
]);
?>
</div>
</div>

21
kcadmin/views/category/update.php

@ -0,0 +1,21 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\models\Category */
$this->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';
?>
<div class="category-update">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

43
kcadmin/views/category/view.php

@ -0,0 +1,43 @@
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model common\models\Category */
$this->title = $model->id;
$this->params['breadcrumbs'][] = ['label' => 'Categories', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="category-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'cat_name',
'icon',
'icon_type',
'description:ntext',
'sort_order',
'created_at',
'updated_at',
],
]) ?>
</div>

6
kcadmin/views/layouts/sidebar.php

@ -14,9 +14,9 @@ use blobt\widgets\Menu;
['label' => 'Test', 'url' => ['site/test']], ['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']],
] ]
] ]
] ]

37
kcadmin/web/css/site.css

@ -31,3 +31,40 @@
.login-form h6 { .login-form h6 {
text-align: center; 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;
}
Loading…
Cancel
Save