douhao123457
2017-02-24 17:10
浏览 55
已采纳

Yii2 - 在listView中使用URL:s的数组

I have an array, Vehicles, "id" and "vehicle", like this database table:

Vehicles
1.Car
2.Truck
3.Bicycle
4.Motorcycle

I make a model of the table, and CRUD it, I am given this generated code in vehicle/index:

<?= ListView::widget([
    'dataProvider' => $dataProvider,
    'itemOptions'  => ['class' => 'item'],
    'itemView'     => function ($model, $key, $index, $widget) {
        return Html::a(Html::encode($model->vehicle), ['view', 'id' => $model->id]);
    },
]) ?>

I see now a list with all my vehicles as links:

Car,
Truck,
Bicycle,
Motorcycle

What I want, is the links to get me somewhere, like this:

Car     - vehicles/car,
Truck   - vehicles/truck
...etc

I will have to hard-code the paths, I guess. Is there a convenient way to do this, in the Html::a function given above? Or should I use another type of function?

图片转代码服务由CSDN问答提供 功能建议

我有一个数组,车辆,“id”和“车辆”,就像这个数据库表一样:

  Vehicles 
1.Car 
2.Truck 
3.Bicycle 
4.Motorcycle 
   
 
 

我制作的模型 表,和CRUD它,我在 vehicle / index 中给出了这个生成的代码:

 &lt;?= ListView :: widget([
  'dataProvider'=&gt; $ dataProvider,
'itemOptions'=&gt; ['class'=&gt;'item'],
'itemView'=&gt;函数($ model,$ key,$ index,$ widget  ){
返回Html :: a(Html :: encode($ model-&gt; vehicle),['view','id'=&gt; $ model-&gt; id]); 
},
]  )?&gt; 
   
 
 

我现在看到一个包含所有车辆链接的列表:

  Car,\  nTruck,
Bicycle,
Motorcycle 
   
 
 

我想要的是将我带到某处的链接,如下所示:

  汽车 - 车辆/汽车,
Truck  - 车辆/卡车
等等
   
 
 

我猜我必须对路径进行硬编码 。 有一个方便的w 要做到这一点,在上面给出的Html :: a函数中? 我应该使用其他类型的函数吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donglu3184 2017-02-24 19:12
    已采纳

    You can use the urlManager

    return Html::a(Html::encode($model->vehicle),
           ['/your-controller/your-action', 'id' => $model->id]);
    

    assuming your controller is name vehicle and in $model->name you have car, or Trukck ...

         return Html::a(Html::encode($model->vehicle),
           ['/vehicle/'. $model->name, 'id' => $model->id]);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题