2
votes

How I can use selectize.js with remote source? I have followed this example:

$('#select-repo').selectize({
    valueField: 'url',
    labelField: 'name',
    searchField: 'name',
    create: false,
    render: {
        option: function(item, escape) {
            return '<div>' +
                '<span class="title">' +
                    '<span class="name"><i class="icon ' + (item.fork ? 'fork' : 'source') + '"></i>' + escape(item.name) + '</span>' +
                    '<span class="by">' + escape(item.username) + '</span>' +
                '</span>' +
                '<span class="description">' + escape(item.description) + '</span>' +
                '<ul class="meta">' +
                    (item.language ? '<li class="language">' + escape(item.language) + '</li>' : '') +
                    '<li class="watchers"><span>' + escape(item.watchers) + '</span> watchers</li>' +
                    '<li class="forks"><span>' + escape(item.forks) + '</span> forks</li>' +
                '</ul>' +
            '</div>';
        }
    },
    score: function(search) {
        var score = this.getScoreFunction(search);
        return function(item) {
            return score(item) * (1 + Math.min(item.watchers / 100, 1));
        };
    },
    load: function(query, callback) {
        if (!query.length) return callback();
        $.ajax({
            url: 'https://api.github.com/legacy/repos/search/' + encodeURIComponent(query),
            type: 'GET',
            error: function() {
                callback();
            },
            success: function(res) {
                callback(res.repositories.slice(0, 10));
            }
        });
    }
});

And works fine as it is. If I change the url with mine it stops working.

Example response from remote remote url:

[
  {
    "id":2,
    "title":"Facere sequi dolore dignissimos eum aut et."
  },
  {
    "id":3,
    "title":"Perspiciatis laudantium aut eos autem dolor quisquam."
  },
  {
    "id":10,
    "title":"Enim provident vel rerum laborum quis nobis."
  },
  {
    "id":25,
    "title":"Porro earum nihil recusandae rerum ratione sunt."
  }
]

I used console.log inside load function and the response is good. Even though there is no dropdown in the select box. I have been trying to make it work for over 3 hours with no success.

1
Add the specific data your URL returns to the question. I'm guessing this if your problem: stackoverflow.com/a/31881542/1160796 - basher
I have updated my question and added an example response. Hope it helps. - Alex Kyriakidis

1 Answers

0
votes

This is exactly like my other answer. You are using Id/Title but your render function uses a TON of other fields (description, username, fork). Change your render to this:

render: {
     option: function (item, escape) {
         return '<div>' + escape(item.title) + '</div>';
     }
}

You also need to change the success function, as it does not get a list of repositories:

success: function(res) {
        callback(res);
    }