Skip to content

Commit aeb6bdb

Browse files
committed
workaround: inputs directly inline in table
1 parent 7dcab04 commit aeb6bdb

File tree

5 files changed

+52
-37
lines changed

5 files changed

+52
-37
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ The `config` dictionary has the following options:
5858
| `numColumns` <br/><br/> default: `4` | Number of columns in the table. |
5959
| `rowsName` <br/><br/> default: `Row` | Name of rows in the table. |
6060
| `columnsName` <br/><br/> default: `Column` | Name of columns in the table. |
61+
| `canEditRowHeader` <br/><br/> default: `true` | Make the row headers an input field? |
62+
| `canEditColumnHeader` <br/><br/> default: `false` | Make the column headers an input field? |
6163
| `names` <br/><br/> default: `["Value", "Status"] ` | Array of the names for all fields in a cell. |
6264
| `types` <br/><br/> default: `[Number, Array]` | Array of the types for all fields in a cell. |
6365
| `values` <br/><br/> default: `[0, ["Active", "Inactive"]]` | Array of the types for all fields in a cell. |

data-table/table.css

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ body {
3232
margin: 1px 0;
3333
}
3434
.data-table{
35-
position: absolute;
3635
width: 659px;
3736
height: 228px;
38-
left: 421px;
39-
top: 188px;
37+
margin-top: 30px;
4038
background: #FFFFFF;
4139
border: 1px solid #000000;
4240
box-sizing: border-box;

data-table/table.js

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Config {
2020
* @property {number} numColumns - Number of columns in the table
2121
* @property {string} rowsName - Name of rows in the table
2222
* @property {string} columnsName - Name of columns in the table
23+
* @property {bool} canEditRowHeader - Are the row names editable?
24+
* @property {bool} canEditColumnHeader - Are the column names editable?
2325
* @property {object} tableIds - Container for all the table's magic strings
2426
* @property {object} datumConfig - Container for default values of a cell
2527
*/
@@ -34,6 +36,8 @@ class Config {
3436
this.rowsNamePlural = this.rowsName + "s";
3537
this.columnsNamePlural = this.columnsName + "s";
3638
this.wrapperDivId = clientConfig.wrapperDivId;
39+
this.canEditRowHeader = true;
40+
this.canEditColumnHeader = false;
3741

3842
/**
3943
* @property {string} tableDivId - ID for the div containing the table
@@ -205,22 +209,52 @@ function createColumnHeader(config) {
205209
cell.id = cellIndexToElementId(config.tableIds.theadElementId, 0, colIndex)
206210
cell.classList.add("data-table-cell");
207211

208-
let cellText = colIndex === 0 ? config.rowsNamePlural : config.columnsName + " " + colIndex;
209-
cell.appendChild(document.createTextNode(cellText));
212+
cell.appendChild(createColumnHeaderCell(config, colIndex));
210213
}
211214

212215
config.currNumColumns = config.defaultNumColumns + 1;
213216
config.currNumRows = 1;
214217
}
215218

219+
function createColumnHeaderCell(config, colIndex) {
220+
if (colIndex === 0) {
221+
return document.createTextNode(config.rowsNamePlural);
222+
}
223+
if (!config.canEditColumnHeader) {
224+
let cellText = config.columnsName + " " + colIndex;
225+
return document.createTextNode(cellText);
226+
}
227+
228+
// Creates the field for text input
229+
let input = document.createElement("INPUT");
230+
input.type = 'text';
231+
input.placeholder = config.columnsName;
232+
input.classList.add('table-entry-field');
233+
return input;
234+
}
235+
236+
function createRowHeaderCell(config, rowIndex) {
237+
if (!config.canEditRowHeader) {
238+
let cellText = config.rowsName + " " + rowIndex;
239+
return document.createTextNode(cellText);
240+
}
241+
242+
// Creates the field for text input
243+
let input = document.createElement("INPUT");
244+
input.type = 'text';
245+
input.placeholder = config.rowsName;
246+
input.classList.add('table-entry-field');
247+
return input;
248+
}
249+
216250
/**
217251
* Adds a single column to the table
218252
* @param {object} config - Table configuration object
219253
* @param {String} [content] - Content to place in the top cell of a column (uses default if
220254
* no string is provided)
221255
* @returns {undefined} - Doesn't return anything
222256
*/
223-
function addSingleColumn(config, content) {
257+
function addSingleColumn(config) {
224258
let table = document.getElementById(config.tableIds.tableElementId);
225259
let numRows = config.currNumRows;
226260
let numCols = config.currNumColumns;
@@ -229,10 +263,7 @@ function addSingleColumn(config, content) {
229263
cell.id = cellIndexToElementId(config.tableIds.theadElementId, 0, numCols);
230264
cell.classList.add("data-table-cell");
231265

232-
let text = content || config.columnsName + " " + numCols;
233-
let middle = document.createTextNode(text);
234-
235-
cell.appendChild(middle);
266+
cell.appendChild(createColumnHeaderCell(config, numCols));
236267

237268
for(let rowIndex = 1; rowIndex < numRows; rowIndex++){
238269
createEntryCell(config, table.rows[rowIndex], rowIndex, numCols);
@@ -264,15 +295,15 @@ function deleteSingleColumn(config) {
264295
* no string is provided)
265296
* @returns {undefined} - Doesn't return anything
266297
*/
267-
function addSingleRow(config, content) {
298+
function addSingleRow(config) {
268299
// Insert a row into the body of the table
269-
let rowIndex = config.currNumRows - 1;
270-
let row = document.getElementById(config.tableIds.tbodyElementId).insertRow(rowIndex);
300+
let rowIndex = config.currNumRows;
301+
let row = document.getElementById(config.tableIds.tbodyElementId).insertRow(rowIndex - 1);
271302
// Then for each column
272303
for (let colIndex = 0; colIndex < config.currNumColumns; colIndex++) {
273304
// Create an entry cell
274305
if (colIndex === 0) {
275-
createRowHeader(config, row, rowIndex, colIndex, content)
306+
createRowHeader(config, row, rowIndex, colIndex)
276307
} else {
277308
createEntryCell(config, row, rowIndex, colIndex)
278309
}
@@ -282,17 +313,11 @@ function addSingleRow(config, content) {
282313
}
283314

284315
// eslint-disable-next-line max-params
285-
function createRowHeader(config, row, rowIndex, colIndex, content) {
286-
316+
function createRowHeader(config, row, rowIndex, colIndex) {
287317
let cell = row.insertCell(colIndex);
288318
cell.id = cellIndexToElementId(config.wrapperDivId, rowIndex, colIndex)
289319
cell.classList.add("data-table-cell");
290-
291-
if (content === undefined) {
292-
cell.innerHTML = (rowIndex + 1);
293-
} else {
294-
cell.innerHTML = content;
295-
}
320+
cell.appendChild(createRowHeaderCell(config, rowIndex));
296321
}
297322

298323
/**
@@ -514,28 +539,14 @@ function createColumnEntryBox(config) {
514539
*/
515540
function createRowOrColumnInputAndBtn(config, leftPanelInfo) {
516541
let entryBoxDiv = document.getElementById(config.entryIds.entryBoxDivId);
517-
// Creates the field for text input
518-
let input = document.createElement("INPUT");
519-
input.type = 'text';
520-
input.id = leftPanelInfo.entryID;
521-
input.placeholder = leftPanelInfo.name;
522-
input.classList.add('table-entry-field');
523-
524-
// If the user hits enter while in the text box, click the addButton
525-
input.addEventListener("onClick", function() {
526-
addButton.click();
527-
})
528-
entryBoxDiv.appendChild(input);
529542

530543
// Creates the button that will take the user input and send it to addSingleColumn() when clicked
531544
let addButton = document.createElement("button");
532545
addButton.classList.add("left-panel-button");
533546
addButton.innerHTML = "+ Add " + leftPanelInfo.name.toLowerCase() +
534547
" to " + leftPanelInfo.endDirection;
535548
addButton.onclick = function () {
536-
let value = input.value.trim();
537-
leftPanelInfo.addEntryFunction(config, value);
538-
input.value = '';
549+
leftPanelInfo.addEntryFunction(config);
539550
}
540551
entryBoxDiv.appendChild(addButton);
541552
}

docs/readme-generator.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ The `config` dictionary has the following options:
5858
| `numColumns` <br/><br/> default: `4` | Number of columns in the table. |
5959
| `rowsName` <br/><br/> default: `Row` | Name of rows in the table. |
6060
| `columnsName` <br/><br/> default: `Column` | Name of columns in the table. |
61+
| `canEditRowHeader` <br/><br/> default: `true` | Make the row headers an input field? |
62+
| `canEditColumnHeader` <br/><br/> default: `false` | Make the column headers an input field? |
6163
| `names` <br/><br/> default: `["Value", "Status"] ` | Array of the names for all fields in a cell. |
6264
| `types` <br/><br/> default: `[Number, Array]` | Array of the types for all fields in a cell. |
6365
| `values` <br/><br/> default: `[0, ["Active", "Inactive"]]` | Array of the types for all fields in a cell. |

index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ The `config` dictionary has the following options:
6565
| `numColumns` <br/><br/> default: `4` | Number of columns in the table. |
6666
| `rowsName` <br/><br/> default: `Row` | Name of rows in the table. |
6767
| `columnsName` <br/><br/> default: `Column` | Name of columns in the table. |
68+
| `canEditRowHeader` <br/><br/> default: `true` | Make the row headers an input field? |
69+
| `canEditColumnHeader` <br/><br/> default: `false` | Make the column headers an input field? |
6870
| `names` <br/><br/> default: `["Value", "Status"] ` | Array of the names for all fields in a cell. |
6971
| `types` <br/><br/> default: `[Number, Array]` | Array of the types for all fields in a cell. |
7072
| `values` <br/><br/> default: `[0, ["Active", "Inactive"]]` | Array of the types for all fields in a cell. |

0 commit comments

Comments
 (0)