How to set selection in mulitiselect list box in cakephp

孤街醉人 提交于 2019-12-11 04:39:56

问题


I want to set the multi selection to my multi list box.

I have two results in my view page.

$inr1 = 0;
$arr1 = array();
$str_arr = '';
foreach ($result as $rows){
    $inr1 = $rows['Employees']['employee_id'];
    $arr1[$inr1] = $rows['Employees']['first_name'].' '.$rows['Employees']['last_name'];
    $str_arr = $str_arr.$inr1.',';
}
$str_arr = substr($str_arr,0,-1);
//print_r($arr1);

$inr = 0;
$arr = array();
foreach ($options as $option){
    $inr = $option['Employee']['employee_id'];
    $arr[$inr] = $option['Employee']['first_name'].' '.$option['Employee']['last_name'];
}

//print_r($arr);

echo $this->Form->input('emp_id_one', array(    'options' => array( $arr),
        'empty' => '(choose one)',
        'div'=>'formfield',
        'error' => array(   'wrap' => 'div',
                            'class' => 'formerror'
                        ),
        'label' => 'Team Members',
        'type' => 'select', 'multiple' => true,
        'selected' => $arr1,
        'style' => 'width:210px; height:125px;',

));

But the values in $arr1 not selected in the list box.

The $arr1 have the selected values.

The $arr have the options to the list.

The problem is that the selction is not working..There have no selection...

How can i do this?

If there is any problem in my code..?


回答1:


Finally i solved my problem by chaing some code:

Add the one line of code after this $str_arr = substr($str_arr,0,-1);'. That is....

$str_arr = substr($str_arr,0,-1);
    $sel = explode(',',$str_arr);

Then change the name of variable as like this :

echo $this->Form->input('emp_id_one', array(    'options' => array( $arr),
        'empty' => '(choose one)',
        'div'=>'formfield',
        'error' => array(   'wrap' => 'div',
                            'class' => 'formerror'
                        ),
        'label' => 'Team Members',
        'type' => 'select', 'multiple' => true,
        'selected' => $sel,
        'style' => 'width:210px; height:125px;',

    ));

Now the values in $sel is selected in the multi list



来源:https://stackoverflow.com/questions/12576717/how-to-set-selection-in-mulitiselect-list-box-in-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!