突出显示表格左下角的单元格(三角形)

赛义夫

我有一个基于提供的行数和列数动态创建的html表。我已经成功创建了表格,现在我想用浅红色突出显示表格(三角形)左下半部分的单元格。与附件文件https://www.screencast.com/t/Va4Xz4v4相同

我用于创建表的代码

//Event handler function
function print_table() {
  let _tblRows, _tblCols, tblElm, rowElm, colElm, randNmbrArray, _tblDiv, _randNmbrAvg, avgElm;

  _tblRows = document.getElementById('rows').value;
  _tblCols = document.getElementById('cols').value;
  randNmbrArray = [];
  _tblDiv = document.getElementById('my_table')
  avgElm = document.getElementById('average');

  if (_tblRows == "") {
    alert("Please enter rows!");
  } else if (_tblCols == "") {
    alert("Please enter columns!");
  } else {
    tblElm = document.createElement('table');

    for (var i = 0; i < _tblRows; i++) {
      rowElm = document.createElement('tr');
      for (var j = 0; j < _tblCols; j++) {
        let _randNmbr = Math.floor(Math.random() * 100) + 1;
        randNmbrArray.push(_randNmbr);
        colElm = document.createElement('td');
        colElm.appendChild(document.createTextNode(_randNmbr));
        rowElm.appendChild(colElm);

      }
      tblElm.appendChild(rowElm);
    }
    _tblDiv.innerHTML = "";
    _tblDiv.append(tblElm);

    _randNmbrAvg = GetAverage(randNmbrArray);
    avgElm.innerHTML = "";
    avgElm.append(`The average of the number in the table is ${_randNmbrAvg.toFixed(2)}`);

  }
}

function GetAverage(numberArray) {
  let total = 0;
  for (var i = 0; i < numberArray.length; i++) {
    total += numberArray[i];
  }
  return total / numberArray.length;
}
table {
  border-collapse: collapse;
  margin: auto 25px auto 25px;
}

table, td, th {
  border: 1px solid #70AEC5;
}

td {
  padding: 3px;
}

th {
  border: 1px solid white;
  background-color: #70AEC5;
  color: white;
  text-align: center;
  padding: 4px 0 4px 0;
}

tr:hover {
  background: #ddd
}

h1 {
  color: #70AEC5;
}

#directions {
  border-radius: 25px;
  border: 2px solid #70AEC5;
  padding: 10px;
  margin: 10px 25px 15px 25px;
}

button {
  background-color: #4CAF50;
  /* Green */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 20px;
  cursor: pointer;
  border-radius: 8px;
}

.triangle {
  background-color: #ffcccc;
}
<h1>Generating a Table</h1>
<h4>By Yukong Zhang</h4>
Rows: <input id="rows" type="text" size="3" value="15"> Columns: <input id="cols" type="text" size="3" value="15">
<button id="print" type="button" onclick="print_table()">Generate</button><br>
<div id="my_table"></div>
<h4 id="average"></h4>

现在我只想突出显示桌子(三角形)的下半部分。

易卜拉欣·马尔里尔

基本上,您必须比较单元格xy对于方阵,单元格的左下三角形匹配条件x <= y,左上角匹配y <= numberOfColumns - x,依此类推...

因此,要突出显示左下角的单元格,只需将triangle添加到每个通过条件的单元格中j <= ii即当前行j的索引和当前列的索引)。

由于生成的矩阵并不总是正方形(列数与行数不同),因此我们必须对xy坐标进行归一化,为此,我们只需将x坐标除以列数,再将y坐标除以行数(这样两个标准化坐标都可以从01)。因此,我们强调符合条件的细胞j / (_tblCols - 1) <= i / (_tblRows - 1)(从两个数列和行减1至账户的事实,索引ij开始在0)。

在内部循环中添加:

if(j / (_tblCols - 1) <= i / (_tblRows - 1)) {
    colElm.className = "triangle";
}

演示:

//Event handler function
function print_table() {
  let _tblRows, _tblCols, tblElm, rowElm, colElm, randNmbrArray, _tblDiv, _randNmbrAvg, avgElm;

  _tblRows = document.getElementById('rows').value;
  _tblCols = document.getElementById('cols').value;
  randNmbrArray = [];
  _tblDiv = document.getElementById('my_table')
  avgElm = document.getElementById('average');

  if (_tblRows == "") {
    alert("Please enter rows!");
  } else if (_tblCols == "") {
    alert("Please enter columns!");
  } else {
    tblElm = document.createElement('table');

    for (var i = 0; i < _tblRows; i++) {
      rowElm = document.createElement('tr');
      for (var j = 0; j < _tblCols; j++) {
        let _randNmbr = Math.floor(Math.random() * 100) + 1;
        randNmbrArray.push(_randNmbr);
        colElm = document.createElement('td');
        colElm.appendChild(document.createTextNode(_randNmbr));
        rowElm.appendChild(colElm);

        if(j / (_tblCols - 1) <= i / (_tblRows - 1)) {
          colElm.className = "triangle";
        }
      }
      tblElm.appendChild(rowElm);
    }
    _tblDiv.innerHTML = "";
    _tblDiv.append(tblElm);

    _randNmbrAvg = GetAverage(randNmbrArray);
    avgElm.innerHTML = "";
    avgElm.append(`The average of the number in the table is ${_randNmbrAvg.toFixed(2)}`);

  }
}

function GetAverage(numberArray) {
  let total = 0;
  for (var i = 0; i < numberArray.length; i++) {
    total += numberArray[i];
  }
  return total / numberArray.length;
}
table {
  border-collapse: collapse;
  margin: auto 25px auto 25px;
}

table, td, th {
  border: 1px solid #70AEC5;
}

td {
  padding: 3px;
}

th {
  border: 1px solid white;
  background-color: #70AEC5;
  color: white;
  text-align: center;
  padding: 4px 0 4px 0;
}

tr:hover {
  background: #ddd
}

h1 {
  color: #70AEC5;
}

#directions {
  border-radius: 25px;
  border: 2px solid #70AEC5;
  padding: 10px;
  margin: 10px 25px 15px 25px;
}

button {
  background-color: #4CAF50;
  /* Green */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 20px;
  cursor: pointer;
  border-radius: 8px;
}

.triangle {
  background-color: #ffcccc;
}
<h1>Generating a Table</h1>
<h4>By Yukong Zhang</h4>
Rows: <input id="rows" type="text" size="3" value="15"> Columns: <input id="cols" type="text" size="3" value="15">
<button id="print" type="button" onclick="print_table()">Generate</button><br>
<div id="my_table"></div>
<h4 id="average"></h4>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章