0

我从一个模型中创建了 2 个数组,其中 1 个是已经从用户那里选择的值,而第二个是用户可以选择的可用值。这是一个编辑页面。我想用两个模型的值填充一个多选输入框,但希望突出显示已经选择的值(第一个数组)。它可以很好地创建模型,并使用 array_merge() 我将两个数组合并为选项,但是所选的并没有突出显示正确的字段。有小费吗?

 // Controller:
 $ivrNumbersAvail = $this->Survey->SurveyIvrNumber->find("list",array("conditions" => array("OR" => array("SurveyIvrNumber.survey_id" => array($id)))));
 $ivrNumbersSelected = $this->Survey->SurveyIvrNumber->find("list",array("conditions" => array("OR" => array("SurveyIvrNumber.survey_id" => array(0)))));

 // In the view:
 echo $this->Form->input('SurveyIvrNumbers.id',array(
                            'empty' => '-- Select IVR Number(s) --',
                            'options' => array_merge($ivrNumbersAvail,$ivrNumbersSelected),
                            'selected' => $ivrNumbersSelected,
            'class' => 'textbox',
                            'multiple' => true,
            'div' => array(
                    'class' => 'field'
            ),
                'label' => array(
                'class' => 'label-tooltip',
                'title' => '', //tool tips
                'text' => 'IVR Numbers: (you can select multiple numbers)'

                ),
                'after' => '<p class="field-help"></p>'
        ));
4

1 回答 1

1

如果您设置$this->request->data为您当前正在编辑的记录,CakePHP 将自动为您填充此数据!

// CONTROLLER
// this line sets the data
$this->request->data = $this->Survey->read(null, $id); 

// this passes the SurveyIvrNumbers to the view, (you can put any options on to this)
$this->set('SurveyIvrNumber',$this->Survey->SurveyIvrNumber->find('all')); 

// VIEW
// CakePHP does the rest
echo $this->Form->input('SurveyIvrNumbers',array( 
    'empty' => '-- Select IVR Number(s) --', // plus other options
);
于 2012-05-25T09:34:39.413 回答