代码之家  ›  专栏  ›  技术社区  ›  kicaj

如何在cakephp中从find('threaded')创建select/options标记

  •  0
  • kicaj  · 技术社区  · 15 年前

    如何从cakephp中的find('threaded')数据创建select/option html标记? 函数find()返回如下结果:

    Array
    

    ( [0]=>数组 ( [论坛]=>阵列 ( [ID]=gt;1 [姓名]=>论坛

                )
    
            [children] => Array
                (
                    [0] => Array
                        (
                            [Forum] => Array
                                (
                                    [id] => 3
                                    [name] => Programowanie
                                    [parent_id] => 1
                                )
                        )
    
                    [1] => Array
                        (
                            [Thread] => Array
                                (
                                    [id] => 11
                                    [name] => Nowe forumowisko
                                    [parent_id] => 1
                                )
                        )
                )
        )
    
    [1] => Array
        (
            [Forum] => Array
                (
                    [id] => 4
                    [name] => Nauka
                    [parent_id] => 0
                )
    
            [children] => Array
                (
                )
         )
    )
    

    怎么用?

    4 回复  |  直到 15 年前
        1
  •  4
  •   mark    13 年前

    有一种内置方法,可将类似树的结果生成到选择选项标记的列表中:

    $this->Model->generateTreeList($conditions, null, null, ' - ', $recursive);
    

    而不是使用find(线程)

    也就是说,如果您将树行为附加到它(因为它显然是一个类似树的模型,所以您可能应该拥有它)。

    但是如果你想保持你的find(线程)方法,你需要通过递归方法手动转换它。

        2
  •  2
  •   Community Mohan Dere    8 年前

    谢谢 DSkinner ,信息量很大.. 我将其修改为更通用的:

    /**
     * Returns an indented html select based on children depth
     *
     * @param array $data_array - Array of data passed in from cake's find('threaded') feature
     * @param array $model - the model name
     * @param array $key - the key field on the model
     * @param array $value - the value field on the model
     * @param array $list - Used internally, contains array to be returned
     * @param int $counter - Used Internally, counter for depth
     * @return array
     */
    public function threaded_to_list($data_array, $model=null, $key='id', $value='name', 
    &$list = array(), $counter = 0, $separator='__')
    {
        if ( ! is_array($data_array))
            return array();
    
        foreach ($data_array AS $data)
        {
            $list[$data[$model][$key]] = str_repeat($separator, $counter).$data[$model][$value];
            if ( ! empty($data['children']))
            {
                $this->threaded_to_list($data['children'], $model, $key, $value, $list, $counter + 1);
            }
        }
        return $list;
    }
    
        3
  •  1
  •   DSkinner    15 年前

    这是对我有用的。

    确保更换:

    • {使用下拉列表的值选择{id}
    • {选择带有显示为选项的标签}
    • {MyMyNoM} 你的模特名字
    /**
     * Returns an indented html select based on children depth
     *
     * @param array $data_array - Array of data passed in from cake's find('threaded') feature
     * @param array $list - Used internally, contains array to be returned
     * @param int $counter - Used Internally, counter for depth
     * @return array
     */
    public function drop_down_from_threaded($data_array, &$list = array(), $counter = 0)
    {
        if ( ! is_array($data_array))
            return array();
    
        foreach ($data_array AS $data)
        {
            $list[$data[{SELECT_ID}]] = str_repeat('  ', $counter).$data[{SELECT_LABEL}];
            if ( ! empty($data['children']))
            {
                $this->drop_down_from_threaded($data['children'], $list, $counter + 1);
            }
        }
        return $list;
    }
    
    /**
     * Get the data from the find('threaded') and pass it to our new function
     */
    
    $results = $this->{MODEL_NAME}->find('threaded');
    $results = $this->drop_down_from_threaded($results);
    
    

    这可能不是对每个人都100%有效,对我也有效,但它应该有助于给你一些开始。

        4
  •  0
  •   sibidiba    15 年前

    什么是“孩子”?好像你的树是在一对多的关系中产生的

    不必使用树行为来使用此方法- 但是所有期望的结果必须能够在一个查询中找到 .

    推荐文章