chosen icon indicating copy to clipboard operation
chosen copied to clipboard

Feature: Option adding (with custom callback)

Open koenpunt opened this issue 12 years ago • 199 comments

Option adding

You can specify a create_option option, which can be a boolean or callback function.

Example for allowing adding of new items:

$(function() {
  $(".chzn-select").chosen({
    create_option: true,
    // persistent_create_option decides if you can add any term, even if part
    // of the term is also found, or only unique, not overlapping terms
    persistent_create_option: true,
    // with the skip_no_results option you can disable the 'No results match..' 
    // message, which is somewhat redundant when option adding is enabled
    skip_no_results: true
  });
});

Or with a callback and an AJAX request to, for example, save the terms in your database:

$(function() {
  $(".chzn-select").chosen({
    create_option: function(term){
      var chosen = this;
      $.post('add_term.php', {term: term}, function(data){
        chosen.append_option({
          value: 'value-' + data.term,
          text: data.term
        });
      });
    }
  });
});

Custom text

Through the options it is also possible to customize the text used in the 'Add option' link, like so:

$(function() {
  $(".chzn-select").chosen({
    create_option_text: 'Create category'
  });
});

I'll try to keep my release up to date with harvesthq/master: https://github.com/koenpunt/chosen/releases

koenpunt avatar Aug 07 '11 17:08 koenpunt

This is great! Would it be easy to make it so hitting the enter key also triggers the "add item" so you don't have to click the link? The enter key already works for selecting existing items from the list. Thanks!

marcusjames avatar Aug 08 '11 04:08 marcusjames

That was in my case intentionally left out, so the user is really aware of adding new items. But maybe I will add it as an extra option.

koenpunt avatar Aug 08 '11 08:08 koenpunt

Any desire to bundle this with the jquery plugin?

zanematthew avatar Dec 16 '11 05:12 zanematthew

I believe this is a good candidate for solving issue #5.

bewest avatar Dec 19 '11 19:12 bewest

I also fixed some small things, and updated my first comment here with the correct usage examples.

Also the functionality in the prototype version now matches with the jquery version again.

koenpunt avatar Dec 19 '11 20:12 koenpunt

+1 on UI +1 on code review

I'm using this branch on a site, hope it gets merged soon.

edrex avatar Dec 26 '11 10:12 edrex

@harvesthq There's any idea if this will be merged?

I'm a third party developer of the Zikula Web Application Framework, and we're interested to include Chosen on the bundled libraries, but this feature is very useful and seems that there's no attention to this pull request.

Personally, I'm using @koenpunt's master branch because it does what I need at this moment, but will anybody review the quality of this PR (I can't because my JS skills are low) and include this in the main project?

This is a feature requested months ago by a ot of people, and would be good to know if someone will take care of this, or if end-users will have to choose between the main project or koenpunt's fork.

Thanks for the great job.

matheo avatar Mar 13 '12 16:03 matheo

Is this going to be merged in? I need to dynamically create items in the list.

sontek avatar Mar 15 '12 19:03 sontek

@matheo I performed a code review on several branches solving issue #5 and recommended this one, FWIW.

bewest avatar Mar 17 '12 23:03 bewest

If this isn't a good patch that could be merged, just let me know why and I will make the changes needed, I really want this feature put into chosen.

sontek avatar Mar 19 '12 21:03 sontek

+1 For getting this merged in. Would also be good to have option for enter key to triggers the "add item"

danbronsema avatar Mar 26 '12 01:03 danbronsema

This would be really helpful. Please merge!

amacneil avatar Apr 11 '12 00:04 amacneil

This is really awesome feature, that chosen lack of.

ixti avatar Apr 25 '12 12:04 ixti

:+1:

willdurand avatar Apr 26 '12 15:04 willdurand

+1 for merging this into master

mikke123 avatar Apr 27 '12 22:04 mikke123

Wants this as well

gabrielbull avatar Apr 29 '12 16:04 gabrielbull

+1

kyriacos avatar Apr 30 '12 02:04 kyriacos

+1

timoxley avatar Apr 30 '12 12:04 timoxley

+!

radomirml avatar May 06 '12 11:05 radomirml

There is a bug when a OnChange event is present for the

    Chosen.prototype.select_append_option = function (options)
    {
        var option;
        option = $('<option />', options).attr('selected', 'selected');
        this.form_field_jq.append(option);
        this.form_field_jq.trigger("liszt:updated");
        this.form_field_jq.trigger("change"); // ADD THIS LINE TO FIRE THE CHANGE EVENT
        return this.search_field.trigger('focus');
    };

I needed the OnChange event to enable the submit button when a item is available in the chosen autocomplete (there was none, and I added one with the new create_option config)

JCKodel avatar May 07 '12 21:05 JCKodel

+1 ! Great feature !

a-bastin avatar May 11 '12 15:05 a-bastin

+1

tute avatar May 16 '12 22:05 tute

+1

oisinlavery avatar May 22 '12 14:05 oisinlavery

+1

abrambailey avatar May 26 '12 02:05 abrambailey

@koenpunt Thanks so much for the awesome plugin. Can you please help me. I'd like to get the below code to trigger a change event even when a new business is typed in, and not selected from the autocomplete options. Cheers, Abran

$(function() { $("#dispenser_business_id").chosen({ create_option: true, persistent_create_option: true, create_option_text: 'Create New Business' }).change( function() {$('#myModal').modal({})}); });

abrambailey avatar May 27 '12 03:05 abrambailey

+1 This is a very useful feature. However adding options to multiple select seems to be broken at the moment. If you add a new item and then start to type in the name of the next one that text it is removed. Starting to type again brings it back to normal.

fson avatar Jul 12 '12 19:07 fson

The problem I reported seems to be fixed now. Works fine!

fson avatar Jul 16 '12 21:07 fson

@funkdified To get back on you, you could use the callback:

$(function() {
   $("#dispenser_business_id").chosen({
      create_option: function(term) {
         this.append_option({
            value: term,
            text: term
         });
         $('#myModal').modal({});
      },
      persistent_create_option: true,
      create_option_text: 'Create New Business'
   });
});

koenpunt avatar Jul 16 '12 21:07 koenpunt

@koenpunt thanks for your changes... I didn't know if you already had option not to show "ADD" for items that are already in the list so I did my own version for jQuery - I hope somebody can integrate it with prototype if it's needed:

$(this.form_field).find('option').filter(function() { return ($(this).text() == terms); }).length == 0

That's the condition I've added in no_results method; i.e:

Chosen.prototype.no_results = function(terms) {
  var no_results_html;
  no_results_html = $('<li class="no-results">' + this.results_none_found + ' "<span></span>"</li>');
  no_results_html.find("span").first().html(terms);
  this.search_results.append(no_results_html);
  if (this.create_option && $(this.form_field).find('option').filter(function() { return ($(this).text() == terms); }).length == 0) { // added second condition
    return this.show_create_option(terms);
  }
};

mikke123 avatar Jul 17 '12 08:07 mikke123

@mikke123 Setting persistent_create_option to false or not supplying it al all should already take care of that.

koenpunt avatar Jul 17 '12 08:07 koenpunt