Skip to content

Commit 82582a0

Browse files
authored
Merge pull request #205 from iamejaaz/sticky-column
feat: add support for sticky columns
2 parents f05049c + 007a7bf commit 82582a0

File tree

8 files changed

+149
-58
lines changed

8 files changed

+149
-58
lines changed

cypress/integration/cell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('Cell', function () {
6161
cy.clickCell(2, 0)
6262
.type('{ctrl}{rightarrow}');
6363
cy.get('.dt-cell--focus')
64-
.should('have.class', 'dt-cell--7-0');
64+
.should('have.class', 'dt-cell--11-0');
6565
});
6666

6767
it('cell selection using shift + arrow keys', function () {

cypress/integration/column.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,35 @@ describe('Column', function () {
2929
.its('currentSort.colIndex')
3030
.should('eq', 2);
3131

32-
cy.get('.dt-scrollable .dt-row:first')
32+
cy.get('.dt-scrollable .dt-row:first div:nth-of-type(3)')
3333
.contains('Airi Satou');
3434

3535
cy.clickDropdownItem(2, 'Reset sorting');
3636
});
3737

3838
it('removes column using dropdown action', function () {
39-
cy.get('.dt-cell--header').should('have.length', 8);
39+
cy.get('.dt-cell--header').should('have.length', 12);
4040

4141
cy.clickDropdown(5);
4242
cy.clickDropdownItem(5, 'Remove column');
4343

44-
cy.get('.dt-cell--header').should('have.length', 7);
44+
cy.get('.dt-cell--header').should('have.length', 11);
4545
});
4646

4747
it('resize column with mouse drag', function () {
48-
cy.get('.dt-cell--header-3 .dt-cell__resize-handle').as('resize-handle');
48+
cy.get('.dt-cell--header-4 .dt-cell__resize-handle').as('resize-handle');
4949
cy
5050
.get('@resize-handle')
5151
.trigger('mousedown')
52-
.trigger('mousemove', { pageX: 510, pageY: 20, which: 1 })
52+
.trigger('mousemove', { pageX: 700, pageY: 20, which: 1 })
5353
.trigger('mouseup');
5454

55-
cy.getColumnCell(3)
56-
.should('have.css', 'width')
57-
.and('match', /13\dpx/);
58-
cy.getCell(3, 1)
59-
.should('have.css', 'width')
60-
.and('match', /13\dpx/);
55+
cy.getColumnCell(4).invoke('css', 'width').then((width) => {
56+
cy.getColumnCell(4)
57+
.should('have.css', 'width', width);
58+
cy.getCell(4, 1)
59+
.should('have.css', 'width', width);
60+
});
6161
});
6262

6363
it('resize column using double click', function () {

index.html

Lines changed: 80 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,85 @@ <h1>Frappe DataTable</h1>
151151

152152
function buildData() {
153153
columns = [
154-
{ name: "Name" },
155-
{ name: "Position" },
156-
{ name: "Office" },
154+
{ name: "Name", width: 150, sticky: true },
155+
{ name: "Position", width: 200 },
156+
{ name: "Office", sticky: true },
157157
{ name: "Extn." },
158-
{
159-
name: "Start Date",
160-
format: (value) => (value || '').split('/').reverse().join('/'),
161-
compareValue: (cell, keyword) => {
162-
const keywordValue = keyword.split('/').reverse().join('/')
163-
return [+new Date(cell.content), +new Date(keywordValue)];
164-
}
165-
},
166-
{ name: "Salary", format: value => formatMoney(value) }
158+
{
159+
name: "Start Date",
160+
format: (value) => (value || '').split('/').reverse().join('/'),
161+
compareValue: (cell, keyword) => {
162+
const keywordValue = keyword.split('/').reverse().join('/')
163+
return [+new Date(cell.content), +new Date(keywordValue)];
164+
}
165+
},
166+
{ name: "Salary", format: value => formatMoney(value) },
167+
{ name: "Department", width:200},
168+
{ name: "Country"},
169+
{ name: "Start date"},
170+
{ name: "Bonus amount"}
171+
167172
];
168-
data = [["Tiger Nixon", { content: "System Architect<br>New line", editable: false }, "Edinburgh", 5421, "2011/04/25", 320800], ["Garrett Winters", "Accountant", "Tokyo", 8422, "2011/07/25", 170750], ["Ashton Cox", "Junior Technical Author", "San Francisco", 1562, "2009/01/12", 86000], ["Cedric Kelly", "Senior Javascript Developer", "Edinburgh", 6224, "2012/03/29", 433060], ["Airi Satou", "Accountant", "Tokyo", 5407, "2008/11/28", 162700], ["Brielle Williamson", "Integration Specialist", "New York", 4804, "2012/12/02", 372000], ["Herrod Chandler", "Sales Assistant", "San Francisco", 9608, "2012/08/06", 137500], ["Rhona Davidson", "Integration Specialist", "Tokyo", 6200, "2010/10/14", 327900], ["Colleen Hurst", "Javascript Developer", "San Francisco", 2360, "2009/09/15", 205500], ["Sonya Frost", "Software Engineer", "Edinburgh", 1667, "2008/12/13", 103600], ["Jena Gaines", "Office Manager", "London", 3814, "2008/12/19", 90560], ["Quinn Flynn", "Support Lead", "Edinburgh", 9497, "2013/03/03", 342000], ["Charde Marshall", "Regional Director", "San Francisco", 6741, "2008/10/16", 470600], ["Haley Kennedy", "Senior Marketing Designer", "London", 3597, "2012/12/18", 313500], ["Tatyana Fitzpatrick", "Regional Director", "London", 1965, "2010/03/17", 385750], ["Michael Silva", "Marketing Designer", "London", 1581, "2012/11/27", 198500], ["Paul Byrd", "Chief Financial Officer (CFO)", "New York", 3059, "2010/06/09", 725000], ["Gloria Little", "Systems Administrator", "New York", 1721, "2009/04/10", 237500], ["Bradley Greer", "Software Engineer", "London", 2558, "2012/10/13", 132000], ["Dai Rios", "Personnel Lead", "Edinburgh", 2290, "2012/09/26", 217500], ["Jenette Caldwell", "Development Lead", "New York", 1937, "2011/09/03", 345000], ["Yuri Berry", "Chief Marketing Officer (CMO)", "New York", 6154, "2009/06/25", 675000], ["Caesar Vance", "Pre-Sales Support", "New York", 8330, "2011/12/12", 106450], ["Doris Wilder", "Sales Assistant", "Sidney", 3023, "2010/09/20", 85600], ["Angelica Ramos", "Chief Executive Officer (CEO)", "London", 5797, "2009/10/09", 1200000], ["Gavin Joyce", "Developer", "Edinburgh", 8822, "2010/12/22", 92575], ["Jennifer Chang", "Regional Director", "Singapore", 9239, "2010/11/14", 357650], ["Brenden Wagner", "Software Engineer", "San Francisco", 1314, "2011/06/07", 206850], ["Fiona Green", "Chief Operating Officer (COO)", "San Francisco", 2947, "2010/03/11", 850000], ["Shou Itou", "Regional Marketing", "Tokyo", 8899, "2011/08/14", 163000], ["Michelle House", "Integration Specialist", "Sidney", 2769, "2011/06/02", 95400], ["Suki Burks", "Developer", "London", 6832, "2009/10/22", 114500], ["Prescott Bartlett", "Technical Author", "London", 3606, "2011/05/07", 145000], ["Gavin Cortez", "Team Leader", "San Francisco", 2860, "2008/10/26", 235500], ["Martena Mccray", "Post-Sales support", "Edinburgh", 8240, "2011/03/09", 324050], ["Unity Butler", "Marketing Designer", "San Francisco", 5384, "2009/12/09", 85675], ["Howard Hatfield", "Office Manager", "San Francisco", 7031, "2008/12/16", 164500], ["Hope Fuentes", "Secretary", "San Francisco", 6318, "2010/02/12", 109850], ["Vivian Harrell", "Financial Controller", "San Francisco", 9422, "2009/02/14", 452500], ["Timothy Mooney", "Office Manager", "London", 7580, "2008/12/11", 136200], ["Jackson Bradshaw", "Director", "New York", 1042, "2008/09/26", 645750], ["Olivia Liang", "Support Engineer", "Singapore", 2120, "2011/02/03", 234500], ["Bruno Nash", "Software Engineer", "London", 6222, "2011/05/03", 163500], ["Sakura Yamamoto", "Support Engineer", "Tokyo", 9383, "2009/08/19", 139575], ["Thor Walton", "Developer", "New York", 8327, "2013/08/11", 98540], ["Finn Camacho", "Support Engineer", "San Francisco", 2927, "2009/07/07", 87500], ["Serge Baldwin", "Data Coordinator", "Singapore", 8352, "2012/04/09", 138575], ["Zenaida Frank", "Software Engineer", "New York", 7439, "2010/01/04", 125250], ["Zorita Serrano", "Software Engineer", "San Francisco", 4389, "2012/06/01", 115000], ["Jennifer Acosta", "Junior Javascript Developer", "Edinburgh", 3431, "2013/02/01", 75650], ["Cara Stevens", "Sales Assistant", "New York", 3990, "2011/12/06", 145600], ["Hermione Butler", "Regional Director", "London", 1016, "2011/03/21", 356250], ["Lael Greer", "Systems Administrator", "London", 6733, "2009/02/27", 103500], ["Jonas Alexander", "Developer", "San Francisco", 8196, "2010/07/14", 86500], ["Shad Decker", "Regional Director", "Edinburgh", 6373, "2008/11/13", 183000], ["Michael Bruce", "Javascript Developer", "Singapore", 5384, "2011/06/27", 183000], ["Donna Snider", "Customer Support", "New York", 4226, "2011/01/25", 112000], ["Donna Snider", "Customer Support", "LOS Angeles", 42.67, "2011/01/25", 112000]];
173+
data = [
174+
["Tiger Nixon", { content: "System Architect<br>New line", editable: false }, "Edinburgh", 5421, "2011/04/25", 320800, "Engineering", "Germany", "2015/03/18", 10000],
175+
["Garrett Winters", "Accountant", "Tokyo", 8422, "2011/07/25", 170750, "Finance", "USA", "2016/05/22", 8000],
176+
["Ashton Cox", "Junior Technical Author", "San Francisco", 1562, "2009/01/12", 86000, "Documentation", "Canada", "2017/11/03", 3000],
177+
["Cedric Kelly", "Senior Javascript Developer", "Edinburgh", 6224, "2012/03/29", 433060, "Engineering", "Australia", "2014/07/09", 15000],
178+
["Airi Satou", "Accountant", "Tokyo", 5407, "2008/11/28", 162700, "Finance", "Japan", "2012/02/12", 5000],
179+
["Brielle Williamson", "Integration Specialist", "New York", 4804, "2012/12/02", 372000, "Engineering", "USA", "2015/08/14", 11000],
180+
["Herrod Chandler", "Sales Assistant", "San Francisco", 9608, "2012/08/06", 137500, "Sales", "Germany", "2011/04/11", 2000],
181+
["Rhona Davidson", "Integration Specialist", "Tokyo", 6200, "2010/10/14", 327900, "IT", "Singapore", "2013/01/19", 12000],
182+
["Colleen Hurst", "Javascript Developer", "San Francisco", 2360, "2009/09/15", 205500, "Engineering", "France", "2012/06/08", 4000],
183+
["Sonya Frost", "Software Engineer", "Edinburgh", 1667, "2008/12/13", 103600, "Engineering", "USA", "2014/10/30", 9000],
184+
["Jena Gaines", "Office Manager", "London", 3814, "2008/12/19", 90560, "Operations", "England", "2016/09/22", 1500],
185+
["Quinn Flynn", "Support Lead", "Edinburgh", 9497, "2013/03/03", 342000, "Customer Support", "Ireland", "2011/04/30", 7000],
186+
["Charde Marshall", "Regional Director", "San Francisco", 6741, "2008/10/16", 470600, "Management", "USA", "2012/02/17", 30000],
187+
["Haley Kennedy", "Senior Marketing Designer", "London", 3597, "2012/12/18", 313500, "Marketing", "UK", "2013/07/04", 12000],
188+
["Tatyana Fitzpatrick", "Regional Director", "London", 1965, "2010/03/17", 385750, "Management", "Canada", "2011/11/08", 25000],
189+
["Michael Silva", "Marketing Designer", "London", 1581, "2012/11/27", 198500, "Design", "UK", "2014/05/02", 4000],
190+
["Paul Byrd", "Chief Financial Officer (CFO)", "New York", 3059, "2010/06/09", 725000, "Finance", "USA", "2012/03/11", 50000],
191+
["Gloria Little", "Systems Administrator", "New York", 1721, "2009/04/10", 237500, "IT", "USA", "2015/09/13", 6000],
192+
["Bradley Greer", "Software Engineer", "London", 2558, "2012/10/13", 132000, "Engineering", "Canada", "2013/01/22", 8000],
193+
["Dai Rios", "Personnel Lead", "Edinburgh", 2290, "2012/09/26", 217500, "HR", "Germany", "2014/04/15", 4000],
194+
["Jenette Caldwell", "Development Lead", "New York", 1937, "2011/09/03", 345000, "Engineering", "USA", "2012/12/05", 22000],
195+
["Yuri Berry", "Chief Marketing Officer (CMO)", "New York", 6154, "2009/06/25", 675000, "Marketing", "USA", "2013/10/18", 60000],
196+
["Caesar Vance", "Pre-Sales Support", "New York", 8330, "2011/12/12", 106450, "Sales", "USA", "2014/11/11", 4000],
197+
["Doris Wilder", "Sales Assistant", "Sidney", 3023, "2010/09/20", 85600, "Sales", "Australia", "2015/08/13", 2000],
198+
["Angelica Ramos", "Chief Executive Officer (CEO)", "London", 5797, "2009/10/09", 1200000, "Management", "UK", "2012/09/04", 100000],
199+
["Gavin Joyce", "Developer", "Edinburgh", 8822, "2010/12/22", 92575, "Engineering", "Canada", "2015/06/17", 7000],
200+
["Jennifer Chang", "Regional Director", "Singapore", 9239, "2010/11/14", 357650, "Management", "Singapore", "2014/09/09", 15000],
201+
["Brenden Wagner", "Software Engineer", "San Francisco", 1314, "2011/06/07", 206850, "Engineering", "USA", "2013/12/22", 5000],
202+
["Fiona Green", "Chief Operating Officer (COO)", "San Francisco", 2947, "2010/03/11", 850000, "Operations", "USA", "2014/02/08", 70000],
203+
["Shou Itou", "Regional Marketing", "Tokyo", 8899, "2011/08/14", 163000, "Marketing", "Japan", "2013/10/04", 9000],
204+
["Michelle House", "Integration Specialist", "Sidney", 2769, "2011/06/02", 95400, "Engineering", "Australia", "2014/01/17", 6000],
205+
["Suki Burks", "Developer", "London", 6832, "2009/10/22", 114500, "Engineering", "UK", "2015/11/07", 7000],
206+
["Prescott Bartlett", "Technical Author", "London", 3606, "2011/05/07", 145000, "Documentation", "England", "2014/03/11", 3000],
207+
["Gavin Cortez", "Team Leader", "San Francisco", 2860, "2008/10/26", 235500, "Management", "USA", "2012/02/25", 18000],
208+
["Martena Mccray", "Post-Sales support", "Edinburgh", 8240, "2011/03/09", 324050, "Support", "Scotland", "2014/08/21", 5000],
209+
["Unity Butler", "Marketing Designer", "San Francisco", 5384, "2009/12/09", 85675, "Design", "USA", "2013/06/28", 1500],
210+
["Howard Hatfield", "Office Manager", "San Francisco", 7031, "2008/12/16", 164500, "Operations", "Canada", "2014/09/02", 2000],
211+
["Hope Fuentes", "Secretary", "San Francisco", 6318, "2010/02/12", 109850, "Administration", "USA", "2015/07/24", 4000],
212+
["Vivian Harrell", "Financial Controller", "San Francisco", 9422, "2009/02/14", 452500, "Finance", "USA", "2014/01/10", 15000],
213+
["Timothy Mooney", "Office Manager", "London", 7580, "2008/12/11", 136200, "Operations", "UK", "2012/10/25", 2000],
214+
["Jackson Bradshaw", "Director", "New York", 1042, "2008/09/26", 645750, "Management", "USA", "2013/06/04", 50000],
215+
["Olivia Liang", "Support Engineer", "Singapore", 2120, "2011/02/03", 234500, "Support", "Singapore", "2013/05/01", 4000],
216+
["Bruno Nash", "Software Engineer", "London", 6222, "2011/05/03", 163500, "Engineering", "England", "2014/11/11", 7000],
217+
["Sakura Yamamoto", "Support Engineer", "Tokyo", 9383, "2009/08/19", 139575, "Support", "Japan", "2013/12/03", 3000],
218+
["Thor Walton", "Developer", "New York", 8327, "2013/08/11", 98540, "Engineering", "USA", "2014/09/14", 4000],
219+
["Finn Camacho", "Support Engineer", "San Francisco", 2927, "2009/07/07", 87500, "Support", "USA", "2013/05/28", 2000],
220+
["Serge Baldwin", "Data Coordinator", "Singapore", 8352, "2012/04/09", 138575, "IT", "Singapore", "2014/03/10", 6000],
221+
["Zenaida Frank", "Software Engineer", "New York", 7439, "2010/01/04", 125250, "Engineering", "USA", "2012/12/15", 3000],
222+
["Zorita Serrano", "Software Engineer", "San Francisco", 4389, "2012/06/01", 115000, "Engineering", "Canada", "2014/06/25", 2000],
223+
["Jennifer Acosta", "Junior Javascript Developer", "Edinburgh", 3431, "2013/02/01", 75650, "Engineering", "UK", "2015/03/13", 1000],
224+
["Cara Stevens", "Sales Assistant", "New York", 3990, "2011/12/06", 145600, "Sales", "USA", "2014/02/21", 2000],
225+
["Hermione Butler", "Regional Director", "London", 1016, "2011/03/21", 356250, "Management", "UK", "2013/11/16", 50000],
226+
["Lael Greer", "Systems Administrator", "London", 6733, "2009/02/27", 103500, "IT", "UK", "2014/12/08", 4000],
227+
["Jonas Alexander", "Developer", "San Francisco", 8196, "2010/07/14", 86500, "Engineering", "USA", "2012/05/18", 1500],
228+
["Shad Decker", "Regional Director", "Edinburgh", 6373, "2008/11/13", 183000, "Management", "Scotland", "2013/07/10", 25000],
229+
["Michael Bruce", "Javascript Developer", "Singapore", 5384, "2011/06/27", 183000, "Engineering", "Singapore", "2014/02/15", 5000],
230+
["Donna Snider", "Customer Support", "New York", 4226, "2011/01/25", 112000, "Support", "USA", "2013/12/07", 2000],
231+
["Donna Snider", "Customer Support", "Los Angeles", 42.67, "2011/01/25", 112000, "Support", "USA", "2013/11/25", 1000]
232+
]
169233

170234
if (largeData) {
171235
for (let i = 0; i < 10; i++) {
@@ -183,7 +247,7 @@ <h1>Frappe DataTable</h1>
183247
var datatable = new DataTable('section', {
184248
checkboxColumn: true,
185249
serialNoColumn: true,
186-
layout: 'fluid',
250+
layout: 'fixed', // fluid / fixed
187251
columns,
188252
data,
189253
inlineFilters: true,
@@ -283,8 +347,8 @@ <h1>Frappe DataTable</h1>
283347
function appendRows() {
284348
datatable.appendRows(
285349
[
286-
["Garrett", "Accountant", "Tokyo", 8422, "2011/07/25", 170],
287-
["Winters", "Accountant", "Tokyo", 8422, "2011/07/25", 123]
350+
["Garrett", "Accountant", "Tokyo", 8422, "2011/07/25", 170, "USA", "2013/10/25", 2000],
351+
["Winters", "Accountant", "Tokyo", 8422, "2011/07/25", 123, "Scotland", "2013/11/25", 1000]
288352
]
289353
)
290354
}

src/body-renderer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export default class BodyRenderer {
123123
this.rowmanager.highlightCheckedRows();
124124
this.cellmanager.selectAreaOnClusterChanged();
125125
this.cellmanager.focusCellOnClusterChanged();
126+
this.bodyScrollable.style.removeProperty('overflow');
126127
}
127128

128129
showToastMessage(message, hideAfter) {

src/cellmanager.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export default class CellManager {
2525
]);
2626

2727
this.bindEvents();
28+
this.stickyRowWidth = 0;
29+
this.stickyColWitdh = [];
2830
}
2931

3032
bindEvents() {
@@ -800,10 +802,40 @@ export default class CellManager {
800802
isTotalRow
801803
});
802804

805+
let styles = '';
806+
803807
const row = this.datamanager.getRow(rowIndex);
804808

805809
const isBodyCell = !(isHeader || isFilter || isTotalRow);
806810

811+
const serialNoColIndex = !this.options.checkboxColumn && this.options.serialNoColumn ? 0 : 1;
812+
813+
let sticky = false;
814+
815+
if (colIndex === 0 && this.options.checkboxColumn) {
816+
if (cell.isHeader && !(cell.id in this.stickyColWitdh)) this.stickyRowWidth = 33;
817+
sticky = true;
818+
} else if (colIndex === serialNoColIndex && this.options.serialNoColumn) {
819+
if (cell.isHeader && !(cell.id in this.stickyColWitdh)) {
820+
this.stickyColWitdh[cell.id] = this.stickyRowWidth;
821+
this.stickyRowWidth += (cell.width || 32);
822+
}
823+
styles = `left:${this.stickyColWitdh[isBodyCell ? cell.column.id : cell.id]}px;`;
824+
sticky = true;
825+
826+
} else if (cell.sticky) {
827+
if (cell.isHeader && !(cell.id in this.stickyColWitdh)) {
828+
this.stickyColWitdh[cell.id] = this.stickyRowWidth;
829+
this.stickyRowWidth += (cell.width || 100);
830+
}
831+
styles = `left:${this.stickyColWitdh[cell.id]}px;`;
832+
sticky = true;
833+
834+
} else if (isBodyCell && cell.column.sticky) {
835+
styles = `left:${this.stickyColWitdh[cell.column.id]}px;`;
836+
sticky = true;
837+
}
838+
807839
const className = [
808840
'dt-cell',
809841
'dt-cell--col-' + colIndex,
@@ -812,11 +844,12 @@ export default class CellManager {
812844
isHeader ? 'dt-cell--header' : '',
813845
isHeader ? `dt-cell--header-${colIndex}` : '',
814846
isFilter ? 'dt-cell--filter' : '',
815-
isBodyCell && (row && row.meta.isTreeNodeClose) ? 'dt-cell--tree-close' : ''
847+
isBodyCell && (row && row.meta.isTreeNodeClose) ? 'dt-cell--tree-close' : '',
848+
sticky ? 'dt-sticky-col' : ''
816849
].join(' ');
817850

818851
return `
819-
<div class="${className}" ${dataAttr} tabindex="0">
852+
<div class="${className}" ${dataAttr} tabindex="0" style="${styles}">
820853
${this.getCellContent(cell)}
821854
</div>
822855
`;

src/datatable.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ class DataTable {
109109
prepareDom() {
110110
this.wrapper.innerHTML = `
111111
<div class="datatable" dir="${this.options.direction}">
112-
<div class="dt-header"></div>
113-
<div class="dt-scrollable"></div>
114-
<div class="dt-footer"></div>
112+
<div class="datatable-content">
113+
<div class="dt-header"></div>
114+
<div class="dt-scrollable"></div>
115+
<div class="dt-footer"></div>
116+
</div>
115117
<div class="dt-freeze">
116118
<span class="dt-freeze__message">
117119
${this.options.freezeMessage}

0 commit comments

Comments
 (0)