将结果分组为Select2

时间:2013-07-12 09:00:47

标签: jquery-select2

如果Select2组件没有使用<select>标记但是<input type="hidden">,并且结果在配置中作为“数据”选项提供,那么它是否有可能对结果进行分组对象

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

2 个答案:

答案 0 :(得分:36)

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

所以例如:

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

答案 1 :(得分:0)

对于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字段以及组中的名称。