【问题标题】:Grouping results in Select2在 Select2 中对结果进行分组
【发布时间】:2013-07-10 18:36:35
【问题描述】:

当 Select2 组件不使用<select> 标记,而是使用<input type="hidden"> 并且结果在配置对象中作为“数据”选项提供时,是否有可能以某种方式对结果进行分组? p>

var select2Options = {
  data: {
    results: myArrayOfResults
  }
};

【问题讨论】:

    标签: jquery-select2


    【解决方案1】:

    是的,results 对象支持children 属性...

    例如:

    var select2Options = {
      data: {
        results: [
          {text: "My shiny group", children: [
              {id: 1, text: "My shiny item"}, 
              {id: 2, text: "My shiny item2"}
          ]}
        ]
      }
    };
    

    【讨论】:

    • 嗯,好久没用了……不知道现在的Select2能不能用。
    • 现在为我工作,但标题必须通过文本更改。无论如何,谢谢,经过几个小时的四舍五入,我得到了这些东西。我相信你在这里的帖子对我有帮助!
    • 为了使其保持最新状态,也许您可​​以编辑答案,只需添加文本而不是 TITLE 的可能性 :)
    • 我明白了,你说得对,我已经简化了,谢谢。
    • 仅供参考:孩子可以堆叠,这样您就可以构建树状层次结构。
    【解决方案2】:

    对于我使用的组和数据工作的 ajax 数据加载,

    $arrFinal = array(array("name"=>"My shiny group 1",
    "children"=>array(array("id"=>1,"name"=>"My shiny item 11"),array("id"=>2,"name"=>"My shiny item 12"))
    ),array("name"=>"My shiny group 2",
    "children"=>array(array("id"=>1,"name"=>"My shiny item 21"),array("id"=>2,"name"=>"My shiny item 22"))
    )
    );
    die(json_encode(array("result" => $arrFinal)));
    
    if formatResult: ratioFormatResult then,
    function ratioFormatResult(row) {
    // Here, you will get both group ("My shiny group 1") as well as data("My shiny item11") as row .
    }
    

    要使组可选择使用 id 字段以及组中的名称。

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2015-12-08
      • 2012-10-06
      • 1970-01-01
      • 2011-03-12
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      相关资源
      最近更新 更多