gpt4 book ai didi

javascript - 调整表格大小时如何通过在字符串中间放置省略号来 chop 字符串

转载 作者:行者123 更新时间:2023-11-28 19:10:57 24 4
gpt4 key购买 nike

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

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

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

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"]]
#ruler { visibility: hidden; white-space: nowrap; }
#listTable{
width: calc(100% - 20px);
border-collapse: collapse;
}
.listTable_firstRow{
height: 41px;
border-bottom: 1px solid #d8d8d8;
}
<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 的 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这样您就可以对其进行测试。

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

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

关于javascript - 调整表格大小时如何通过在字符串中间放置省略号来 chop 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59334548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com