填充国家/地区下拉菜单 [英] Populate country dropdown

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

问题描述

我有一个数据库表与列ID,国家。

I have a database table with columns ID , Country.

内部控制器从模型中获得国家:

Inside controller i am getting the countries from model:

    $countries = $countries->find('list',array('fields',array('id','country')));
    $this->set('countries',$countries);

在ctp文件中,我想通过在选项值中设置id来获取下拉列表在文本国家名称。

In the ctp file i am trying to get the a the dropdown by setting in option value the id and in the text the country name.

   <?= $this->Form->input('Country of residence',array('type'=>'select','options'=>$countries)); ?>

下拉列表中填充了id和文本id。我怎么能得到'国家'作为文本,而不是id?

The dropdown is populated by have value the id and as text the id again. How can i get the 'country' as text instead of id ?

推荐答案

'list' ,则无法提供只是用于键/值对。您应该指定 keyField valueField

$countries = $countries->find('list', ['keyField' => 'id', 'valueField' => 'country']);

或者,您可以在CountriesTable文件中设置 displayField()为您设置默认的 valueField 。默认 keyField 将是您的表的主键。如果烘焙模型用于国家/地区表格,系统会自动设置这些选项。然后,只要

Alternatively, you can set the displayField() option in your CountriesTable file to have a default valueField set for you. Default keyField would be the primary key of your table. These options would be set automatically if you bake model for the countries table. And then, you are good to go with just

$countries = $countries->find('list');

并且,在表单的字段名称中不要使用空格。通过此 doc 一次

And, do not use space in the field name of your form. Go through this doc once

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

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