Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selectize: Setting Default Value in onInitialize with setValue

I have a web application with multiple Selectize objects initialized on the page. I'm trying to have each instance load a default value based on the query string when the page loads, where ?<obj.name>=<KeywordID>. All URL parameters have already been serialized are are a dictionary call that.urlParams.

I know there are other ways to initializing Selectize with a default value I could try; but, I'm curious why calling setValue inside onInitialize isn't working for me because I'm getting any error messages when I run this code.

I'm bundling all this JavaScript with Browserify, but I don't think that's contributing to this problem.

In terms of debugging, I've tried logging this to the console inside onInititalize and found that setValue is up one level in the Function.prototype property, the options property is full of data from load, the key for those objects inside options corresponds to the KeywordID. But when I log getValue(val) to the console, I get an empty string. Is there a way to make this work or am I ignoring something about Selectize or JavaScript?

module.exports = function() {    
    var that = this;

    ...

    this.selectize = $(this).container.selectize({
        valueField: 'KeywordID',  // an integer value
        create: false,
        labelField: 'Name',
        searchField: 'Name',
        preload: true,
        allowEmptyOptions: true,
        closeAfterSelect: true,
        maxItems: 1,
        render: {
           option: function(item) {
                return that.template(item);
            },
        },
        onInitialize: function() {
            var val = parseInt(that.urlParams[that.name], 10);  // e.g. 5
            this.setValue(val);
        },
        load: function(query, callback) {
            $.ajax({
                url: that.url,
                type: 'GET',
                error: callback,
                success: callback
            })
        }
    });

};

...
like image 851
kas Avatar asked Jun 14 '16 18:06

kas


People also ask

Is it possible to trigger setValue() instead of oninitialize?

Instead of using onInitialize you could add a load trigger to the selectize. This will fire after the load has finished and will execute setValue () as expected.

How to initialize selectize ' selected value?

Note that for this you first have to get the selectize instanze ( $select [0].selectize ). but i dont know why? You can initialize each selectize ' selected value by setting the items property. Fetch the value from your querystring then add it as an item of the items property value:

Why does selectize set options to default to false?

If true, Selectize will treat any options with a "" value like normal. This defaults to false to accomodate the common <select> practice of having the first empty option to act as a placeholder. The animation duration (in milliseconds) of the scroll animation triggered when going [up] and [down] in the options dropdown.

How do I set the default value to the auto-implemented property?

In C# 6.0 or higher versions, assign the inline default value to the auto-implemented property, as shown below. The following example sets the default value to a private property field. You can assign the default value using the DefaultValueAttribute attribute, as shown below.


2 Answers

After sprinkling in some console.logs into Selectize.js, I found that the ajax data hadn't been imported, when the initialize event was triggered. I ended up finding a solution using jQuery.when() to make setValue fire after the data had been loaded, but I still wish I could find a one-function-does-one-thing solution.

module.exports = function() {    
    var that = this;

    ...

    this.selectize = $(this).container.selectize({
        valueField: 'KeywordID',  // an integer value
        create: false,
        labelField: 'Name',
        searchField: 'Name',
        preload: true,
        allowEmptyOptions: true,
        closeAfterSelect: true,
        maxItems: 1,
        render: {
           option: function(item) {
                return that.template(item);
            },
        },
        load: function(query, callback) {
            var self = this;
            $.when( $.ajax({
                url: that.url,
                type: 'GET',
                error: callback,
                success: callback
            }) ).then(function() {
                var val = parseInt(that.urlParams[that.name], 10);  // e.g. 5
                self.setValue(val);
            });
        }
    });

};

...
like image 123
kas Avatar answered Nov 09 '22 13:11

kas


You just need to add the option before setting it as the value, as this line in addItem will be checking for it: if (!self.options.hasOwnProperty(value)) return;

inside onInitialize you would do:

var val = that.urlParams[that.name]; //It might work with parseInt, I haven't used integers in selectize options though, only strings.
var opt = {id:val, text:val};
self.addOption(opt);
self.setValue(opt.id);
like image 24
basher Avatar answered Nov 09 '22 12:11

basher