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

[Bug]: Dynamically added options not show when added first time #750

Closed
pgodowski-jt opened this issue Jul 30, 2024 · 2 comments
Closed
Labels
bug Something isn't working stale No activity

Comments

@pgodowski-jt
Copy link

Bug description

I have two select fields. On change of first one, the second is filled dynamically. Recently I updated webpack and encore. After that, at the first change of first select, the second is not updated (despite of instance options are well filled). But when I change value of first select again, everything works ok.

Expected behavior

Everything works from first time.

Steps to reproduce

First select:

<select id="demand_cosAgency" name="demand[cosAgency]"
        class="form-control-sm tomselected ts-hidden-accessible">
    <option value="" selected="selected">- Select an agency -</option>
    <option value="141">Label 141</option>
    <option value="183">Label 183</option>
    <option value="197">Label 197</option>
    ...
</select>

Second select:

<select id="demand_client" name="demand[client]"
        class="form-control-sm tomselected ts-hidden-accessible">
    <option value="">- First select an agency -</option>
</select>

Javascript part included in $.ajax complete(response) ):

const tomSelectInstance = document.getElementById('demand_client').tomselect;
      tomSelectInstance.clear();
      tomSelectInstance.clearOptions();
      tomSelectInstance.clearCache();
      tomSelectInstance.settings.maxOptions = null;

      console.log(response.responseJSON.agencyClients);

      $.each(response.responseJSON.agencyClients, (k, v) => {
        tomSelectInstance.addOption({ value: k, text: v });
      });

      tomSelectInstance.settings.placeholder = '- Select a client -';
      tomSelectInstance.open();

      console.log(tomSelectInstance.options);

      tomSelectInstance.clearCache();
      tomSelectInstance.sync();
      tomSelectInstance.refreshOptions();

Results of console.log(response.responseJSON.agencyClients);:

{
    "2340": "Label text",
    "2341": "Another label text",
    "2342": "blablabla",
    "2343": "text text"
    (...)
}

Results of console.log(tomSelectInstance.options);:

{
    "2340": {
        "value": "2340",
        "text": "Label text",
        "$order": 1,
        "$id": "demand_client-opt-1",
        "$div": {div#demand_client-opt-1.option.active}
    },
    "2341": {
        "value": "2341",
        "text": "Another label text",
        "$order": 2,
        "$id": "demand_client-opt-2",
        "$div": {div#demand_client-opt-2.option}
    },
    "2342": {
        "value": "2342",
        "text": "blablabla",
        "$order": 3,
        "$id": "demand_client-opt-3",
        "$div": {div#demand_client-opt-3.option}
    },
    "2343": {
        "value": "2343",
        "text": "text text",
        "$order": 4,
        "$id": "demand_client-opt-4",
        "$div": {div#demand_client-opt-4.option}
    }
    (...)
}

There are no #demand_client-opt-*.option divs on the page after first call. And after second one, everything works well.

I tried to add setTimeout, but nothing change.

Additional context

Symfony 6.4.10 with Twig.
System:
OS: Windows 10 10.0.19045
Binaries:
Node: 18.12.1
Yarn: 1.22.22
npm: 8.19.2
Browsers:
Chrome: Chromium (127.0.6533.72)
Firefox: 128.0.3
Packages:
postcss-loader: ^7.0.1 => 7.3.3
sass-loader: ^13.0.2 => 13.3.2
webpack: ^5.* => 5.93.0
webpack-cli: ^5.* => 5.1.4
webpack-notifier: ^1.15.0 => 1.15.0
tom-select: ^2.* => 2.3.1

@pgodowski-jt pgodowski-jt added the bug Something isn't working label Jul 30, 2024
@pgodowski-jt
Copy link
Author

pgodowski-jt commented Aug 6, 2024

I found, that it works on Symfony 6.2 but stop works on Symfony 6.3.
There is some init problem. I turned off symfonys' autocomplete and init tomselect manually if needed. And it works now.

Copy link

github-actions bot commented Dec 5, 2024

This issue has not been active in 120 days and has been marked "stale". Remove stale label or comment or this will be closed in 15 days

@github-actions github-actions bot added the stale No activity label Dec 5, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale No activity
Projects
None yet
Development

No branches or pull requests

1 participant