Aligning decimal points in HTML

后端 未结 11 1760
渐次进展
渐次进展 2020-11-27 12:52

I have a table containing decimal numbers in one column. I\'m looking to align them in a manner similar to a word processor\'s \"decimal tab\" feature, so that all the poin

11条回答
  •  青春惊慌失措
    2020-11-27 13:38

    See this article by Krijn Hoetmer for your options and how to achieve this. The essence of this solution is to use CSS and JS to achieve this:

    (function() {
      var currencies = /(\$|€|€)/;
      var leftWidth = 0, rightWidth = 0;
      for(var tableCounter = 0, tables = document.getElementsByTagName("table");
          tableCounter < tables.length; tableCounter++) {
        if(tables[tableCounter].className.indexOf("fix-align-char") != -1) {
          var fCols = [], leftPart, rightPart, parts;
          for(var i = 0, cols = tables[tableCounter].getElementsByTagName("col"); i < cols.length; i++) {
            if(cols[i].getAttribute("char")) {
              fCols[i] = cols[i].getAttribute("char");
            }
          }
          for(var i = 0, trs = tables[tableCounter].rows; i < trs.length; i++) {
            for(var j = 0, tds = trs[i].getElementsByTagName("td"); j < tds.length; j++) {
              if(fCols[j]) {
                if(tds[j].innerHTML.indexOf(fCols[j]) != -1) {
                  parts = tds[j].innerHTML.split(fCols[j]);
                  leftPart = parts.slice(0, parts.length -1).join(fCols[j]);
                  leftPart = leftPart.replace(currencies, "$1");
                  rightPart = fCols[j] + parts.pop();
                  tds[j].innerHTML = "" + leftPart + "" + rightPart + "";
                } else {
                  tds[j].innerHTML = tds[j].innerHTML.replace(currencies, "$1");
                  tds[j].innerHTML = "" + tds[j].innerHTML + "";
                }
                tds[j].className = "char-align";
                var txt = document.createTextNode(tds[j].firstChild.offsetWidth);
                if(leftWidth < tds[j].firstChild.offsetWidth) {
                  leftWidth = tds[j].firstChild.offsetWidth;
                }
                if(tds[j].childNodes[1]) {
                  txt = document.createTextNode(tds[j].childNodes[1].offsetWidth);
                  if(rightWidth < tds[j].childNodes[1].offsetWidth) {
                    rightWidth = tds[j].childNodes[1].offsetWidth;
                  }
                }
              }
            }
          }
        }
      }
      // This is ugly and should be improved (amongst other parts of the code ;)
      var styleText = "\n" +
          "\n";
      document.body.innerHTML += styleText;
    })();
    table {
      border-collapse: collapse;
      width: 600px;
    }
    th {
      padding: .5em;
      background: #eee;
      text-align: left;
    }
    td {
      padding: .5em;
    }
    #only-css td.char-align {
      width: 7em;
    }
    #only-css span.left {
      float: left;
      width: 4em;
      text-align: right;
    }
    #only-css span.currency {
      float: left;
      width: 2em;
      text-align: left;
    }
    #only-css span.right {
      float: right;
      width: 3em;
      text-align: left;
    }
    Number Description Costs
    1 Lorem ipsum dolor sit amet $3 ,99
    2 Consectetuer adipiscing elit $13 ,95
    3 Pellentesque fringilla nisl ac mi $4
    4 Aenean egestas gravida magna $123 ,999

提交回复
热议问题