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

Repopluate select with new data after ajax call #79

Closed
mansouralex opened this issue Jul 5, 2016 · 2 comments
Closed

Repopluate select with new data after ajax call #79

mansouralex opened this issue Jul 5, 2016 · 2 comments

Comments

@mansouralex
Copy link

mansouralex commented Jul 5, 2016

I have problems getting the select plugin working in my case, tried as suggested on the plugin website with no luck.

$.ajax({
                type: "GET",
                url: requestURL,
                dataType: 'json',
                success: function(result)
                   {
                    $.each(result, function(i, obj) {
                        console.log(obj.description);
                        $('#cake-filling-types-select')
                                .append($("<option data-img-src='" + obj.image + "'></option>")
                                        .attr("value", obj.code)
                                        .text(obj.description));
                    });
                $("#cake-filling-types-select").imagepicker({
                    show_label  : true
                });

                $("#cake-filling-types-select").data("picker").sync_picker_with_select();

            }});

What I need to achieve is removing all the current options and repopulate with the new ones. At the moment I found that not all items are populated to the select control.

Also, how I can remove all current options?

Any help?

Thanks.

@ArmindoMaurits
Copy link

Why don't you just clear the select via jquery, then call the data picker again (or maybe just the sync picker with selects function, but I don't know if that works).

$("cake-filling-types-select").empty();

@mansouralex
Copy link
Author

@ArmindoMaurits Thank you for your reply.

Yes I used the empty jquery method now and it looks fine.

Also, for the other issue where some items are not getting into the select control, I found that these the ones that is missing the img src.

The updated code:

          $.ajax({
                type: "GET",
                url: requestURL,
                dataType: 'json',
                success: function(result){

                   $("#cake-filling-types-select").empty();

                    $.each(result, function(i, obj) {

                        if(obj.image != null)
                        {
                            var imgSrc = obj.image;
                        }else{
                            imgSrc = '';
                        }

                        $('#cake-filling-types-select')
                                .append($("<option data-img-src='" + imgSrc + "'></option>")
                                        .attr("value", obj.code)
                                        .text(obj.description));
                    });

                    $("#cake-filling-types-select").imagepicker({
                        show_label  : true
                    });

                }});

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

2 participants