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 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试