Posts Tagged with "Design"

既に発行済みのブログであっても適宜修正・追加することがあります。
We may make changes and additions to blogs already published.
posted by sakurai on November 11, 2022 #549

アクションindexのビューの修正

index.php/?r=docsとしてドキュメント一覧表を表示します。表549.1に示すように、index.phpにより実行されます。

図%%.1
図549.1 yii2標準のGridViewによる表示

yii2も標準ではリレーション先まで見てくれないため、手修正を行います。前稿のように、views/docs/index.phpの修正法を示します。自動生成されたindex.phpを見ると、

    <?= GridView::widget([
        
'dataProvider' => $dataProvider,
        
'filterModel' => $searchModel,
        
'columns' => [
            [
'class' => 'yiigridSerialColumn'],
            
'id',
            
'title',
            
'pages',
            
'link',
            
'category_id',

これをyii2のGridViewのcolumnsにおいて、(リレーション名).(メンバ名)でリレーション先を表示できるとあるように修正します。

    <?= GridView::widget([
        
'dataProvider' => $dataProvider,
        
'filterModel' => $searchModel,
        
'columns' => [
            [
'class' => 'yiigridSerialColumn'],
            
'id',
            
'title',
            
'pages',
            
'link',
            
'category.name',

これを実行した結果を図549.2に示します。数字ではなく、カテゴリ名が表示されています。

図%%.2
図549.2 修正したGridViewによる表示

タイトル行がNameですが、Categoryに変更したい場合は、’category.name:text:Category'のように修正します。


左矢前のブログ 次のブログ右矢


ページ: