【问题标题】:HTML Select Separator in CakePHP 1.3CakePHP 1.3 中的 HTML 选择分隔符
【发布时间】:2014-01-14 17:28:36
【问题描述】:

我有一个国家列表,我使用 find('list') 从我的数据库中检索到,我想在第 4 个元素之后添加一个分隔符 - 这是一个国家列表,前四个是最受欢迎的,然后我想要分隔符,然后其余的只是按字母顺序排列。我似乎找不到这样做的好方法。有什么建议吗?

谢谢。

【问题讨论】:

    标签: cakephp-1.3 form-helpers


    【解决方案1】:

    一种选择是使用“optgroup”。如果您的数组有 2 个级别,Cakephp 表单助手将生成它。

    $countries = $this->Country->find('list'); // based on your implementation
    
    $popularCountries = array();
    $otherCountries = array();
    foreach ($countries as $countryId => $countryName) {
        if ($countryId < 5) { // this might also depend on the IDs, or how you determine if a country is popular or not
            $popularCountries[$countryId] = $countryName;
        } else {
            $otherCountries[$countryId] = $countryName;
        }
    }
    $options = array(
        'Popular countries' => $popularCountries,
        'Other' => $otherCountries
    );
    

    然后使用

    echo $this->Form->input('country', array('type' => 'select', 'options' => $options));
    

    您也可以在列表中的第 4 个元素之后插入一个没有值的国家和名称等于 '-------'。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多