Summary of MVC views making item and category

Source: Internet
Author: User
Tags yii

View Layer INDEX.PHG Code:

<?PHP Useyii\helpers\html; UseYii\grid\gridview; UseYii\widgets\pjax; UseFrontend\models\item;/*@var $this Yii\web\view*//*@var $searchModel Frontend\models\itemsearch*//*@var $dataProvider Yii\data\activedataprovider*/$this->title = ' Items ';$this->params[' breadcrumbs ' [] =$this-title;? ><divclass= "Item-index" > $this->title)?>//echo $this->render (' _search ', [' model ' = ' $searchModel]);?><p> <?= html::a (' Create Item ', [' Create '], [' class ' = ' btn btn-success '])?> </p> <? = GridView::Widget ([' Dataprovider ' =$dataProvider, ' filtermodel ' =$searchModel, ' columns ' = [            [' Class ' = ' yii\grid\checkboxcolumn '],//check box column display            [' Class ' = ' Yii\grid\serialcolumn '],[//According to cate_id in the static method of model query category Cate_name display' attribute ' = ' cate_id ', ' value ' =function($model){                    returnItem::get_type_text ($model-cate_id); },//' filter ' = is a drop-down list filter' Filter ' = Item::get_type (),            ], ' name ', ' Buy_price ', ' Sell_price ',            [                ' attribute ' = ' created_at ', ' Format ' =>[' Date ', ' php:y-m-d h:m:s '],//date format display            ],            [                ' attribute ' = ' updated_at ', ' Format ' =>[' Date ', ' php:y-m-d h:m:s '],            ],            [                ' attribute ' = ' status ', ' value ' =function($model){                    return $model->status = = 1? ' Selling ': ' Discontinued ';//Determine the status data given by the model, for 1 is for sale}, ' filter ' = [' discontinued ', ' Sale '],//dropdown filter box            ],//' Img_url:url ',            [            ' Class ' = ' yii\grid\actioncolumn ',//Operation column            ],        ],    ]); ?> <?php Pjax::End();?></div>

Model Layer Code:

<?phpnamespace frontend\models; UseYii; Usefrontend\models\category;/** * * @property string $id * @property integer $cate _id * @property string $name * @property double $buy _price * @prop Erty Double $sell _price * @property integer $created _at * @property integer $updated _at * @property integer $status * @pro Perty string $img _url*/classItemextends\yii\db\activerecord{ Public Static $category;//Use static variable categoryfunction__construct () {Parent::__construct (); if(Is_null(Self::$category) {//single mode, only one category is generated. Self::$category=category::find ()->select ([' cate_id ', ' cate_name '])All (); }   }    /** * @inheritdoc*/     Public Static functionTableName () {return' Item '; }    /** * @inheritdoc*/     Public functionrules () {return [            [[' cate_id ', ' created_at ', ' updated_at ', ' status '], ' integer ',            [[' Buy_price ', ' sell_price '], ' number ',            [[' Created_at ', ' updated_at '], ' required ',            [[' Name '], ' string ', ' max ' = 100],            [[' Img_url '], ' string ', ' max ' = 255],        ]; }    /** * @inheritdoc*/     Public functionAttributelabels () {return [            ' id ' = ' + ' ' number ', ' cate_id ' = ' + ' category ', ' name ' ' = ' and ' name ', ' buy_price ' = ' purchase price (HKD) ',            ' Sell_price ' = ' Sales price (CNY) ', ' created_at ' = ' creation time ', ' updated_at ' = ' Update Time ', ' Status ' = ' state ', ' img_url ' = ' img URL ',        ]; }    /** * @param unknown $id * @return ambigous <unknown>*/     Public Static functionGet_type_text ($id){        /**array_column (Array,column_key,index_key);        The *php function Array_column (array,column_key,index_key) * replaces the (use) Yii\helpers\arrayhelper::map (); */        $datas=array_column (self::$category, ' Cate_name ', ' cate_id '); return  $datas[$id]; }
Generate drop-down filter box, select which future key value will be returned to the query, the key value is the item's cate_id
Public Static functionGet_type () {$cat= Array_column (self::$category, ' Cate_name ', ' cate_id '); return $cat; }}

The code for the Itemsearch layer does not need to be modified.

Summary of MVC views making item and category

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.