Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery multiselect - nested <option>

I'm using the following jquery plugin for selecting multiple options in my select control: jquery multiselect

How can I achieve nested <option> here? I know it's possible because the rendered html uses <li> tags

The case is that I want to have the similar result in my combobox:

[ ] England
   [ ] London
   [ ] Leeds
   [ ] Manchaster

Does anyone have an idea how to achieve this kind of solution. Any help would be apprieciate.

like image 596
niao Avatar asked Jan 08 '12 22:01

niao


People also ask

How can I get multiple selected values of select box in jQuery?

With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.

How create a multiple selected checkbox with dropdown in jQuery?

jQuery MultiSelect With Select All Option: The following code adds select all text to options list in multiselect dropdown. It allows to check/uncheck all options at once. $('#langOpt'). multiselect({ columns: 1, placeholder: 'Select Languages', search: true, selectAll: true });


1 Answers

Description

Assuming i understand what you want you can do this using optgroup.

Check out this jsFiddle Demonstration i have created for you.

Sample

<select multiple="multiple" size="5">
<optgroup label="England">
    <option value="London">London</option>
    <option value="Leeds">Leeds</option>
    <option value="option3">Manchaster</option>
</optgroup>
<optgroup label="USA">
    <option value="option4">New York</option>
    <option value="option5">Chicago</option>
</optgroup>
</select>

More Information

  • jsFiddle Demonstration
  • jQuery UI MultiSelect Widget
  • jQuery UI MultiSelect Widget Demos
  • vZHai - jQuery UI MultiSelect Widget

Update

I have created a jsFiddle Demonstration.

Complete working sample

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">

  <script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js"></script>
  <link rel="stylesheet" type="text/css" href="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/jquery.multiselect.css">
  <link rel="stylesheet" type="text/css" href="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/assets/style.css">
  <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/ui-lightness/jquery-ui.css">
  <script type='text/javascript' src="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/src/jquery.multiselect.js"></script>

  <script type='text/javascript'>//<![CDATA[ 
    $(function(){
        $("select").multiselect();
    });  
  </script>
</head>
<body>
  <select multiple="multiple" size="5">
    <optgroup label="England">
        <option value="London">London</option>
        <option value="Leeds">Leeds</option>
        <option value="option3">Manchaster</option>
    </optgroup>
    <optgroup label="USA">
        <option value="option4">New York</option>
        <option value="option5">Chicago</option>
    </optgroup>
  </select>
</body>
</html>

Update after an intensive discussion with niao, who ended with

niao: yes, that's what I need. I will have to add some css to make it looks pretty. You can append your answer and I will be more than happy to accept it

You can see the result in this jSFiddle.

I encourage you to download the resources (javascript and css files) to put that on your enviroment.

Full working sample

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

  <script src="http://wwwendt.de/tech/dynatree/jquery/jquery.js" type="text/javascript"></script>
  <script src="http://wwwendt.de/tech/dynatree/jquery/jquery-ui.custom.js" type="text/javascript"></script>
  <script src="http://wwwendt.de/tech/dynatree/jquery/jquery.cookie.js" type="text/javascript"></script>

  <link href="http://wwwendt.de/tech/dynatree/src/skin/ui.dynatree.css" rel="stylesheet" type="text/css" id="skinSheet">
  <script src="http://wwwendt.de/tech/dynatree/src/jquery.dynatree.js" type="text/javascript"></script>

  <script type="text/javascript">
    var treeData = [
      {title: "England", key: "England", expand: true,
        children: [
          {title: "Region", key: "Region", activate: true, expand:true,
            children: [
              {title: "London", key: "London" },
              {title: "Leeds", key: "Leeds" }
            ]
          }
        ]
      }
    ];
    $(function(){
      $("#tree3").dynatree({
        checkbox: true,
        selectMode: 3,
        children: treeData,
        onSelect: function(select, node) {
          // Get a list of all selected nodes, and convert to a key array:
          var selKeys = $.map(node.tree.getSelectedNodes(), function(node){
            return node.data.key;
          });
          $("#displayText").val(selKeys.join(", "));
        },
        onDblClick: function(node, event) {
          node.toggleSelect();
        },
        onKeydown: function(node, event) {
          if( event.which == 32 ) {
            node.toggleSelect();
            return false;
          }
        },
      });

      $("#opener").click(function() {
         var tree = $("#tree3");
         if (tree.css("display") == "none")
         {
            tree.css("display", "block") 
         } else {
            tree.css("display", "none");
         }
      });
  });
</script>
</head>

<body class="example">
  <div style="width: 500px;">
      <div>
        <input readonly="true" type="text" id="displayText" style="float:left;width:470px"/>
        <input type="button" id="opener" style="width:1px"/>
      </div>
      <div style="display:none" id="tree3"></div>
  </div>
</body>
</html>
like image 192
dknaack Avatar answered Sep 26 '22 00:09

dknaack