Yii 1-listData()和dropdownList()

时间:2018-10-15 15:16:35

标签: drop-down-menu yii yii1.x

我有此代码:

$vehicleList = CHtml::listData(Vehicle::model()->findAllByAttributes(
        array(
            'user_id'=>$company->user_id,
            'status'=>1,'archived'=>0, 'deleted'=>0
        )
    ),
    'id', 'vehicle_model'
);

那个代码:

<?php echo CHtml::activeDropDownList(
    $jobEvent,
    'vehicle_id',
    $vehicleList,
    array(
        'empty'=>'',
        'options'=>$vehicleListOptions,
    ));
?>

生成如下输出:

<option value="45" class="option_45">Alfa 159 </option>

Vehicle::model()->findAllByAttributes() 我想获取其他属性,例如License PlateVehicle Description 进行这样的输出:

<option value="45" class="option_45">Alfa 159 (License Plate ) <br> Vehicle Description </option>

有什么主意吗?

2 个答案:

答案 0 :(得分:0)

您应该使用Closure生成这样的描述:

$vehicles = Vehicle::model()->findAllByAttributes([
    'user_id' => $company->user_id,
    'status' => 1, 'archived' => 0, 'deleted' => 0,
]);
$vehicleList = CHtml::listData($vehicles, 'id', function ($model) {
    /* @var $model Vehicle */
    return "{$model->vehicle_model} ({$model->license_plate}) {$model->vehicle_description}";
});

您可以在documentation of this method中找到更多信息。

答案 1 :(得分:0)

实际上我已经以他的方式解决了我的要求

 $vehicleList = CHtml::listData(Vehicle::model()->findAllByAttributes(
                    array(
                        'user_id'=>$company->user_id,
                        'status'=>1,'archived'=>0, 'deleted'=>0
                    ),
                    array("select"=>"id,concat(vehicle_model ,' ( ', license_plate ,' ) - ', vehicle_description) as vehicle_model")
                ),
                    'id', 'vehicle_model'
                );

通过添加新的数组行

array(“ select” =>“ id,concat(vehicle_model,'(',license_plate,')-',vehicle_description)as vehicle_model”)),

我正在尝试在(license_plate)之后添加“ \ n”,varchar(13)来添加新行。.但是它不起作用。.