selectize.js icon indicating copy to clipboard operation
selectize.js copied to clipboard

Preserve custom HTML5 data attributes

Open ghost opened this issue 12 years ago • 61 comments

Feature request

Preserve custom HTML5 data attributes, so that in this case data-fruit won't disappear when going from <select> to Selectize:

http://jsfiddle.net/k44p2by6/

ghost avatar Jan 03 '14 05:01 ghost

+1

MaherSaif avatar Jan 13 '14 17:01 MaherSaif

I've created a plugin to override -not extend- the function which changes the original select options.

Selectize.define('preserve_original_select', function(options) {
  var self = this;

  this.updateOriginalInput = (function() {
    var original = self.updateOriginalInput;
    return function() {
      var i, n, options, self = this;

      if (self.$input[0].tagName.toLowerCase() === 'select') {
        for (i = 0, n = self.items.length; i < n; i++) {
          self.$input.find('[value="'+self.items[i]+'"]').prop('selected', true);
        }
      } else {
        self.$input.val(self.getValue());
      }

      if (self.isSetup) {
        self.trigger('change', self.$input.val());
      }

      // original.apply(this, arguments);
    };
  })();

});

Don't forget to load the plugin.

MaherSaif avatar Jan 13 '14 18:01 MaherSaif

Plugin is good but data-attributes in OPTIONs shouldn't disappear by default. It is hurt for transparent selectizing of existing SELECT elements.

xak2000 avatar Mar 19 '14 11:03 xak2000

Yes! Selectize should be updated to allow for the preservation of custom attributes in each option.

dylanbromby avatar Mar 31 '14 22:03 dylanbromby

+1

vizcay avatar May 17 '14 14:05 vizcay

Would really appreciate a fix to this. Can't get plugin working.

ryannealmes avatar Jun 25 '14 13:06 ryannealmes

+1

jcomack avatar Aug 25 '14 13:08 jcomack

Agreed, selectize should retain data attributes

jontjs avatar Aug 27 '14 08:08 jontjs

+1

nexenator avatar Sep 01 '14 15:09 nexenator

I have no idea how I got CC'd on all the posts to this list, but I'd appreciate it if it stopped. Thanks!

On Sep 1, 2014, at 8:05 AM, Stephan Helbig [email protected] wrote:

+1

— Reply to this email directly or view it on GitHub.

dylanbromby avatar Sep 01 '14 15:09 dylanbromby

+1

arjan0307 avatar Oct 20 '14 14:10 arjan0307

+1

jensljungblad avatar Dec 17 '14 14:12 jensljungblad

+1

telmogh avatar Dec 17 '14 21:12 telmogh

+1

iamroi avatar Dec 21 '14 14:12 iamroi

This is working.

$(ele).selectize({ ..... dataAttr: 'data-extra', //default is data-data ..... } });

Add data-extra in every option as json encoded, Ex option value="one" data-extra="{'test':'123'}" option value="two" data-extra="{'test':'456'}"

Get the selectize instance using this, var selectize = $(ele)[0].selectize

Users selected options can be found here: selectize.items

Data attributes of all options can be found here: selectize.options

In our case it will be, selectize.options['one'].test selectize.options['two'].test

Let me know if this is working.

iamroi avatar Dec 21 '14 15:12 iamroi

I think that it's already possibile to do that without extending the plugin (certainly, a full support will be appreciated) simply using the onInitialize method in combination with the revertSettings attribute (jsfiddle):

$('select').selectize({
    onInitialize: function () {
        var s = this;
        this.revertSettings.$children.each(function () {
            $.extend(s.options[this.value], $(this).data());
        });
    }
});

The possibilities are infinite with this feature, like a FULL HTML SELECT.

indrimuska avatar Feb 10 '15 11:02 indrimuska

@indrimuska You are a hero sir. That works like charm!

JamesWilsonProjects avatar Feb 17 '15 20:02 JamesWilsonProjects

@JamesWilsonVSolvit :smile: thank you man!

indrimuska avatar Feb 17 '15 20:02 indrimuska

works like charm, but not for option groups as their are undefined :(. any idea how to get to next level.. ( get each from option group and keep custom-data ? )

mkimont avatar Apr 20 '15 12:04 mkimont

Hi @kimek, as you can see, revertSettings has a $children attribute that contains all the previous children of the original select you define. If your select has got many <optgroup>s, the $children attribute contains these jquery elements, not the <option>s you want. That's why you get undefined as value of those datas.

schermata 2015-04-20 alle 15 28 10

The simplest way to overcome this problem is to look for the <option>s inside the <optgroup>s as you normally do in jQuery (.find()). Here it is an example.

indrimuska avatar Apr 20 '15 13:04 indrimuska