简体   繁体   English

如何在SELECT输入字段中设置默认选项

[英]How to set the default option in SELECT input field

I'm trying to set the default value for a SELECT element but it's not working. 我正在尝试为SELECT元素设置默认值,但是它不起作用。 This is my code: 这是我的代码:

 $enum_options = array('1'=>__('None'), '2'=>__('Call for Interview'),'3'=>__('Rejected'),'4'=>__('Pending for Upcoming Oportunities'));
 echo $record['Record']['status'];
 echo $this->Form->input('status', array('label' => __('Status'),'options'=> $enum_options, 'default' => $record['Record']['status']));

The status field in the DB is type ENUM: DB中的状态字段为ENUM类型:

 `status` enum('None','Call for Interview','Rejected','Pending for Upcoming Oportunities') NOT NULL DEFAULT 'None',

Any help? 有什么帮助吗? Regards and thanks in advance 提前致以谢意

I would never use the view to set default values. 我永远不会使用该视图设置默认值。 this is part of the logic and should be inside the controller action 这是逻辑的一部分,应该在控制器动作中

so in your case 所以你的情况

if (!$this->request->isPost()) {
    $this->request->data['Record']['status'] = 2;
}

etc 等等

Also note that Cake itself doesnt really support ENUM. 还要注意,Cake本身并不真正支持ENUM。 your approach has several downsides and limitations. 您的方法有几个缺点和局限性。 A more suitable and extendable solution is http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/ 一个更合适且可扩展的解决方案是http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/

use "empty". 使用“空”。 this will work perfect. 这将完美工作。

$enum_options = array('1'=>'None', '2'=>'Call for Interview', '3'=>'Rejected', '4'=>'Pending for Upcoming Oportunities');

echo $this->Form->input('status', array('type'=>'select', 'label'=>'Status', 'options'=> $enum_options, 'empty'=>$record['Record']['status']));
$enum_options = array('1'=>__('None'), '2'=>__('Call for Interview'),'3'=>__('Rejected'),'4'=>__('Pending for Upcoming Oportunities'));
if($record['Record']['status'] == 'Call for Interview'){
 echo $this->Form->input('status', array('label' => __('Status'),'options'=> $enum_options, 'selected' => '2'));
}else if($record['Record']['status'] == 'Rejected'){
 echo $this->Form->input('status', array('label' => __('Status'),'options'=> $enum_options, 'selected' => '3'));
}else if($record['Record']['status'] == 'Pending for Upcoming Oportunities'){
 echo $this->Form->input('status', array('label' => __('Status'),'options'=> $enum_options, 'selected' => '4'));
}

Simple 2 line tricks.. Surely it will work 简单的2行技巧。.当然可以

$enum_options = array('1'=>__('None'), '2'=>__('Call for Interview'),'3'=>__('Rejected'),'4'=>__('Pending for Upcoming Oportunities'));

 echo $this->Form->input('status', array('label' => __('Status'),'options'=> $enum_options, 'selected' => !empty($record['Record']['status'])?$enum_options[$record['Record']['status']]:1));

Be sure to check the name of your form. 确保检查您的表格名称。 It will determine what the record should be named in order to get it to default. 它将确定记录应命名的名称,以使其成为默认记录。 To have the status default to the record you are pulling from the database, you would do something like this: 要使状态默认为您要从数据库中提取的记录,可以执行以下操作:

<?php
   echo $this->Form->create('MODELNAME');
   echo $this->Form->input('status', array('options'=> $enum_options));
?>

Then in the controller, make sure your record is set correctly: 然后在控制器中,确保记录设置正确:

$this->request->data['MODELNAME']['status'] = #;

Notice how both the record and the Form->create MODELNAME match. 注意记录和Form-> create MODELNAME如何匹配。 This is a must in order for the default value to automatically be selected. 为了自动选择默认值,这是必须的。 Then all you need to do is set # to the number value of the option you want set as the default. 然后,您所需要做的就是将#设置为您要设置为默认选项的数字值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM