【问题标题】:How create select/options tag from find('threaded') in CakePHP如何在 CakePHP 中从 find('threaded') 创建选择/选项标签
【发布时间】:2010-03-04 19:02:43
【问题描述】:

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

Array

( [0] => 数组 ( [论坛] => 数组 ( [id] => 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
            (
            )
     )
)

怎么做?

【问题讨论】:

    标签: list cakephp


    【解决方案1】:

    有一个内置方法可以将树状结果制作成选择选项标签的列表:

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

    而不是使用 find(thread)

    如果你将 Tree 行为附加到它上面(你可能应该拥有它,因为它显然是一个树状模型)。

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

    【讨论】:

      【解决方案2】:

      感谢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】:

        这对我有用。

        确保替换:

        • {SELECT_ID} 与下拉列表的值
        • {SELECT_LABEL} 带有作为选项显示的内容
        • {MODEL_NAME} 带有您的型号名称
        /** * 返回一个基于子深度的缩进 html 选择 * * @param array $data_array - 从 cake 的 find('threaded') 功能传入的数据数组 * @param array $list - 内部使用,包含要返回的数组 * @param int $counter - 内部使用,深度计数器 * @return 数组 */ 公共函数 drop_down_from_threaded($data_array, &$list = array(), $counter = 0) { 如果(!is_array($data_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); } } 返回$列表; } /** * 从 find('threaded') 中获取数据并将其传递给我们的新函数 */ $results = $this->{MODEL_NAME}->find('threaded'); $results = $this->drop_down_from_threaded($results);

        这可能不是对每个人都 100% 有效,它适用于我,但它应该可以帮助你开始。

        【讨论】:

          【解决方案4】:

          什么是“孩子”?似乎您的树是通过一对多关系生成的

          不必使用树行为来使用此方法 - 但必须可以在单个查询中找到所有所需的结果

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-11-28
            • 2019-05-28
            相关资源
            最近更新 更多