Last active
October 25, 2020 04:39
-
-
Save bantic/906c5c50bdeae3d9012eb9a1c3e4b66e to your computer and use it in GitHub Desktop.
Ember Table Fixed Footer
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 Ember from 'ember'; | |
const alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''); | |
function makeRows(count, colCount, text='') { | |
return Array(count).fill().map((v,i) => { | |
return Array(colCount).fill().map((v,j) => alpha[j]).reduce((obj, letter) => { | |
obj[letter] = `${letter} ${text} ${i}`; | |
return obj; | |
}, {}); | |
}); | |
} | |
export default Ember.Controller.extend({ | |
columns: Ember.computed('colCount', 'fixedCol', function() { | |
let names = Array(parseInt(this.colCount)).fill().map((v,i) => alpha[i]); | |
let cols = names.map(name => { | |
return {name, valuePath:name}; | |
}); | |
if (this.get('fixedCol')) { | |
cols[0].isFixed = 'left'; | |
} | |
return cols; | |
}), | |
bodyRowCount: 30, | |
footRowCount: 10, | |
colCount: 5, | |
fixedCol: false, | |
bodyRows: Ember.computed('bodyRowCount', 'colCount', function() { | |
return makeRows(parseInt(this.get('bodyRowCount')),this.get('colCount'),'body'); | |
}), | |
footRows: Ember.computed('footRowCount', 'colCount', function() { | |
return makeRows(parseInt(this.get('footRowCount')),this.get('colCount'),'foot'); | |
}), | |
_setMaxFootRows(count) { | |
let tfoot = document.querySelector('tfoot'); | |
let itemHeight = tfoot.querySelector('tr').getBoundingClientRect().height; | |
tfoot.querySelectorAll('tr').forEach((tr, trIndex) => { | |
let bottom = (count - trIndex) * itemHeight; | |
tr.querySelectorAll('td').forEach(td => { | |
if (bottom < 0) { | |
td.style.position = ''; | |
td.style.bottom = ''; | |
} else { | |
td.style.position = 'sticky'; | |
td.style.bottom = `${bottom}px`; | |
} | |
}); | |
}); | |
}, | |
actions: { | |
setRows(bodyCount, footCount) { | |
this.set('bodyRowCount', parseInt(bodyCount)); | |
this.set('footRowCount', parseInt(footCount)); | |
}, | |
updateBodyCount(count) { | |
console.log(count); | |
if (Number.isNaN(parseInt(count))) { return; } | |
this.send('setRows', count, this.get('footRowCount')); | |
}, | |
updateFootCount(count) { | |
if (Number.isNaN(parseInt(count))) { return; } | |
this.send('setRows', this.get('bodyRowCount'), count); | |
}, | |
updateColCount(count) { | |
if (Number.isNaN(parseInt(count))) { return; } | |
this.set('colCount', parseInt(count)); | |
}, | |
toggleFixedCol() { | |
this.toggleProperty('fixedCol'); | |
}, | |
setMaxFootRows(count) { | |
if (Number.isNaN(parseInt(count))) { return; } | |
this._setMaxFootRows(parseInt(count)); | |
} | |
} | |
}); |
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
body { | |
margin: 1em; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
input { | |
width: 5em; | |
text-align: center; | |
font-family: sans-serif; | |
} | |
.table-container { | |
outline: 1px solid red; | |
height: 500px; | |
width: 400px; | |
} | |
thead th { | |
background-color: LightGreen; | |
} | |
tbody td { | |
background-color: lightpink; | |
} | |
tfoot td { | |
background-color: lightblue; | |
} | |
.fixed-col th:first-child, .fixed-col td:first-child { | |
border-right: 2px dotted black; | |
} |
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
{ | |
"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-table": "2.1.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment