如何在cakephp中的数组中创建具有相同的值和标签的select? [英] how do you create a select with both value and the label the same from an array in cakephp?

查看:92
本文介绍了如何在cakephp中的数组中创建具有相同的值和标签的select?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从表中的控制器中创建了一个数组:

I created an array in the controller from a table :

$tasktemplateResults = $this->Tasktemplate->find('list');

在视图中,我有一个表单帮助器,用于创建下拉列表:

And in the view I have a form helper that creates a drop down list:

echo $this->Form->input('Tasktemplate.id', array('options' => array($tasktemplateResults),'label' => 'Task'));

html输出如下:

<div class="input select">
    <label for="TasktemplateId">Task</label>
    <select name="data[Tasktemplate][id]" id="TasktemplateId"> 
        <option value="1">Test task</option> 
        <option value="2">Second test task</option> 
    </select>
</div>

我想要以下html输出:

I would like to have this html output:

<div class="input select">
    <label for="TasktemplateId">Task</label>
    <select name="data[Tasktemplate][id]" id="TasktemplateId"> 
        <option value="Test task">Test task</option> 
        <option value="Second test task">Second test task</option> 
    </select>
</div>

如何获得与上面显示的数组相同的名称和值对?

How do I get the name and value pairs the same as the array as shown above?

推荐答案

您可以将参数添加到find'list'方法中,以指定要用于键和值的字段。

You can add parameters to the find 'list' method to specify which fields to use for the key and the value.

示例:

    $usernameMap = $this->Article->User->
find('list', array('fields' => array('User.username', 'User.first_name')));

http://book.cakephp.org/view/1022/find-list

这篇关于如何在cakephp中的数组中创建具有相同的值和标签的select?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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