javascript - 如何在我的表格中动态添加行中打破文本?
问题描述
我创建了你可以在那里看到的效果表:
问题是,当我有很长的“文本信息”时,表格变得越来越宽,而不是文本被破坏并进入新的行和行得到扩展(拉长)。
此处显示的问题:
https ://imgur.com/3w5V03r
即使我\n
在某个时刻添加了文本,我也希望它打破。
let newText = document.createTextNode('TEXT INFO AND VERY LONG TEXT INFO \n THAT SHOULD MAKE THE TABLE \n THE SAME THICKNESS BUT LONGER');
我还阅读了有关str.split()
方法的信息,但我不知道它是否适合这里。
另外,我通过想法创建了新表,而不是按照我想要的方式格式化的行:
但是在那里使用它似乎太复杂了,但我想不出更简单的解决方案。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>...::: Multicast :::...</title>
<link rel="stylesheet" type="text/css" href="multicast.css">
<script src="js/jquery.min.js"></script>
<script src="js/jquery.sortElements.js"></script>
</head>
<body>
<?php
$provider = "Test_provider";
echo '<table id="my-table">';
echo '<tr><th colSpan = "3">INPUT ROW MULTICAST</th></tr>';
echo '<tr><th id="sort11">Sort A</th><th id="sort11">Sort B</th><th id="sort11">Sort C</th></tr>';
for($counter = 0; $counter <= 9; $counter++){
$provider = "Test_provider";
echo' <tr id ="'.$counter.'"><td><button onclick="specialFunc()">'.$provider.'</button></td><td>Row '.$counter.' Col '.$counter.'</td><td>Row '.$counter.' Col '.$counter.'</td></tr>';
}
echo '</table>';
?>
<script>
isFirst = true;
var testTable = document.getElementById("my-table"),rIndex,cIndex;
function addRow(rIndex) {
var currPos = rIndex;
let tableRef = document.getElementById("my-table");
let newRow = tableRef.insertRow(rIndex);
newRow.id = "open row"
let newCell = newRow.insertCell(0);
newCell.colSpan = 3;
let newText = document.createTextNode('TEXT INFO');
newCell.appendChild(newText);
newRowNumb = document.getElementById("open row").rowIndex;
console.log("new row addRow:" + newRowNumb);
}
function specialFunc(){
buttFlag = true;
for(var i = 2; i < testTable.rows.length; i++)
{
for(var j = 0; j < testTable.rows[i].cells.length; j++)
{
testTable.rows[i].cells[j].onclick = function()
{
rIndex = this.parentElement.rowIndex;
cIndex = this.cellIndex;
//console.log(rIndex,cIndex);
if(buttFlag==true){
if (isFirst ){
rIndex = rIndex+1;
addRow(rIndex);
console.log("new row first call :" + newRowNumb);
isFirst = false;
currPos = rIndex;
buttFlag = false;
}
else{
if(rIndex == (currPos - 1))
temp=1
else{
if(rIndex==1){rIndex=rIndex+1;}
if(typeof currPos != "undefined"){testTable.deleteRow(currPos);}
if(rIndex < currPos){rIndex=rIndex+1;}
addRow(rIndex);
console.log("new row second call and more:" + newRowNumb);
currPos = rIndex;
buttFlag = false;
}
}
}
}
};
}
};
</script>
<script>
//var table = $('table');
var table = $('#my-table');
$('#sort11,#sort12,#sort13')
.each(function(){
var th = $(this),
thIndex = th.index(),
inverse = false;
th.click(function(){
if (typeof newRowNumb != "undefined") {
console.log("del row sort func :" + newRowNumb);
document.getElementById("my-table").deleteRow(newRowNumb);
newRowNumb = void 0;
}
table.find('td').filter(function(){
return $(this).index() === thIndex;
}).sortElements(function(a, b){
return $.text([a]) > $.text([b]) ?
inverse ? -1 : 1
: inverse ? 1 : -1;
}, function(){
// parentNode is the element we want to move
return this.parentNode;
});
//newRowNumb = void 0;
currPos = void 0;
isFirst = true;
});
});
</script>
</body>
</html>
解决方案
你可以通过 CSS 设置 max-width 来解决这个问题。
编辑:对于您的示例,最好更改 tr 的 max-midth。
td {
max-width: 200px;
min-width: 150px;
word-wrap: break-word;
}
<table border="1">
<tr>
<th>Header1</th>
<td>Value</td>
</tr>
<tr>
<th>Header</th>
<td>MMMMMMMMMMMMMMMMMMMMMMMMMMMassiveValue</td>
</tr>
</table>
推荐阅读
- asp.net - 如何在运行时在 WebForms 中更改 UserControl 中控件的属性
- r - 按发件人分组的同一时间跨度内的消息合并行
- reactjs - 如何立即更新 React setState
- mysql - 无法获取查询工作的位置
- javascript - Mapbox 错误:“无法读取未定义的属性 '0'”
- javascript - 根据偏移量和限制从多个文件中获取 JSON 对象
- android - 我可以使用 RxJava 缩短房间插入时间吗?
- java - 在实现 Selenium WebDriverEventListener 的类中声明静态字符串变量会导致 ChangeValueOf 和 ChangeValueOf 方法出错
- r - 有没有办法间接访问输入参数?
- android-livedata - 如何从callbackFlow协程的collectLatest更新liveData