jQuery Chosen Universal Initialization

  • 2021-07-26 06:39:20
  • OfStack

1 is directly using Chosen, an js plug-in, whose purpose is to beautify the drop-down box. github Address: https://harvesthq.github.io/chosen/

no_results_text: Text to display without search results for "xxxxx"

allow_single_deselect: Does true allow deselection
disable_search: Does true have a search box

max_selected_options: Maximum number of selections when select is multiple selection

Official description document address

Official documentation address for configuration options


/*  Function : Chosen Universal initialization 
 *  Founder: Brian  Creation time: 2016-12-13
 */
(function ($j) {
  var chosenTool = function (el, options) {
    this.opts = options;
    this.chosenInit();
    this.chose_get_init();
    this.chose_mult_set_init(this.opts.hidClassName);
    this.clickEvent();
    return this;
  }
  chosenTool.opts = {
    selectId: '',//selectId
    hidClassName: '',// Hidden field Class
    placeholdertxt: '',//select Medium placeholder Text 
    noresulttxt: '',// Text to display when the entered name is not found 
    dataJson: ''// Data source 
  };
  $j.fn.myChosenTool = function (opt) {
    var value,
      args = Array.prototype.slice.call(arguments, 1);
    var $jthis = $j(this),
      data = $jthis.data('chosenTool'),
      options = $j.extend({}, chosenTool.opts, $jthis.data(),
        typeof option === 'object' && option);
    if (typeof option === 'string') {
      // Determine whether the method called by the user exists 
      //if ($j.inArray(option, allowedMethods) < 0) {
      //  throw new Error("Unknown method: " + option);
      //}
      if (!data) {
        return;
      }
      value = data[option].apply(data, args);
      if (option === 'destroy') {
        $jthis.removeData('chosenTool');
      }
    }
    /* Calling methods inside plug-ins from outside plug-ins needs to be modified to the following form */
    //if (typeof opt === 'string') {
    //  if (!data) {
    //    return;
    //  }
    //  value = data[opt].apply(data, args);
    //  if (opt === 'destroy') {
    //    $jthis.removeData('chosenTool');
    //  }
    //}
    if (!data) {
      opt["selectId"] = $j(this).attr("id");
      $jthis.data('chosenTool', (data = new chosenTool(this, opt)));
    }
    console.log(data);
    return typeof value === 'undefined' ? this : value;
  };
  chosenTool.prototype.clickEvent = function () {
    var _this = this;
    $j("#" + this.opts.selectId).on("change", function () {
      _this.chose_get_value();
    });
  };
  /* Drop-down box initialization method */
  chosenTool.prototype.selectInfoInit = function () {
    var proOPts = "";
    this.opts.dataJson = $j.parseJSON(this.opts.dataJson);
    $j.each(this.opts.dataJson, function (index, item) {
      proOPts += "<option value='" + item.ValueField + "'>" + item.TextField + "</option>";
    });
    $j("#" + this.opts.selectId).append(proOPts);
    // Initialization chosen
    $j("#" + this.opts.selectId).chosen({
      allow_single_deselect: true, // Allow deselection 
      placeholder_text_multiple: this.opts.placeholdertxt, // When no value is selected in the multi-check box   Text displayed 
      no_results_text: this.opts.noresulttxt,// Text displayed without search results 
      search_contains: true// Whether fuzzy search is supported 
    });
  };
  /* Object initialization method */
  chosenTool.prototype.chosenInit = function () {
    this.selectInfoInit();
  };
  // Private method to check whether the parameter is legal 
  chosenTool.prototype.isValid = function () {
    return !this.options || (this.options && typeof this.options === "object") ? true : false;
  };
  // Data synchronization 
  chosenTool.prototype.chose_get_init = function () {
    var selectId = this.opts.selectId;
    $j("#" + this.opts.selectId).chosen().change(
         function () {
           $j("#" + selectId).trigger("liszt:updated");// Update drop-down box 
         });
  };
  // Single choice select value Get 
  chosenTool.prototype.chose_get_value = function () {
    var selectVal = $j("#" + this.opts.selectId).val();
    $j("." + this.opts.hidClassName).val(selectVal);
  };
  // Single choice select value Get 
  chosenTool.prototype.chose_mult_set_init = function () {
    var values = $j("." + this.opts.hidClassName).val();
    if (values && values.indexOf(',') > 0) {
      var arr = values.split(',');
      var length = arr.length;
      var value = '';
      for (i = 0; i < length; i++) {
        value = arr[i];
        $j("#" + this.opts.selectId + " [value='" + value + "']").attr('selected', 'selected');
      }
    } else {
      $j("#" + this.opts.selectId + " [value='" + values + "']").attr('selected', 'selected');
    }
    $j("#" + this.opts.selectId).trigger("liszt:updated");
  };
  //select text Get, please pay attention when selecting multiple 
  chosenTool.prototype.chose_get_text = function () {
    return $j("#" + this.opts.selectId + " option:selected").text();
  };
})(jQuery);

Related articles: