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

Custom values in onServerSearch are not respected #330

Open
MichalLauer opened this issue Apr 25, 2024 · 1 comment
Open

Custom values in onServerSearch are not respected #330

MichalLauer opened this issue Apr 25, 2024 · 1 comment
Assignees

Comments

@MichalLauer
Copy link

Hello,

there is a bug that if a user sets their own regex expression within onServerSearch(...), the correct results are no longer displayed. See reprex here -https://codepen.io/MichalLauer/pen/XWQOVpo. There, I am using a custom regex to search and return values (line 5). For example, if you search for "c b a", I want to return all items that contain "c", "b", and "a". The regex correctly identifies them and prints them (lines 8 - 10). So the contents of onServerSearch are correct. However, nothing is printed in the virtual select.

This happens even in cases without regex. For example, if I wanted to always show only the first option, the VirtualSelect shows items based on the default search function - https://codepen.io/MichalLauer/pen/JjVxxXK. In the second reprex, no matter what you search for, the only option visible should always be "a b c". However, searching for items such as "i" returns "i j k". The custom function works in both cases correctly, which can be checked in the console where only visible options are printed.

@MichalLauer MichalLauer changed the title Custom regex in onServerSearch is not respected Custom values in onServerSearch are not respected Apr 25, 2024
@MichalLauer
Copy link
Author

MichalLauer commented May 17, 2024

I was not able to identify what exactly causes the issue, but a temporary solution is to replace the virtualSelect.setServerOptions(newOptions); call with the same code but exclude this.setVisibleOptionsCount(); calls.

So instead of

function searchLabel(searchValue, virtualSelect) {
  ...
  virtualSelect.setServerOptions(newOptions);
}

I used

function searchLabel(searchValue, virtualSelect) {
  ...
  // --
  /** merging already selected options details with new options */
  if (selectedOptions.length) {
    const newOptionsValueMapping = {};
    optionsUpdated = true;

    newOptions.forEach((d) => {
      newOptionsValueMapping[d.value] = true;
    });

    selectedOptions.forEach((d) => {
      if (newOptionsValueMapping[d.value] !== true) {
        // eslint-disable-next-line no-param-reassign
        d.isVisible = false;
        newOptions.push(d);
      }
    });

    virtualSelect.setOptionsMethod(newOptions, true);
  }

  if (optionsUpdated) {

    if (virtualSelect.multiple) {
      virtualSelect.toggleAllOptionsClass();
    }

    virtualSelect.setValueText();
  } else {
    virtualSelect.updatePosition();
  }

  function removeServerSearchingClass($allWrappers) {
    // Convert $allWrappers to a jQuery object
    const $jqueryAllWrappers = $($allWrappers);
    $jqueryAllWrappers.removeClass('server-searching');
  }

  removeServerSearchingClass(virtualSelect.$allWrappers);
  virtualSelect.visibleOptionsCount = ...; // manually compute the number of visible items
  virtualSelect.afterSetVisibleOptionsCount();
  // --
}

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