6

I have an select with some option's:

 <select id="select">
   <option>1</option>
   <option disabled="true">2</option>
   <option>3</option>
 </select>

I'm using Chosen Plugin for jQuery and the problem is that disabled options are removed from the view. But I need to show it as unclickable disabled elements.

Is there any chance with jquery chosen plugin?

-- The example would transformed to:

 <ul class="chzn-results">
   <li id="location_select_chzn_o_0" class="active-result result-selected">1</li>
   <li id="location_select_chzn_o_2" class="active-result">3</li>
</ul>

So the secound element is not made unvisible, it simply not exist.

3
  • can you inspect the element in Firebug and see if that element has a style of 'display:none;', 'visibility:hidden', or 'opacity:0'? Or is it completely removed from the DOM?
    – Ohgodwhy
    Jun 17, 2012 at 22:18
  • Sorry for late answer, I edited the question.
    – take
    Jun 18, 2012 at 16:37
  • 3
    I took a look at the Source and Docs for the plugin. It's evident that Chosen automatically highlights selected options and removes disabled options As mentioned directly in the DOCS. A closer inspection reveals that you'll have a hay-day reconfiguring this.
    – Ohgodwhy
    Jun 18, 2012 at 18:25

2 Answers 2

9

nicholmikey's method is correct but here is the code you need to replace in chosen.jquery.js It's just a few simple lines (commented below). Hope this helps.

AbstractChosen.prototype.result_add_option = function(option) {
  var classes, style;

    option.dom_id = this.container_id + "_o_" + option.array_index;
    classes = option.selected && this.is_multiple ? [] : ["active-result"];
    if (option.selected) {
      classes.push("result-selected");
    }
    if (option.group_array_index != null) {
      classes.push("group-option");
    }

    // THIS IS NEW ---------------
    if (option.disabled) {
      classes.push("disabled");
    }
    // ---------------------------

    if (option.classes !== "") {
      classes.push(option.classes);
    }
    style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : "";
    return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"' + style + '>' + option.html + '</li>';
};

and this

Chosen.prototype.result_select = function(evt) {
  var high, high_id, item, position;
  if (this.result_highlight) {
    high = this.result_highlight;
    high_id = high.attr("id");
    this.result_clear_highlight();
    if (this.is_multiple) {
      this.result_deactivate(high);
    } else {
      this.search_results.find(".result-selected").removeClass("result-selected");
      this.result_single_selected = high;
      this.selected_item.removeClass("chzn-default");
    }
    high.addClass("result-selected");
    position = high_id.substr(high_id.lastIndexOf("_") + 1);
    item = this.results_data[position];

    // THIS IS NEW ---------------
    if(this.form_field.options[item.options_index].disabled){
      return false;
    }
    // ---------------------------

    item.selected = true;
    this.form_field.options[item.options_index].selected = true;
    if (this.is_multiple) {
      this.choice_build(item);
    } else {
      this.selected_item.find("span").first().text(item.text);
      if (this.allow_single_deselect) this.single_deselect_control_build();
    }
    if (!(evt.metaKey && this.is_multiple)) this.results_hide();
    this.search_field.val("");
    if (this.is_multiple || this.form_field_jq.val() !== this.current_value) {
      this.form_field_jq.trigger("change", {
        'selected': this.form_field.options[item.options_index].value
      });
    }
    this.current_value = this.form_field_jq.val();
    return this.search_field_scale();
  }
};

Finally to grey them out I added this css...

.chzn-results .disabled{color:#CCC;}
1
  • Work fine for me thanks. But I need a certain time to see that you have deleted the code 'if (!option.disabled) {' .Perhaps will be better dont delete it and just comment it ;-)
    – Hugo
    May 29, 2013 at 8:33
6

You will need to edit the plugin as outlined here :

http://bitsmash.wordpress.com/2012/10/01/making-disabled-elements-visible-with-the-jquery-chosen-plugin/

The plugin reads in a select, removes it from the DOM, and adds a new ul. Options marked "Disabled" are skipped when adding li to the new ul

Here is the method of interest in chosen.jquery.js

AbstractChosen.prototype.result_add_option = function(option) {
  var classes, style;
  if (!option.disabled) {
    option.dom_id = this.container_id + "_o_" + option.array_index;
    classes = option.selected && this.is_multiple ? [] : ["active-result"];
    if (option.selected) classes.push("result-selected");
    if (option.group_array_index != null) classes.push("group-option");
    if (option.classes !== "") classes.push(option.classes);
    style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : "";
    return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"' + style + '>' + option.html + '</li>';
  } else {
    return "";
  }
};

Notice that if an option is disabled it returns nothing. Take the line

return "";

and put in the html/css you would like for a disabled item. Or, you can remove the if (!option.disabled) block, and add a new if (!option.disabled) block that adds a special CSS class if the item is disabled.

Next, you need to make sure when users click a disabled item nothing happens. You need to edit this method :

 Chosen.prototype.result_select = function(evt) {
  var high, high_id, item, position;
  if (this.result_highlight) {
    high = this.result_highlight;
    high_id = high.attr("id");
    this.result_clear_highlight();
    if (this.is_multiple) {
      this.result_deactivate(high);
    } else {
      this.search_results.find(".result-selected").removeClass("result-selected");
      this.result_single_selected = high;
      this.selected_item.removeClass("chzn-default");
    }
    high.addClass("result-selected");
    position = high_id.substr(high_id.lastIndexOf("_") + 1);
    item = this.results_data[position];
    item.selected = true;
    this.form_field.options[item.options_index].selected = true;
    if (this.is_multiple) {
      this.choice_build(item);
    } else {
      this.selected_item.find("span").first().text(item.text);
      if (this.allow_single_deselect) this.single_deselect_control_build();
    }
    if (!(evt.metaKey && this.is_multiple)) this.results_hide();
    this.search_field.val("");
    if (this.is_multiple || this.form_field_jq.val() !== this.current_value) {
      this.form_field_jq.trigger("change", {
        'selected': this.form_field.options[item.options_index].value
      });
    }
    this.current_value = this.form_field_jq.val();
    return this.search_field_scale();
  }
};

Add a block that says, if disabled, return false, then when users click on that item nothing will happen.

1
  • Sorry for late response. Your example works very well, thanks!
    – take
    Oct 23, 2012 at 11:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.