5

Currently I am customizing multiple selection drop-down list of Multiple Selection Plugin and these are the behaviors that I am going to apply on my customized selection :

  1. When parent is selected, all children will be selected.
  2. When all the children are selected, the parent would be selected too, otherwise if one of children is deselected, then the parent would be deselected too.
  3. When all the children are selected, there should be only parent's name that showed in the selected filed.
  4. The sub level should work as the 1st level as well.

(1), (2), and (4) I have already accomplished it. But for (3), I have not come up with any solution yet.

This is the sample json string of the multi selection :

var _str = '{"10":{"0":"0","1":"DISPONIBILITES","2":"t","style":"font-weight: bold;"},"16":{"0":"0","1":"TRESORERIE NETTE","2":"t","style":"font-weight: bold;"},...."}}}';

Here is the https://jsfiddle.net/skL589uu/7/ that I created.

It would be great if anyone here could give me some idea about that.

1 Answer 1

1

You don't have to write any custom logic for that, multi-select.js provides a feature called optGroups.

just group the child items under the parent group in the HTML and the rest is taken care.

Here is link to official docs, Multi-select OptGroups

HTML CODE:

  <select multiple="multiple">
    <optgroup label="Group 1">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </optgroup>
    <optgroup label="Group 3">
      <option value="15">15</option>
    </optgroup>
  </select>

JQUERY CODE:

$(function() {
   $("select").multipleSelect({
     multiple: true,
     multipleWidth: 55,
     width: '100%'
   });
});

Live Demo @ JSFiddle

Sign up to request clarification or add additional context in comments.

5 Comments

The selection html structure is returned from database so I don't want to modify it. I forgot to mentioned about this in my question.
you will be modifying it only for the purpose of rendering , so it wouldn't affect your data structure on DB. So while parsing the DOM structure and mapping it to the Select tag, you can just create a OptGroup tag and then add the child elements within it.
OptGroup doesn't work on selection that has multi level.
your right multi-select library is not supporting multi level list with OptGroup. so there is not straight forward solution for this, you need to have some custom logic for handling multi-select using the helper methods provided by the library. eg. you can set the parent id to select box when all its childs are selected,$('select').multipleSelect('setSelects', [parentId]);, jsfiddle.net/dreamweiver/skL589uu/18 . similarly you need to figure out how to handle the other scenarios. ref: wenzhixin.net.cn/p/multiple-select/docs/#methods
You have a script error and should look into this. @dreamweiver

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.