製作item和category的mvc視圖總結

來源:互聯網
上載者:User

標籤:begin   查詢   grid   btn   filter   idv   製作   銷售   adc   

View層index.phg 代碼:

<?phpuse yii\helpers\Html;use yii\grid\GridView;use yii\widgets\Pjax;use frontend\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;?><div class="item-index">    <h1><?= Html::encode($this->title) ?></h1>    <?php Pjax::begin(); ?>    <?php // 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‘],//複選框列顯示            [‘class‘ => ‘yii\grid\SerialColumn‘],            [   //根據cate_id在model的靜態方法裡面查詢category的cate_name顯示                ‘attribute‘ =>‘cate_id‘,                ‘value‘ => function($model){                    return Item::get_type_text($model->cate_id);                },                //‘filter‘ =>是下拉式清單過濾器                ‘filter‘ => Item::get_type(),            ],            ‘name‘,            ‘buy_price‘,            ‘sell_price‘,            [                ‘attribute‘ => ‘created_at‘,                ‘format‘=>[‘date‘,‘php:Y-m-d H:m:s‘],//日期格式顯示            ],            [                ‘attribute‘ => ‘updated_at‘,                ‘format‘=>[‘date‘,‘php:Y-m-d H:m:s‘],            ],            [                ‘attribute‘ => ‘status‘,                ‘value‘ => function($model){                    return $model->status == 1?‘在售‘:‘停售‘;//判斷model給的status資料,為1時候是在售                },                ‘filter‘ => [‘停售‘,‘在售‘],//下拉篩選框            ],            // ‘img_url:url‘,            [            ‘class‘ => ‘yii\grid\ActionColumn‘,//操作列            ],        ],    ]); ?>    <?php Pjax::end(); ?></div>

model層代碼:

<?phpnamespace frontend\models;use Yii;use frontend\models\Category;/** * * @property string $id * @property integer $cate_id * @property string $name * @property double $buy_price * @property double $sell_price * @property integer $created_at * @property integer $updated_at * @property integer $status * @property string $img_url */class Item extends \yii\db\ActiveRecord{    public static $category;//使用了靜態變數category    function __construct() {       parent::__construct();       if(is_null(SELF::$category)){//單一模式,只產生一個category。        SELF::$category=Category::find()->select([‘cate_id‘,‘cate_name‘])->all();       }   }    /**     * @inheritdoc     */    public static function tableName()    {        return ‘item‘;    }    /**     * @inheritdoc     */    public function rules()    {        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 function attributeLabels()    {        return [            ‘id‘ => ‘編號‘,            ‘cate_id‘ => ‘類別‘,            ‘name‘ => ‘品名‘,            ‘buy_price‘ => ‘採購價(HKD)‘,            ‘sell_price‘ => ‘銷售價(CNY)‘,            ‘created_at‘ => ‘建立時間‘,            ‘updated_at‘ => ‘更新時間‘,            ‘status‘ => ‘狀態‘,            ‘img_url‘ => ‘Img Url‘,        ];    }    /**    * 通過欄目id獲得欄目名稱    * @param unknown $id    * @return Ambigous <unknown>    */    public static function  get_type_text($id){        /*        *array_column(array,column_key,index_key);        *php內建函數array_column(array,column_key,index_key)        *替換了(use) Yii\helpers\ArrayHelper::map();        */        $datas =array_column(SELF::$category,‘cate_name‘,‘cate_id‘);        return  $datas[$id];    }
  //產生下拉篩選框,選擇哪個以後key值會返給查詢,key值就是item的cate_id
public static function get_type(){ $cat = array_column(SELF::$category,‘cate_name‘,‘cate_id‘); return $cat; }}

itemSearch 層的代碼不需要修改。

製作item和category的mvc視圖總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.