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

.sortField doesn't work with remote data ? #192

Closed
martinbarilik opened this issue Aug 31, 2021 · 5 comments
Closed

.sortField doesn't work with remote data ? #192

martinbarilik opened this issue Aug 31, 2021 · 5 comments
Labels
bug Something isn't working

Comments

@martinbarilik
Copy link

Hi, remote data settings for tom-select does seem to corrupt original order of json response.

I am using code below:

      let options = {
	      plugins: ['change_listener'],
	      render: {}
      };

      options.sortField = 'obchodne_meno'
      options.valueField = 'obchodne_meno';
      options.labelField = 'obchodne_meno';
      options.searchField = 'obchodne_meno';
      options.load = function (query, callback) {
      let url = `${Routes.my_path()}?str=${encodeURIComponent(query)}`;
      fetch(url).then(response => response.json()).then(json => {
	      callback(json.items);
      }).catch(() => {
	      callback();
      });
      }

      new TomSelect(select, options);

sortField doesn't seem to do anything and i am not setting any order in options. What's the catch ?

@martinbarilik martinbarilik added the bug Something isn't working label Aug 31, 2021
@oyejorge
Copy link
Member

oyejorge commented Sep 6, 2021

Can you give me a little more to work with? Example JSON for a query along with the display you see vs the display you'd like to see?

@oyejorge
Copy link
Member

oyejorge commented Sep 6, 2021

Actually, I think this is what you're looking for

...
 fetch(url).then(response => response.json()).then(json => {
     this.clearOptions(); // <-- clear options from previous queries
     callback(json.items);
}).catch(() => {
...

@martinbarilik
Copy link
Author

Sorry, it didn't work.

Here is the example: https://jsfiddle.net/mArtinko5MB/k7nbsf5a/59/

What is expected: Write "ale" into selectbox so it starts request.

expected order is:

  • "Alena Tsomethingova"
  • "Alena Komethingova"
  • "Alena Zanythingá"

But it isn't ( it's Zanythingá, Komethingova, Tsomethingova).

The funny thing is that when i change values ( names ) to Tsomething, Ksomething, Zsomething and write "ale" into SB, it suddenly works and order is as expected. I have no idea what is going on. Bug is somehow connected to how string is computed in sort function.

@oyejorge
Copy link
Member

oyejorge commented Sep 7, 2021

Shoot, that's right.

Unless present, a special $score field will be automatically added to the beginning of the sort list. This will make results sorted primarily by match quality (descending).

The solution is to make sure $score is in the sortField setting:

 sortField: [{field:'name'},{field:'$score'}],

or

 sortField: [{field:'$order'},{field:'$score'}],

@martinbarilik
Copy link
Author

Great, it works. Thanks !

@oyejorge oyejorge closed this as completed Sep 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants