Skip to content

Commit a92aa13

Browse files
committed
Fix row selection when column names are shown
Add documentation to DataTable kind Add example
1 parent 41d1309 commit a92aa13

File tree

5 files changed

+107
-29
lines changed

5 files changed

+107
-29
lines changed

Table/DataTable.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
.maklesoft-datatable .maklesoft-datatable-cell {
55
border: solid 1px;
6+
min-width: 70px;
67
}
78

89
.maklesoft-datatable td {
@@ -48,7 +49,7 @@ column names
4849
Row with an odd row number
4950
*/
5051
.maklesoft-datatable .maklesoft-datatable-odd {
51-
background-color: Gray;
52+
background-color: #eee;
5253
}
5354

5455
/*
@@ -61,6 +62,6 @@ Selected row
6162
/*
6263
Startpoint for range selection
6364
*/
64-
.maklesoft-datatable .maklesoft-datatable-rangeselection {
65+
.maklesoft-datatable .maklesoft-datatable-rangeselection .maklesoft-datatable-cell {
6566
background-color: Lime;
6667
}

Table/DataTable.js

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,63 @@ Copyright (c) by MaKleSoft
44
This code is provided "as is", without any kind of warranty.
55
http://www.maklesoft.com
66
*/
7+
/**
8+
A control that is designed to display and/or collect tabular data. This implementation uses the maklesoft.Table kind for displaying the data. Aside from editing cells and deleting and adding rows and columns
9+
this control also supports various selection modes and dynamical styling of individual cells
10+
based on indizes and values.
11+
12+
## properties
13+
* rowCount: The number of rows the table should have. If no row count is specified the length of the data
14+
array is used
15+
Default 0
16+
* colCount: The number of columns the table should have. If no column count is specified the maximum
17+
column length of the data array is used
18+
Default 0
19+
* editable: A boolean that specifies whether or not the cells should be editable. If true, row and column
20+
selection requires a double tap instead of a single tap
21+
Default false
22+
* data: A two dimensional array containing the data to be displayed. Omit this property to get a blank table
23+
but dont forget to set the row and column count and make the table editable!
24+
Default [[]]
25+
* selectionMode: Determines the selection behaviour of the table. There are five different selection modes:
26+
* maklesoft.DataTable.SelectionModes.SINGLE_ROW: One row can be selected at a time
27+
* maklesoft.DataTable.SelectionModes.MULTI_ROW: Multiple rows can be selected at a time
28+
* maklesoft.DataTable.SelectionModes.SINGLE_COLUMN: One column can be selected at a time
29+
* maklesoft.DataTable.SelectionModes.MULTI_COLUMN: Multiple columns can be selected at a time
30+
* maklesoft.DataTable.SelectionModes.NONE: No selection possible
31+
Default: maklesoft.DataTable.SelectionModes.NONE
32+
* showRowNumbers: Whether or not to display the row numbers
33+
Default: true
34+
* showColumnNames: Whether or not to use column names
35+
Default: false
36+
* columnNames: An array specifying the column names to be used. If this property is omitted the columns will
37+
be labeled alphabetically
38+
* cellClass: This can either be a string, specifying a generic class name for all cells or a function that returns a class name depending on the row index, column index and data of the cell. For more information, see 'Dynamic styling'.
39+
40+
## Selection
41+
The VirtualTable kind supports various selection modes for selecting single or multiple rows or columns.
42+
If the table is not editable the user can select a row/column by tapping/clicking on it. If the table is
43+
editable a double click/tab is required.
44+
Range selection is possible by tapping/clicking a row and holding it and then tapping/clicking on another row
45+
in order to select all rows between both. Range selection is only possible in the MULTI_ROW mode.
46+
47+
## Adding and deleting rows columns
48+
Rows and columns can be added or deleted at a given index or based on user selection. For more information inspect the
49+
documentation of the respective methods.
50+
51+
## Data manipulation
52+
The tables data can be manipulated programmatically or through user input. Once the editing is done the data can be
53+
conveniently received using getData()
54+
55+
## Dynamic styling
56+
Custom functions can be provided for the cellClass property in order to dynamically style cells. This function should
57+
take the column and row index and the value of the cell and decide which css class to apply to the cell. For example the default
58+
cellClass function returns the class name "number" if the value in the cell is a number:
59+
function(colIndex, rowIndex, data) {
60+
if (typeof(data) == "number")
61+
return "number";
62+
}
63+
*/
764
enyo.kind({
865
name: "maklesoft.DataTable",
966
kind: "Control",
@@ -87,7 +144,7 @@ enyo.kind({
87144

88145
var className = typeof this.cellClass == "function" ? this.cellClass(rowIndex, colIndex, data) : this.cellClass;
89146
var cellConfig = {
90-
kind: "HFlexBox", name: "cell_" + rowIndex + "_" + colIndex, onclick: "cellClick", ondblclick: "cellDblclick", onmouseheld: "cellMouseheld", className: className, components: [{kind: "BasicInput", value: data, disabled: !this.editable, rowIndex: rowIndex, colIndex: colIndex, name: "input_" + rowIndex + "_" + colIndex, flex: 1, style: "width: 100%; padding: 0px 10px 0px 10px", hint: "", onchange: "inputChanged"}]
147+
kind: "HFlexBox", name: "cell_" + rowIndex + "_" + colIndex, onclick: "cellClick", ondblclick: "cellDblclick", onmousehold: "cellMouseheld", className: className, components: [{kind: "BasicInput", value: data, disabled: !this.editable, rowIndex: rowIndex, colIndex: colIndex, name: "input_" + rowIndex + "_" + colIndex, flex: 1, style: "width: 100%; padding: 0px 10px 0px 10px", hint: "", onchange: "inputChanged"}]
91148
};
92149

93150
if (((this.selectionMode == maklesoft.DataTable.SelectionMode.SINGLE_COLUMN || this.selectionMode == maklesoft.DataTable.SelectionMode.MULTI_COLUMN) && this.$.selection.isSelected(colIndex)) ||
@@ -110,15 +167,15 @@ enyo.kind({
110167
//if the range selection has allready been triggered with another startindex
111168
if (this.rangeSelectionStartIndex) {
112169
//remove the marker from the old start index row+
113-
this.$.table.getRow(this.rangeSelectionStartIndex).removeClass("maklesoft-datatable-rangeselection");
170+
this.$.table.fetchRow(this.rangeSelectionStartIndex).removeClass("maklesoft-datatable-rangeselection");
114171
}
115172
//Highlight the row at the start index
116-
this.$.table.getRow(startindex).addClass("maklesoft-datatable-rangeselection");
173+
this.$.table.fetchRow(startIndex).addClass("maklesoft-datatable-rangeselection");
117174
this.rangeSelectionStartIndex = startIndex;
118175
},
119176
endRangeSelection: function(endIndex) {
120177
var startIndex = this.rangeSelectionStartIndex;
121-
this.$.table.getRow(startIndex).removeClass("maklesoft-datatable-rangeselection");
178+
this.$.table.fetchRow(startIndex).removeClass("maklesoft-datatable-rangeselection");
122179
for (var i = Math.min(startIndex, endIndex); i <= Math.max(startIndex, endIndex); i++) {
123180
this.$.selection.select(i);
124181
}
@@ -127,12 +184,13 @@ enyo.kind({
127184
},
128185
selectRow: function(index) {
129186
this.$.selection.select(index);
187+
var rowOffset = this.showColumnNames ? 1 : 0;
130188
//this.$.list.refresh();
131189
//Directly change the selected rows styling instead of refreshing for better performance
132-
this.$.table.getRow(index).addRemoveClass("maklesoft-datatable-selected", this.$.selection.isSelected(index));
190+
this.$.table.fetchRow(index + rowOffset).addRemoveClass("maklesoft-datatable-selected", this.$.selection.isSelected(index));
133191
//But then we have to make sure that the last selected rows styling is also changed when in SINGLE_ROW selection mode
134192
if (this.selectionMode == maklesoft.DataTable.SelectionMode.SINGLE_ROW && (this.lastSelected !== undefined)) {
135-
this.$.table.getRow(this.lastSelected).removeClass("maklesoft-datatable-selected");
193+
this.$.table.fetchRow(this.lastSelected + rowOffset).removeClass("maklesoft-datatable-selected");
136194
}
137195
this.lastSelected = index;
138196
},

Table/DataTableExample.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
/*
2+
@author Martin Kleinschrodt
3+
Copyright (c) by MaKleSoft
4+
This code is provided "as is", without any kind of warranty.
5+
http://www.maklesoft.com
6+
*/
17
enyo.kind({
28
name: "DataTableExample",
39
kind: "VFlexBox",
410
components: [{
511
kind: "Header",
6-
components: [{content: "ScrollingVirtualTable example <br> <span style='color:gray; font-size: 12pt'>Brought to you by MaKleSoft</span>", flex: 1},
12+
components: [{content: "DataTable example - <a href='https://github.com/MaKleSoft/enyo-plugins/tree/master/Table'>Get the code on Github</a><br> <span style='color:gray; font-size: 12pt'>Brought to you by MaKleSoft</span>", flex: 1},
713
{kind: "Button", caption: "load data", onclick: "loadData"}]
814
},{
915
kind: "HFlexBox", flex: 1, components: [{
10-
kind: "Scroller", flex: 1, components: [{
16+
kind: "BasicScroller", horizontal: true, autoHorizontal: false, flex: 1, components: [{
1117
kind: "maklesoft.DataTable",
1218
name: "table",
1319
rowCount: 50,

Table/Table.js

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*
2+
@author Martin Kleinschrodt
3+
Copyright (c) by MaKleSoft
4+
This code is provided "as is", without any kind of warranty.
5+
http://www.maklesoft.com
6+
*/
17
enyo.kind({
28
name: "maklesoft.Table",
39
kind: enyo.Control,
@@ -18,23 +24,31 @@ enyo.kind({
1824
build: function() {
1925
this.destroyControls();
2026
var row, cell, config;
27+
2128
for (var i=0; i<this.rowCount; i++) {
2229
row = this.createComponent({
2330
kind: "Control", nodeTag: "tr"
2431
});
32+
2533
for (var j=0; j<this.colCount; j++) {
2634
cell = row.createComponent({
2735
kind: "Control", nodeTag: "td"
2836
});
2937

30-
config = this.doSetupCell(i, j) || {};
31-
config = enyo.isArray(config) ? config : [config];
38+
config = this.doSetupCell(i, j) || "";
3239

33-
if (this.shouldDecorateCells) {
34-
this.decorateCell(config, i, j);
35-
}
40+
// Provide the ability to fill the cells with plain content instead of another control.
41+
if (typeof config == "string" || typeof config == "number") {
42+
cell.setContent(config);
43+
} else {
44+
config = enyo.isArray(config) ? config : [config];
45+
46+
if (this.shouldDecorateCells) {
47+
this.decorateCell(config, i, j);
48+
}
3649

37-
cell.createComponents(config, {owner: this.owner});
50+
cell.createComponents(config, {owner: this.owner});
51+
}
3852
}
3953
}
4054
},
@@ -44,22 +58,21 @@ enyo.kind({
4458
c.colIndex = col;
4559
}
4660
},
47-
getRow: function(rowIndex) {
61+
//* @public
62+
/**
63+
* Fetch the row control at the given index
64+
* @param {int} index
65+
* The index of the row
66+
*/
67+
fetchRow: function(rowIndex) {
4868
return this.getControls()[rowIndex];
4969
},
70+
//* @public
71+
/**
72+
* Rebuild the table.
73+
*/
5074
refresh: function() {
5175
this.build();
5276
this.render();
5377
}
54-
});
55-
56-
enyo.kind({
57-
name: "TableTest",
58-
kind: "Control",
59-
setupCell: function(row, col) {
60-
return {content: "cell " + row + ", " + col};
61-
},
62-
components: [
63-
{kind: "maklesoft.Table", onSetupCell: "setupCell", colCount: 5, rowCount: 5}
64-
]
6578
});

Table/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<html>
22
<head>
3-
<title>VirtualTable example</title>
3+
<title>DataTable example</title>
44
<meta name="viewport" content="width=device-width initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
55
<meta name="apple-mobile-web-app-capable" content="yes">
66
<script src="../../enyo/framework/enyo.js" type="text/javascript"></script>

0 commit comments

Comments
 (0)