Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Faux occlusion with Ember-Power-Select
Michal Bryxí
Michal Bryxí

Posted on

     

Faux occlusion with Ember-Power-Select

The problem

Ember-Power-Select is a very powerfulEmberJS addon for creating drop-down selects.

Recently I had an interesting case that I needed to have all options loaded client-side and the number of options was not small (several thousands). At this point I have learned that the component does not have any form ofocclusion rendering, so that all the thousand options would just be spit into the DOM. Which is not great.

After few iterations I found a decent solution:

The solution

// index.hbs<PowerSelect@selected={{@selected}}@onChange={{@onChange}}@options={{this.initialSet}}@searchEnabled={{true}}@search={{this.search}}as|item|>{{item.name}}</PowerSelect>
Enter fullscreen modeExit fullscreen mode
// index.jsimportComponentfrom"@glimmer/component";import{action}from"@ember/object";exportdefaultclassMySelectComponentextendsComponent{// Maximum number of items to be displayed in the drop-down.MAX_ITEMS=100;// All the items before any filter is applied.getinitialSet(){// We use `null` filter, to limit the number// of items in initial list to `MAX_ITEMS`return[...this.filter(null)];}// Our component receives all the possible options// as `items` argument.getallItems(){returnthis.args.items;}// Search action invoked by the PowerSelect.@actionsearch(needle){return[...this.filter(needle)];}// We're going to filter options using a generator*filter(needle){const{allItems,maxSize}=this;letcount=0;leti=0;// Iterate over all items from initial set.// Stop when we found maximum number of items// or when we went through the whole initial set.while(count<this.MAX_ITEMS&&i<allItems.length){// If there is no search term, every item matches.// Otherwise test current item against given needle.if(!needle||this.check(allItems[i],needle)){// If we found a match, yield it.yieldallItems[i];count++;}i++;}}// Function that will test whether item matches needlecheck(item,needle){// For simplicity let's say each item has property// `name` and we just check for a substring match.if(item.name.includes(needle)){returntrue;}}}
Enter fullscreen modeExit fullscreen mode

Conclusion

With this approach I'm able to have all the options loaded client-side and display only relatively small portion in the Ember-Power-Select dropdown.

It is worth noting that this approach is suitable only for quite a narrow amount of items. At one point the payload from the server would be too big to transfer / filter on efficiently. Use your own judgement. I myself would look for different approach for bigger numbers than lower thousands of items.


Photo byWade Austin Ellis onUnsplash

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
iamdtang profile image
David Tang
  • Location
    Los Angeles
  • Joined

Nice! Have you tried usingvertical-collection as theoptionsComponent in Power Select?

CollapseExpand
 
michalbryxi profile image
Michal Bryxí
Cycle 🚴 , climb 🗻 , run 🏃 , travel 🌍 , enjoy life ♥.IT guy with the need to live fully.
  • Email
  • Location
    Interlaken, Switzerland
  • Education
    University of West Bohemia
  • Pronouns
    he/him
  • Work
    Sandcastle Architect
  • Joined

Yes. First I've seencibernox's own integration attempt which seems unmaintained. So it felt like there is some problem with it. And later on when I tried to integrate it myself, there really was some hard caveat that I can't remember right now 🤔

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Cycle 🚴 , climb 🗻 , run 🏃 , travel 🌍 , enjoy life ♥.IT guy with the need to live fully.
  • Location
    Interlaken, Switzerland
  • Education
    University of West Bohemia
  • Pronouns
    he/him
  • Work
    Sandcastle Architect
  • Joined

More fromMichal Bryxí

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp