cell and cellinfo input linked

This commit is contained in:
2018-10-16 13:32:42 +08:00
parent ea3b9d82c9
commit 3f36e1b922
2 changed files with 62 additions and 31 deletions

View File

@@ -3,19 +3,24 @@ class Table {
columnLength: number; columnLength: number;
selectedRow: string; selectedRow: string;
selectedCell: string; selectedCell: string;
constructor(rowLength: number, columnLength: number) { instanceOfCells: any;
constructor(rowLength: number, columnLength: number, instanceOfCells: any) {
this.rowLength = rowLength; this.rowLength = rowLength;
this.columnLength = columnLength; this.columnLength = columnLength;
this.instanceOfCells = instanceOfCells;
this.selectedRow; this.selectedRow;
this.selectedCell; this.selectedCell;
} }
render(cell) { render() {
// clear children elements // clear children elements
const allCells = []; console.log("i am table render");
var cellIndex = -1;
var myNode = document.getElementById("table"); var myNode = document.getElementById("table");
while (myNode.firstChild) { while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild); myNode.removeChild(myNode.firstChild);
} }
// loop through rowlength and collentgh // loop through rowlength and collentgh
for (var i = 0; i < this.rowLength; i++) { for (var i = 0; i < this.rowLength; i++) {
var table: HTMLTableElement = <HTMLTableElement>( var table: HTMLTableElement = <HTMLTableElement>(
@@ -25,19 +30,16 @@ class Table {
for (var j = 0; j < this.columnLength; j++) { for (var j = 0; j < this.columnLength; j++) {
var letter = String.fromCharCode("A".charCodeAt(0) + j - 1); var letter = String.fromCharCode("A".charCodeAt(0) + j - 1);
const instanceOfCell = new cell("cell" + letter + i);
const newCell = row.insertCell(-1); const instanceOfCell = this.instanceOfCells[(cellIndex += 1)];
newCell.id = "cell" + letter + i; const cellElement = instanceOfCell.cellElement();
// allCells.push(instanceOfCell); row.appendChild(cellElement);
newCell.addEventListener("click", () => {
instanceOfCell.setColspan(5); cellElement.addEventListener("dblclick", () => {
console.log(instanceOfCell);
this.showCellDetails(instanceOfCell); this.showCellDetails(instanceOfCell);
}); });
i && j cellElement.innerHTML = i && j ? instanceOfCell.cellId : i || letter;
? ((newCell.colSpan = cell.colspan), (newCell.innerHTML = letter + i))
: (newCell.innerHTML = i.toString() || letter);
} }
} }
} }
@@ -53,50 +55,73 @@ class Table {
const cellLiElement = document.createElement("li"); const cellLiElement = document.createElement("li");
const cellLableElement = document.createElement("label"); const cellLableElement = document.createElement("label");
const cellInputElement = document.createElement("input"); const cellInputElement = document.createElement("input");
cellInputElement.setAttribute("id", instanceOfCell.cellId);
const cellButtonElement = document.createElement("button");
cellButtonElement.innerHTML = "确定";
// cellButtonElement.onclick = function() {
// const inputElement = document.getElementById(instanceOfCell.cellId);
// };
cellLableElement.innerHTML = key; cellLableElement.innerHTML = key;
cellInputElement.setAttribute("value", instanceOfCell[key]); cellInputElement.setAttribute("value", instanceOfCell[key]);
cellInputElement.setAttribute("name", key);
cellInputElement.setAttribute("class", "cell-detail");
cellLiElement.appendChild(cellLableElement); cellLiElement.appendChild(cellLableElement);
cellLiElement.appendChild(cellInputElement); cellLiElement.appendChild(cellInputElement);
cellLiElement.appendChild(cellButtonElement);
cellUlElement.appendChild(cellLiElement); cellUlElement.appendChild(cellLiElement);
} }
} }
cellInfoElement.appendChild(cellUlElement);
const cellButtonElement = document.createElement("button");
cellButtonElement.innerHTML = "确定";
cellButtonElement.onclick = () => {
const inputElements = document.getElementsByClassName("cell-detail");
for (const key in instanceOfCell) {
// type script error
for (const input of inputElements) {
if (key === input.name) {
instanceOfCell[key] = input.value;
}
}
}
this.render();
};
cellInfoElement.appendChild(cellUlElement);
cellInfoElement.appendChild(cellButtonElement);
} }
changeCellDetails(instanceOfCell) {}
} }
class Cell { class Cell {
cellId: string; cellId: string;
border: number; border: string;
color: string; color: string;
width: number; width: number;
colspan: number; colspan: string;
constructor(id) { constructor(id) {
this.cellId = id; this.cellId = id;
this.border = 1; this.border = "";
this.color = "grey"; this.color = "";
this.width = 80; this.width = 80;
this.colspan = 1; this.colspan = "1";
} }
setColor(newColor: string) { setColor(newColor: string) {
this.color = newColor; this.color = newColor;
} }
setBorder(newBorder: number) { setBorder(newBorder: string) {
this.border = newBorder; this.border = newBorder;
} }
setColspan(newColspan: number) { setColspan(newColspan: string) {
this.colspan = newColspan; this.colspan = newColspan;
} }
cellElement() {
let tdElement = document.createElement("td");
tdElement.setAttribute("colspan", this.colspan);
tdElement.style.backgroundColor = this.color;
tdElement.style.width = this.width + "px";
tdElement.style.border = this.border + "px solid";
tdElement.innerHTML = this.cellId;
return tdElement;
}
} }
export { Table, Cell }; export { Table, Cell };

View File

@@ -1,6 +1,7 @@
import { Table, Cell } from "./classes"; import { Table, Cell } from "./classes";
const submit = document.getElementById("submitRowCol"); const submit = document.getElementById("submitRowCol");
const instanceOfCells = [];
submit.addEventListener( submit.addEventListener(
"click", "click",
@@ -9,8 +10,13 @@ submit.addEventListener(
const totalRow = Number(rowElement.value); const totalRow = Number(rowElement.value);
const colElement = <HTMLInputElement>document.getElementById("totalCol"); const colElement = <HTMLInputElement>document.getElementById("totalCol");
const totalCol = Number(colElement.value); const totalCol = Number(colElement.value);
const table = new Table(totalRow, totalCol);
table.render(Cell); for (let i = 0; i < totalRow * totalCol; i++) {
const newCell = new Cell(i.toString());
instanceOfCells.push(newCell);
}
const table = new Table(totalRow, totalCol, instanceOfCells);
table.render();
}, },
false false
); );