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?

  • 写回答

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]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题