CakePHP表单下拉菜单 [英] CakePHP Form Dropdown

查看:308
本文介绍了CakePHP表单下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有这个表格输出:

 数组(
=> array(
'Price'=> array(
'id'=>'1',
'amount'=>'20',
' price'=>'180.00',
'type_id'=>'1',
'active'=>'a'

),
(int)1 => array(
'Price'=> array(
'id'=>'2',
'amount'=>'30'
'price'=>'232.50',
'type_id'=>'1',
'active'=>'a'

),

...等等。



我需要一个下拉式窗体,显示金额和价格(例如20 @ 180.00),但选择后,会显示id字段。



我修改了一个名为$ prices的新数组,因此输出结果如下:

  (int)0 => array(
'id'=>'1',
'amount'=>'20',
'price'=& ,
'type_id'=> '1',
'active'=> 'a',
'display'=> '20 @ 180.00'
),
(int)1 => array(
'id'=>'2',
'amount'=>'30',
'price'=>'232.50',
'type_id '=>'1',
'active'=>'a',
'display'=> '30 @ 232.50'
pre>

但是,我不确定该数组是否必要。



但是主要的问题是

  echo $ this-我不知道如何在表单选项中选择显示 > Form-> input('Project.quantity',array(
'options'=> $ prices ['display']
));

只需添加

 'options'=> $ prices 

在下拉列表中显示大量内容 http:// f .cl.ly / items / 1e0X0m0D1f1c2o3K1n3h /屏幕%20Shot%202013-05-08%20at%201.13.48%20 PM.png )。



有更好的方法这样做吗?

解决方案

您可以使用虚拟栏位



在您的模式中:

  public $ virtualFields = array b $ b'display'=>'CONCAT(amount,@,price)'
);

在控制器中:

  $ prices = $ this-> Price-> find('list',array(
'fields'=> array('id','display')
));


I've got this table in my database that outputs this:

array(
(int) 0 => array(
    'Price' => array(
        'id' => '1',
        'amount' => '20',
        'price' => '180.00',
        'type_id' => '1',
        'active' => 'a'
    )
),
(int) 1 => array(
    'Price' => array(
        'id' => '2',
        'amount' => '30',
        'price' => '232.50',
        'type_id' => '1',
        'active' => 'a'
    )
), 

...And so on.

I need a drop down in my form that displays the amount and price together (ie. "20 @ 180.00"), but when selected, gets the "id" field.

I reworked a new array called $prices so it outputs like so...

array(
(int) 0 => array(
    'id' => '1',
    'amount' => '20',
    'price' => '180.00',
    'type_id' => '1',
    'active' => 'a',
    'display' => '20 @ 180.00'
),
(int) 1 => array(
    'id' => '2',
    'amount' => '30',
    'price' => '232.50',
    'type_id' => '1',
    'active' => 'a',
    'display' => '30 @ 232.50'

However, I'm not sure if that array is necessary.

But the main problem is that I don't know what to put in the Form options to make it select the "display" field.

echo $this->Form->input('Project.quantity', array(
  'options' => $prices[?????]['display']
));

Simply adding the

'options' => $prices

displays a lot of stuff in the drop down (http://f.cl.ly/items/1e0X0m0D1f1c2o3K1n3h/Screen%20Shot%202013-05-08%20at%201.13.48%20PM.png).

Is there a better way of doing this?

解决方案

You can use virtual fields.

In your model:

public $virtualFields = array(
  'display' => 'CONCAT(amount, " @ ", price)'
);

In the controller:

$prices = $this->Price->find('list', array(
  'fields' => array('id', 'display')
));

这篇关于CakePHP表单下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆