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 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?