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

Cannot dynamically populate data from ajax response #79

Open
abdulrachman opened this issue Jun 25, 2022 · 5 comments
Open

Cannot dynamically populate data from ajax response #79

abdulrachman opened this issue Jun 25, 2022 · 5 comments
Assignees

Comments

@abdulrachman
Copy link

Hi there,

I just trying get data from ajax, and I want to try to populate array of data / response to DualListBox (available or selected)
Can you please just give me the right way?

Thankyou

@JostCrow JostCrow self-assigned this Sep 29, 2022
@bloodyburger
Copy link

bloodyburger commented Sep 30, 2022

I do this to dynamically update from Ajax response, just make sure dualListbox is declared globally.

$.ajax({
           url: '/fxx',
           type: "POST",
           data: formData,
           dataType: "json",
           headers: {
               'X-CSRFToken': $("[name=csrfmiddlewaretoken]").val()
           },
           mimeType: "multipart/form-data",
           success: function(data) {
               console.log(data.input_cols)
               var cols = data.input_cols
               //let dualListbox = new DualListbox("#dg_dual_listbox");
               dualListbox.available = [];
               dualListbox.options = "";
               $.each(cols, function(idx, col) {
                   console.log(col)
                   dualListbox._addOption({
                       'text': col,
                       'value': col
                   });
               });
               dualListbox.updateAvailableListbox();

           },

@JostCrow
Copy link
Collaborator

I am also working on a new release where adding data dynamically should be possible.

@bloodyburger
Copy link

I do this to dynamically update from Ajax response, just make sure dualListbox is declared globally.

$.ajax({
           url: '/fxx',
           type: "POST",
           data: formData,
           dataType: "json",
           headers: {
               'X-CSRFToken': $("[name=csrfmiddlewaretoken]").val()
           },
           mimeType: "multipart/form-data",
           success: function(data) {
               console.log(data.input_cols)
               var cols = data.input_cols
               //let dualListbox = new DualListbox("#dg_dual_listbox");
               dualListbox.available = [];
               dualListbox.options = "";
               $.each(cols, function(idx, col) {
                   console.log(col)
                   dualListbox._addOption({
                       'text': col,
                       'value': col
                   });
               });
               dualListbox.updateAvailableListbox();

           },

There is a problem with this approach, the selected values are not passed to the request. I'm waiting for update from @JostCrow to see his approach.

@Fregionz
Copy link

Fregionz commented Jun 22, 2023

Hello, i'm using dynamic option value, but when i try to add or remove it return a error:

Uncaught TypeError: Cannot read properties of undefined (reading 'selected')
at d.changeSelected (dual-listbox.js:140:47)
at d.actionItemDeselected (dual-listbox.js:285:18)
at HTMLButtonElement. (dual-listbox.js:344:18)

even my option have selected element already:

arrOptions.push ({
text : val['name'],
value : val['id'],
selected : listSelected.includes(val['id'])
});

@eobeda
Copy link

eobeda commented Aug 21, 2023

Hi,

I had a similar issue and found that the "value" is expected to be a string value. I couldn't tell from your example the data type you are using. A quick fix might be to try:

value : val['id'].toString(),

when loading the data. Watch out for how this data conversation might affect other bits of your downstream logic.

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

5 participants