【问题标题】:How to truncate strings by placing an ellipsis in the middle of a string when the table is resized调整表格大小时如何通过在字符串中间放置省略号来截断字符串
【发布时间】:2019-12-14 11:07:49
【问题描述】:

我正在尝试在网络上制作文件浏览器的原型,它有一个显示文件列表的表格。我想让表格灵活,根据屏幕宽度调整列宽并截断文件名以在单元格中仅显示 1 行文本。我希望它截断中间的字符串,而不是尾部,以显示文件扩展名。

我能够通过一些研究编写代码。但问题是,代码似乎太重了,所以当我测试它时,它会使 web 视图变慢并且有时会崩溃。有没有更简单的方法可以得出相同的结果?

  • 当屏幕调整大小并且我调用表格单元格的宽度值时,数字永远不会低于某个数字 - 但在视觉上宽度小于数字。你能告诉我是什么导致了这个问题吗?

HTML

<table id = "listTable">
    <tr class = "listTable_firstRow">
        <th style = "width: 54px;"><img src="assets/unchecked.png" style = "width: 16px; display: table-cell; margin: auto;" id = "tableSelAll"></th>
        <th style = "width: 54px;">Type</th>
        <th style = "text-align: left;padding-left: 18px; min-width: 0px;">Name</th>
        <th style = "width: 12%; min-width: 72px;">Owner</th>
        <th style = "width: 12%; min-width: 72px;">Last modified</th>
        <th style = "width: 12%; min-width: 72px;">Updated</th>
        <th style = "width: 12%; min-width: 72px;">File size</th>
    </tr>
</table>

JAVASCRIPT

function getvisualLength(target){
    var ruler = document.getElementById("ruler");
    ruler.innerHTML = target;
    return ruler.offsetWidth;
}
function getTrimmedString(targetString, targetWidth){
    var tmp = targetString;
    var tail = targetString.substr(targetString.indexOf('.')-3);
    var truncLength = getvisualLength("...");
    var tailLength = getvisualLength(truncLength);

    if (getvisualLength(tmp) > targetWidth){
        while (getvisualLength(tmp) > (targetWidth-(truncLength+tailLength+10))){
            tmp = tmp.substr(0, tmp.length-1);
        }
    }

    else{
        return tmp;
    }

    return tmp+"..."+tail;
}
window.addEventListener("resize", function(){
    var table = document.getElementById("listTable");
    for (var i = 4; i < table.rows.length; i++){
        var getCellWidth = document.getElementById("listTable").rows[0].cells[2].offsetWidth;
        var str = files[i][0];
        var tmp = getTrimmedString(str, getCellWidth);
        document.getElementById("listTable").rows[i].cells[2].innerHTML = tmp;
    }
});
function addListToTable(target, list){
    var table = document.getElementById(target);
    for (var i=0; i<list.length; i++){
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);
        var getIconSource;

        row.style.cssText = "height:41px; border-bottom: 1px solid #f4f4f4";

        for (var k=0; k<thmbIconList.length; k++){
            if (thmbIconList[k][0] == list[i][2]){
                getIconSource = thmbIconList[k][1];
            }
        }
        row.clickStat = "off";
        row.id = "tableRow";
        var checkbox = row.insertCell(0);
        var type = row.insertCell(1);
        var name = row.insertCell(2);
        var owner = row.insertCell(3);
        var lastmodified = row.insertCell(4);
        var update = row.insertCell(5);
        var size = row.insertCell(6);
        checkbox.innerHTML = "<img src=\"assets/unchecked.png\" style = \"width: 16px; display: table-cell; margin: auto;\">"
        type.innerHTML = "<img src=\""+getIconSource+"\"style = \"width: 20px; display: table-cell; margin: auto;\">"
        name.innerHTML = list[i][0];
        name.style.cssText = "display: block;"
        name.style.cssText = "text-align: left; padding-left: 18px";
        owner.innerHTML = "me";
        owner.style.cssText = "min-width: 72px; text-align: center"
        lastmodified.innerHTML = list[i][1];
        lastmodified.style.cssText = "min-width: 72px; text-overflow:ellipsis; overflow: hidden; white-space: nowrap; text-align: center";
        update.innerHTML = list[i][1];
        update.style.cssText = "min-width: 72px; text-align: center";
        size.innerHTML = list[i][3];
        size.style.cssText = "min-width: 72px;text-align: center";        
    }
}
addListToTable("listTable", folders);
addListToTable("listTable", files);

var folders = [["Documents", "Oct 24 2019", "folder","-"], ["MyFolder_1", "Jan 2 2019", "folder","-"], ["MyFolder_2", "Oct 1 2019", "folder","-"]]
var files = [["description20191025.docx","Oct 25 2019","word", "20MB"], ["description20191025.pptx","Oct 25 2019","ppt", "20MB"], ["description20191025.xlsx","Oct 25 2019","excel", "20MB"], ["nppt_slideBackup.nppt","Oct 25 2019","nppt", "20MB"],["7D65EC6E-4A3882CF.png", "Oct 21 2019", "img", "128MB"], ["photo-153855424537648538.png", "Oct 17 2019", "img", "1.2MB"], ["photo-1538537642458538.png", "Oct 17 2019", "img", "20MB"], ["photo-15385376424548538.png", "Oct 17 2019", "img", "128MB"], ["photo-15385376425248538.png", "Oct 17 2019", "img", "1.2MB"]]

CSS

#ruler { visibility: hidden; white-space: nowrap; }
#listTable{
    width: calc(100% - 20px);
    border-collapse: collapse;
}
.listTable_firstRow{
    height: 41px;
    border-bottom: 1px solid #d8d8d8;
}

【问题讨论】:

  • 如果我推荐的解决方案有效,请告诉我。祝你好运!
  • 到目前为止,您对标尺问题的评论对我很有帮助。但是,关于截断,您的解决方案可以产生 +"..."+。但是,我正在寻找的解决方案是截断字符串的中间以适应表格单元格的宽度变化。你能帮我找到解决办法吗?
  • 明白。今晚我会再试一次。

标签: javascript html css string truncation


【解决方案1】:

您可以使用 JavaScript 的 str.replace(); 函数来实现正则表达式。

可以在此处找到示例:Replacing filename with a Regex in JavaScript

您可以拆分每个字符串以删除前三个字符,然后修改示例中的代码以替换文件名中除扩展名之外的所有字符。

像这样:

function truncateFilename(str) {
  // Find first 3 characters of filename
  var start = str.substring(0,3);

  /* Replace everything before the final '.' with an ellipsis.
   * The final '.' is retained and therefore only two additional '.' are needed.
   */
  var end = str.replace(/^.*(?=\.[^.]+$)/g, "..");

  /* Append resulting extention to the first 3 characters of
   * the file name that we found earlier.
   */
  return start + end;
}

var filename = "my_very_long_file_name.exe";
console.log(truncateFilename(filename));

我已经制作了a fiddle,所以你可以测试一下。

关于你的尺子问题,我建议你为你遇到的每个问题发布一个单独的问题,你更有可能找到解决方案。你的问题已经有了答案hereoffsetWidth 包括填充、边框等,而clientWidth 测量元素的实际宽度。你应该试试:

function getvisualLength(target){
    var ruler = document.getElementById("ruler");
    ruler.innerHTML = target;
    return ruler.clientWidth;
}

【讨论】:

    猜你喜欢
    • 2021-10-24
    • 2011-05-08
    • 2019-04-20
    • 2015-03-11
    • 1970-01-01
    • 2011-07-18
    • 2011-04-05
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多