selectize / selectize.js

Selectize is the hybrid of a textbox and <select> box. It's jQuery based, and it has autocomplete and native-feeling keyboard navigation; useful for tagging, contact lists, etc.
https://selectize.dev/
Apache License 2.0
13.02k stars 3.58k forks source link

Loading Values from Multi-Dimensional JSON Array of Objects #892

Closed trevornagy closed 9 years ago

trevornagy commented 9 years ago

I'm currently attempting to load this json into selectize:

{"platforms": [{"id":32,"name":"Sega Saturn","slug":"saturn"}, {"id":14,"name":"Mac","slug":"mac"}, {"id":47,"name":"Virtual Console (Nintendo)","slug":"vc"}, {"id":34,"name":"Android","slug":"android"}, {"id":84,"name":"SG-1000","slug":"sg1000"}, {"id":58,"name":"Super Famicom","slug":"sfam"}, {"id":82,"name":"Web browser","slug":"browser"}] }

I have this code, which isn't working, could anyone give me some suggestions?

var data = JSON.parse('{"platforms":[{"id":32,"name":"Sega Saturn","slug":"saturn"},{"id":14,"name":"Mac","slug":"mac"},{"id":47,"name":"Virtual Console (Nintendo)","slug":"vc"},{"id":34,"name":"Android","slug":"android"},{"id":84,"name":"SG-1000","slug":"sg1000"},{"id":58,"name":"Super Famicom","slug":"sfam"},{"id":82,"name":"Web browser","slug":"browser"}]}');
var options = [];
$.each(data.platforms, function()
{
    options.push({
        id: this.id,
        title: this.name,
        url: "hhtp://site.com/"+this.slug
    });
});
var $select = $('#select-tools').selectize({
    maxItems: null,
    valueField: 'id',
    labelField: 'title',
    searchField: 'title',
    options: options,
    create: false
});

I'm basing it off of this example, and this one too. What am I doing incorrectly?

trevornagy commented 9 years ago

I fixed it, the Javascript was correct. My HTML wasn't.