dscs63759 2014-12-12 05:10
浏览 109
已采纳

x-editable for yii:连接下拉列表中的字段

I have a CGridView with an EditableColumn with a dropdown select defined like this:

array(
    'name'  => 'Id_department',
    'header'=> 'Department',
    'value' => '$data->Id_department ? Department::model()->findByPk($data->Id_department)->getConcatened() : "[click to edit]"',
    'class' => 'editable.EditableColumn',
    'editable' => array(
        'type'      => 'select',
        'model'     => new Employee(),
        'url'       => $this->createUrl('employee/update'),
        'source'    => $this->createUrl('department/getDepartments'),
        'params'    => array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken),
    ),
);

with this action in the controller:

public function actionGetDepartments(){
    $models = Department::model->findAll();
    echo CJSON::encode(Editable::source($models, 'Id_department', 'Name'));
    Yii::app()->end();
}

This code builds the dropdown select with the Name attribute as the literal the user see on each option.

The think is that I have to show various attributes, not only one, so I tried to put a function call to a function that concatenates that fields in a single String in the source() function, but it doesn't works:

Editable::source($models, 'Id_department', concatenate())

Is there any way to do this?

  • 写回答

1条回答 默认 最新

  • doulai8128 2014-12-18 06:45
    关注

    I finally found a solution (not the best one, obviously, but useful by the moment).

    Basically, what I'm doing is adding a new attribute called $concatenate_field on the model and putting in it all I need. Then, I can use that field on the function call.

    public function actionGetDepartments(){
        $models = Department::model->findAll();
        foreach($models as $model){
            $model->concatenated_field = $model->getConcatened();
        }
        echo CJSON::encode(Editable::source($models, 'Id_department', 'concatenated_field'));
        Yii::app()->end();
    }
    

    I'm still looking for a better way to do it, but by the moment it works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部