allow new values with chosen.js multiple select

后端 未结 10 1963
囚心锁ツ
囚心锁ツ 2020-12-12 19:30

I\'m using the chosen.js plugin http://harvesthq.github.com/chosen/ with jQuery to allow the user to select multiple options from a select. However, I now want to be able to

相关标签:
10条回答
  • 2020-12-12 19:31

    Here's a simple way that I did it:

    $(".search-field").find("input").live( "keydown", function (evt) {
        var stroke;
        stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
        if (stroke == 9) { // 9 = tab key
            $('#tags').append('<option value="' + $(this).val() + '" selected="selected">' + $(this).val() + '</option>');
            $('#tags').trigger('chosen:updated');
        }
    });
    
    0 讨论(0)
  • 2020-12-12 19:33

    I have updated the code from 3nochroot once more. Now I have only one selector to find the multiselect input.

    $(document).ready(function() {
    $(".js-choicelist").chosen({
        //config comes here
    }).parent().find('.chosen-container .search-field input[type=text]').keydown(
        function (evt) {
            var stroke, _ref, target, list;
            // get keycode
            stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
            // If enter or tab key
            if (stroke === 9 || stroke === 13) {
                target = $(evt.target);
                // get the list of current options
                chosenList = target.parents('.chosen-container').find('.chosen-choices li.search-choice > span').map(function () { return $(this).text(); }).get();
                // get the list of matches from the existing drop-down
                matchList = target.parents('.chosen-container').find('.chosen-results li').map(function () { return $(this).text(); }).get();
                // highlighted option
                highlightedList = target.parents('.chosen-container').find('.chosen-results li.highlighted').map(function () { return $(this).text(); }).get();
                // Get the value which the user has typed in
                var newString = $.trim(target.val());
                // if the option does not exists, and the text doesn't exactly match an existing option, and there is not an option highlighted in the list
                if ($.inArray(newString,matchList) < 0 && $.inArray(newString,chosenList) < 0 && highlightedList.length == 0) {
                    // Create a new option and add it to the list (but don't make it selected)
                    var newOption = '<option value="' + newString + '">' + newString + '</option>';
                    var choiceSelect = target.parents('.select-multiple').find('select');
                    choiceSelect.prepend(newOption);
                    // trigger the update event
                    choiceSelect.trigger("chosen:updated");
                    // tell chosen to close the list box
                    choiceSelect.trigger("chosen:close");
                    return true;
                }
                // otherwise, just let the event bubble up
                return true;
            }
        }
    )
    

    })

    0 讨论(0)
  • 2020-12-12 19:41

    I know this not the answer, but, an alternate solution.

    I was searching for the on-the-fly adding part and found that http://ivaynberg.github.com/select2/#tags provides the same thing as chosen + other stuffs like "Tagging".

    0 讨论(0)
  • 2020-12-12 19:42

    I stumbled upon this looking for the same ideas. Seems like its a pretty popular feature request, and a couple of forks have implemented it. Looks like it'll be merged into the master branch soon enough.

    +1 for this particular pull which worked a charm: https://github.com/harvesthq/chosen/pull/166

    You can view Koenpunt's fork here: https://github.com/koenpunt/chosen

    0 讨论(0)
  • 2020-12-12 19:42

    I was just trying to solve the same problem. I wound up modifying the source code a bit. Here's the new keyup_checker function. Take a look at case 13:

    AbstractChosen.prototype.keyup_checker = function(evt) {
      var stroke, _ref;
      stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
      this.search_field_scale();
      switch (stroke) {
        case 8:
          if (this.is_multiple && this.backstroke_length < 1 && this.choices > 0) {
            return this.keydown_backstroke();
          } else if (!this.pending_backstroke) {
            this.result_clear_highlight();
            return this.results_search();
          }
          break;
        case 13:
          evt.preventDefault();
          if (this.results_showing) {
            if (!this.is_multiple || this.result_highlight) {
              return this.result_select(evt);
            }  
    
            $(this.form_field).append('<option>' + $(evt.target).val() + '</option>');
            $(this.form_field).trigger('liszt:updated');
            this.result_highlight = this.search_results.find('li.active-result').last();
            return this.result_select(evt);
          } 
          break;
        case 27:
          if (this.results_showing) this.results_hide();
          return true;
        case 9:
        case 38:
        case 40:
        case 16:
        case 91:
        case 17:
          break;
        default:
          return this.results_search();
      }
    };
    
    0 讨论(0)
  • 2020-12-12 19:42

    An update to leogdion's answer which works with later versions of chosen:

            var dropDown = $('#select_chosen');
            // Make the chosen drop-down dynamic. If a given option is not in the list, the user can still add it
            dropDown.parent().find('.chosen-container .search-field input[type=text]').keydown( 
                function (evt) {
                   var stroke, _ref, target, list;
                   // get keycode
                   stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
                   // If enter or tab key
                   if (stroke === 9 || stroke === 13) {
                       target = $(evt.target);               
                       // get the list of current options
                       chosenList = target.parents('.chosen-container').find('.chosen-choices li.search-choice > span').map(function () { return $(this).text(); }).get();
                       // get the list of matches from the existing drop-down
                       matchList = target.parents('.chosen-container').find('.chosen-results li').map(function () { return $(this).text(); }).get();
                       // highlighted option
                       highlightedList = target.parents('.chosen-container').find('.chosen-results li.highlighted').map(function () { return $(this).text(); }).get();
                       // Get the value which the user has typed in
                       var newString = $.trim(target.val());
                       // if the option does not exists, and the text doesn't exactly match an existing option, and there is not an option highlighted in the list
                       if ($.inArray(newString,matchList) < 0 && $.inArray(newString,chosenList) < 0 && highlightedList.length == 0) {
                         // Create a new option and add it to the list (but don't make it selected)
                         var newOption = '<option value="' + newString + '">' + newString + '</option>';
                         $("#select").prepend(newOption);
                         // trigger the update event
                         $("#select").trigger("chosen:updated");
                         // tell chosen to close the list box
                         $("#select").trigger("chosen:close");
                         return true;
                      } 
                      // otherwise, just let the event bubble up
                      return true;
                   }
                }
            )
    
    0 讨论(0)
提交回复
热议问题