Ember-cli-fsg-list
ember-cli-fsg-list is an EmberCLI addon.
It is a filterable, sortable, and groupable Ember component.
You can also assign custom action to the items in the list.
It is really helpful when you want to create something like this:
Example:
npm install ember-cli-fsg-list --save-dev
- In your controller 'app/controllers/demo.js':
var DemoController = Ember.Controller.extend({
list: [
{id: 1 , name: 'Chun Yang' , occupation: 'Web Developer'} ,
{id: 2 , name: 'Dummy A' , occupation: 'Web Developer'} ,
{id: 3 , name: 'Dummy B' , occupation: 'QA'} ,
{id: 4 , name: 'Ellen Degeneres' , occupation: 'QA'} ,
{id: 5 , name: 'Jackie Chan' , occupation: 'Manager'} ,
].map(function(obj){
return Ember.Object.create(obj);
}),
itemPartial: 'person',
filterTerm: null,
filterKeys: ['name'],
filterFn: function(item){
return item.get('id') > this.get('filterTerm');
},
sortKeys: ['occupation', 'id:asc', 'name:desc'],
sortFn: function(a, b){
return a.get('id') - b.get('id');
},
groupFn: function(item){
return item.occupation;
},
groupTitleAttrs: ['titleImageUrl'],
titleImageUrl: function(title){
return 'http://' + title + '.com';
},
actions: {
logToConsole: function(item){
console.log('The item you clicked is: ', item);
}
}
});
- In your template 'app/templates/demo.hbs'
{{input value=filterTerm placeholder="name"}}
<ul>
{{fsg-list
list = list
itemPartial = itemPartial
filterTerm = filterTerm
filterBy = filterKeys
sortBy = sortKeys
groupBy = groupFn
groupTitleAttrs = groupTitleAttrs
actionName = 'logToConsole'
titleImageUrl = titleImageUrl
}}
</ul>
- In your partial template 'app/template/person.hbs'
<li class="item">
{{#if item._isTitle}}
<span class="title">{{item._title}}</span>
<img {{bind-attr src=item.titleImageUrl}}/>
{{else}}
<div {{action '_selectItem' item}}>
<span class="id">{{item.id}}</span>
(<span class="occupation">{{item.occupation}}</span>)
<span class="name">{{item.name}}</span>
</div>
{{/if}}
</li>
Component variables:
- list: Ember array of Ember objects
- itemPartial: string, template name
- filterTerm: string
- filterBy: array of strings OR function
- sortBy: array of strings OR function
- groupBy: a function
- groupTitleAttrs: array of strings
- actionName: string, action name in your controller
Emitted variables from the component to partial template:
- item: Ember object, an object from the input list
- item._isTitle: boolean, if this item is a group title
- item._title: string, output of the group function
- _selectItem: function, used to bubble up the action to your controller