Created
          November 28, 2017 06:26 
        
      - 
      
 - 
        
Save ibare/06ac68ea6d6fbae33c942128d387a6c3 to your computer and use it in GitHub Desktop.  
    Woowahan table plugin sample
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import Woowahan from 'woowahan'; | |
| import template from './main.handlebars'; | |
| export const Main = Woowahan.View.create('Main', { | |
| template, | |
| initialize() { | |
| this.setModel({ | |
| myList: [ | |
| { all: '부각용', wait: '7', ad: '10', deal: '8' }, | |
| { all: '광선검', wait: '17', ad: '32', deal: '18' }, | |
| { all: '홍길동', wait: '2', ad: '1', deal: '3' }, | |
| { all: '옴하하', wait: '11', ad: '3', deal: '1' }, | |
| ] | |
| }); | |
| this.super(); | |
| } | |
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import Woowahan from 'woowahan'; | |
| import mainView from './main-view'; | |
| import { tablePlugins } from './table-plugin'; | |
| global.$ = global.jQuery = Woowahan.$; | |
| const app = new Woowahan(); | |
| app.use([ | |
| Woowahan.Plugin('tablePlugin', tablePlugins), | |
| ]); | |
| app.start({ | |
| url: '/', | |
| view: mainView, | |
| container: '.container' | |
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | export const tablePlugins = function(element, collection) { | |
| if (!Array.isArray(collection)) return; | |
| let columns = {}; | |
| let tds = Array.from(element.querySelectorAll('thead th')); | |
| tds.forEach(td => { | |
| let { columnName, align = 'left', format = '$v' } = td.dataset; | |
| columns[columnName] = { | |
| align, format | |
| }; | |
| }); | |
| let rows = collection.map(row => { | |
| let tdString = Object.keys(columns).map(colName => { | |
| return `<td align="${columns[colName].align}">${columns[colName].format.replace('$v', row[colName])}</td>`; | |
| }).join('\n'); | |
| return `<tr>${tdString}</tr>`; | |
| }).join('\n'); | |
| element.querySelector('tbody').innerHTML = rows; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment