Skip to content

Instantly share code, notes, and snippets.

@mistahenry
Last active November 12, 2018 10:47
Show Gist options
  • Save mistahenry/28586b27cbbe6689a07406df396c9821 to your computer and use it in GitHub Desktop.
Save mistahenry/28586b27cbbe6689a07406df396c9821 to your computer and use it in GitHub Desktop.
Ember Light Table based on Ember Data model definition
import Component from '@ember/component';
import { computed } from '@ember/object';
import Table from 'ember-light-table';
export default Component.extend({
init() {
this._super(...arguments);
let table = new Table(this.get('columns'), this.get('models'), { enableSync: this.get('enableSync') });
let sortColumn = table.get('allColumns').findBy('valuePath', this.get('sort'));
// Setup initial sort column
if (sortColumn) {
sortColumn.set('sorted', true);
}
this.set('table', table);
},
columns: computed('modelClass', function() {
let modelClass = this.modelClass;
if(!modelClass){ return [] }
let columns = [];
modelClass.eachAttribute(function(key, meta){
columns.push({
label: key,
valuePath: key
});
});
return columns;
})
});
import Ember from 'ember';
import Foo from 'twiddle/models/foo';
export default Ember.Controller.extend({
init(){
this._super(...arguments);
var record = this.store.createRecord('foo', {
name: "model1",
status: "status1"
});
this.models = [record];
this.modelClass = record.constructor;
},
appName: 'Ember Twiddle'
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
export default Model.extend({
"name": attr('string'),
"status": attr('string')
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{dynamic-table models=models modelClass=modelClass}}
<br>
<br>
{{#light-table table height='65vh' as |t|}}
{{!--
In order for `fa-sort-asc` and `fa-sort-desc` icons to work,
you need to have ember-font-awesome installed or manually include
the font-awesome assets, e.g. via a CDN.
--}}
{{t.head
iconSortable='fa fa-sort'
iconAscending='fa fa-sort-asc'
iconDescending='fa fa-sort-desc'
fixed=true
}}
{{#t.body
canSelect=false
as |body|
}}
{{#if isLoading}}
{{#body.loader}}
{{table-loader}}
{{/body.loader}}
{{/if}}
{{/t.body}}
{{/light-table}}
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2",
"ember-light-table": "1.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment