2018-10-15 15:21:18 +08:00
|
|
|
class Table {
|
|
|
|
rowLength: number;
|
|
|
|
columnLength: number;
|
|
|
|
selectedRow: string;
|
|
|
|
selectedCell: string;
|
2018-10-16 13:32:42 +08:00
|
|
|
instanceOfCells: any;
|
|
|
|
constructor(rowLength: number, columnLength: number, instanceOfCells: any) {
|
2018-10-15 15:21:18 +08:00
|
|
|
this.rowLength = rowLength;
|
|
|
|
this.columnLength = columnLength;
|
2018-10-16 13:32:42 +08:00
|
|
|
this.instanceOfCells = instanceOfCells;
|
2018-10-15 15:21:18 +08:00
|
|
|
this.selectedRow;
|
|
|
|
this.selectedCell;
|
|
|
|
}
|
2018-10-16 13:32:42 +08:00
|
|
|
render() {
|
2018-10-15 15:21:18 +08:00
|
|
|
// clear children elements
|
2018-10-16 13:32:42 +08:00
|
|
|
console.log("i am table render");
|
|
|
|
var cellIndex = -1;
|
|
|
|
|
2018-10-15 15:21:18 +08:00
|
|
|
var myNode = document.getElementById("table");
|
|
|
|
while (myNode.firstChild) {
|
|
|
|
myNode.removeChild(myNode.firstChild);
|
|
|
|
}
|
2018-10-16 13:32:42 +08:00
|
|
|
|
2018-10-16 16:32:22 +08:00
|
|
|
// loop through rowlength and collength
|
2018-10-15 15:21:18 +08:00
|
|
|
for (var i = 0; i < this.rowLength; i++) {
|
|
|
|
var table: HTMLTableElement = <HTMLTableElement>(
|
|
|
|
document.getElementById("table")
|
|
|
|
);
|
|
|
|
var row = table.insertRow(-1);
|
2018-10-19 17:34:51 +08:00
|
|
|
console.log(table);
|
2018-10-15 15:21:18 +08:00
|
|
|
|
|
|
|
for (var j = 0; j < this.columnLength; j++) {
|
|
|
|
var letter = String.fromCharCode("A".charCodeAt(0) + j - 1);
|
2018-10-16 13:32:42 +08:00
|
|
|
|
|
|
|
const instanceOfCell = this.instanceOfCells[(cellIndex += 1)];
|
|
|
|
const cellElement = instanceOfCell.cellElement();
|
|
|
|
row.appendChild(cellElement);
|
|
|
|
|
|
|
|
cellElement.addEventListener("dblclick", () => {
|
2018-10-15 15:21:18 +08:00
|
|
|
this.showCellDetails(instanceOfCell);
|
|
|
|
});
|
|
|
|
|
2018-10-16 13:32:42 +08:00
|
|
|
cellElement.innerHTML = i && j ? instanceOfCell.cellId : i || letter;
|
2018-10-15 15:21:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
showCellDetails(instanceOfCell) {
|
|
|
|
var cellInfoElement = document.getElementById("cellInfo");
|
|
|
|
while (cellInfoElement.firstChild) {
|
|
|
|
cellInfoElement.removeChild(cellInfoElement.firstChild);
|
|
|
|
}
|
|
|
|
const cellUlElement = document.createElement("ul");
|
|
|
|
|
|
|
|
for (const key in instanceOfCell) {
|
|
|
|
if (instanceOfCell.hasOwnProperty(key)) {
|
|
|
|
const cellLiElement = document.createElement("li");
|
|
|
|
const cellLableElement = document.createElement("label");
|
|
|
|
const cellInputElement = document.createElement("input");
|
|
|
|
|
|
|
|
cellLableElement.innerHTML = key;
|
2018-10-16 13:32:42 +08:00
|
|
|
|
2018-10-15 15:21:18 +08:00
|
|
|
cellInputElement.setAttribute("value", instanceOfCell[key]);
|
2018-10-16 13:32:42 +08:00
|
|
|
cellInputElement.setAttribute("name", key);
|
|
|
|
cellInputElement.setAttribute("class", "cell-detail");
|
2018-10-15 15:21:18 +08:00
|
|
|
|
|
|
|
cellLiElement.appendChild(cellLableElement);
|
|
|
|
cellLiElement.appendChild(cellInputElement);
|
|
|
|
cellUlElement.appendChild(cellLiElement);
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 13:32:42 +08:00
|
|
|
|
|
|
|
const cellButtonElement = document.createElement("button");
|
|
|
|
cellButtonElement.innerHTML = "确定";
|
|
|
|
cellButtonElement.onclick = () => {
|
|
|
|
const inputElements = document.getElementsByClassName("cell-detail");
|
|
|
|
for (const key in instanceOfCell) {
|
2018-10-16 16:32:22 +08:00
|
|
|
// type script error---------------------------
|
2018-10-18 17:35:01 +08:00
|
|
|
// for (const input of inputElements) {
|
|
|
|
// if (key === input.name) {
|
|
|
|
// instanceOfCell[key] = input.value;
|
|
|
|
// }
|
|
|
|
// }
|
2018-10-16 13:32:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.render();
|
|
|
|
};
|
2018-10-15 15:21:18 +08:00
|
|
|
cellInfoElement.appendChild(cellUlElement);
|
2018-10-16 13:32:42 +08:00
|
|
|
cellInfoElement.appendChild(cellButtonElement);
|
2018-10-15 15:21:18 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 13:32:42 +08:00
|
|
|
|
2018-10-15 15:21:18 +08:00
|
|
|
class Cell {
|
|
|
|
cellId: string;
|
2018-10-16 13:32:42 +08:00
|
|
|
border: string;
|
2018-10-15 15:21:18 +08:00
|
|
|
color: string;
|
|
|
|
width: number;
|
2018-10-16 13:32:42 +08:00
|
|
|
colspan: string;
|
2018-10-16 16:32:22 +08:00
|
|
|
fontSize: string;
|
2018-10-15 15:21:18 +08:00
|
|
|
constructor(id) {
|
|
|
|
this.cellId = id;
|
2018-10-16 13:32:42 +08:00
|
|
|
this.border = "";
|
|
|
|
this.color = "";
|
2018-10-15 15:21:18 +08:00
|
|
|
this.width = 80;
|
2018-10-16 13:32:42 +08:00
|
|
|
this.colspan = "1";
|
2018-10-16 16:32:22 +08:00
|
|
|
this.fontSize = "12";
|
2018-10-15 15:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setColor(newColor: string) {
|
|
|
|
this.color = newColor;
|
|
|
|
}
|
2018-10-16 13:32:42 +08:00
|
|
|
setBorder(newBorder: string) {
|
2018-10-15 15:21:18 +08:00
|
|
|
this.border = newBorder;
|
|
|
|
}
|
2018-10-16 13:32:42 +08:00
|
|
|
setColspan(newColspan: string) {
|
2018-10-15 17:39:28 +08:00
|
|
|
this.colspan = newColspan;
|
2018-10-15 15:21:18 +08:00
|
|
|
}
|
2018-10-16 16:32:22 +08:00
|
|
|
setFontSize(newFontSize: string) {
|
|
|
|
this.fontSize = newFontSize;
|
|
|
|
}
|
|
|
|
|
2018-10-16 13:32:42 +08:00
|
|
|
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";
|
2018-10-16 16:32:22 +08:00
|
|
|
tdElement.style.fontSize = this.fontSize + "px";
|
2018-10-16 13:32:42 +08:00
|
|
|
tdElement.innerHTML = this.cellId;
|
|
|
|
|
|
|
|
return tdElement;
|
|
|
|
}
|
2018-10-15 15:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export { Table, Cell };
|