-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.js
62 lines (54 loc) · 1.94 KB
/
grid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
let rows = 100;
let cols = 26;
const addressColCont = document.querySelector(".address-col-cont");
for (let i = 0; i < rows; i++) {
let addressCol = document.createElement("div");
addressCol.setAttribute("class", "address-col");
addressCol.innerText = i + 1;
addressColCont.appendChild(addressCol);
}
const addressRowCont = document.querySelector(".address-row-cont");
for (let i = 0; i < cols; i++) {
let addressRow = document.createElement("div");
addressRow.setAttribute("class", "address-row");
addressRow.innerText = String.fromCharCode(i + 65);
addressRowCont.appendChild(addressRow);
}
const addressCellsCont = document.querySelector(".cells-cont");
for (let i = 0; i < rows; i++) {
let addressRow = document.createElement("div");
addressRow.setAttribute("class", "address-row-cells");
for (let j = 0; j < cols; j++) {
let addressCell = document.createElement("div");
addressCell.setAttribute("class", "cell");
addressCell.setAttribute("contenteditable", "true");
addressCell.setAttribute("spellcheck", false);
addressCell.setAttribute("rid",i);
addressCell.setAttribute("cid",j);
addressRow.appendChild(addressCell);
if (i === 0 && j == 0) {
addressCell.setAttribute("id", "selectable");
}
addEventListenerForAddressBarCont(addressCell, i, j);
}
addressCellsCont.appendChild(addressRow);
}
function addEventListenerForAddressBarCont(cell, i, j) {
const addressBarCont = document.querySelector(".address-bar");
if (i === 0 && j == 0) {
addressBarCont.value = "1A";
}
cell.addEventListener("click", () => {
const address =String.fromCharCode(j + 65) + (i + 1) ;
addressBarCont.value = address;
});
}
// For selecting first cell
const el = document.getElementById("selectable");
const selection = window.getSelection();
const range = document.createRange();
selection.removeAllRanges();
range.selectNodeContents(el);
range.collapse(false);
selection.addRange(range);
el.focus();