Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load from JSON #1151

Closed
EmilMoe opened this issue Sep 13, 2015 · 4 comments
Closed

Load from JSON #1151

EmilMoe opened this issue Sep 13, 2015 · 4 comments

Comments

@EmilMoe
Copy link

EmilMoe commented Sep 13, 2015

I need to load my lists from JSON files so I have made an extension to your script. Rather than modifying it I chose to extend it in order to keep it simple to receive your updates. Everyone are welcome to use my extension, but I hope it will be implemented in the codebase.

It works like this:
Add an attribute to your select: data-url="link-to-json", make sure the select has the class selectpicker.

The json file must be formatted like this:

[{
    name: "Name",
    id: 1
},
{
    name: "Name2",
    id: 2
}]

And so on. It will take name as display value and id as option value.

$(document).ready(function()
{
    $('select[class~="selectpicker"][data-url]').each(function(index, value)
    {
        var select = $(this);
        var url    = $(this).attr('data-url');
        var list   = [];

        $.getJSON(url, function(data)
        {
            $.each(data, function(key, val)
            {
                list.push('<option value="' + val.id + '">' + val.name + '</option>');
            });

            select.html(list.join(''));
            select.selectpicker('refresh');
        });
    });
});
@caseyjhol
Copy link
Member

@EmilMoe
Copy link
Author

EmilMoe commented Oct 5, 2015

An updated version which allows you to define the value and label as attributes
data-id="value"
data-label="label"

$( document ).ready(function()
{
    $('select[class~="selectpicker"][data-url]').each(function(index, value)
    {
        var select = $(this);
        var url    = $(this).attr('data-url');
        var id     = $(this).attr('data-id');
        var label  = $(this).attr('data-label');

        $.getJSON(url, function(data)
        {
            select.html('');

            $.each(data, function(key, val)
            {
                select.append('<option value="' + val[id] + '">' + val[label] + '</option>');
            });

            select.selectpicker('refresh');
        });
    });
});

@waspinator
Copy link

another option to load/reload data from a json item list

function loadSelectItems(select, items) {

    var options = '';
    $.each(items, function(key, value) {
        options += '<option value=' + key + '>' + value + '</option>';
    });
    select.empty();
    select.append(options);
    select.selectpicker('refresh');
}

@caseyjhol
Copy link
Member

Please 👍 and follow #899.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants