Я вижу ожидаемую подстроку в консоли, но исходная строка не изменяется в HTML.
Окончательный результат должен обрезать любой длинный текст в каждой таблице <td>
.
Примечание. Я НЕ хочу использовать limitTo в HTML на каждом <td>
, поскольку это решение является тестом для большого приложения, использующего тысячи таблиц, и мы не хотим вручную добавлять ограничения на более длинный контент.
Вот фрагмент кода JS, с которым у меня возникли проблемы: (Я оставил комментарии, поскольку они являются актуальными попытками решения проблемы)
// force table content to wrap to next line after 30 characters (limit set in CSS)
for (var i = 0; i < tableTD.length; i++){
var text = tableTD[i].textContent;
//var truncatedText = '';
if(text.length > 30 ) {
tableTD[i].classList.add('wrap');
truncate(text, 60);
//truncatedText = truncate(text, 60);
//return truncatedText;
} else { tableTD[i].classList.add('nowrap'); }
}
// testing
// function truncate(str, num){
// var truncatedStr = '';
// if (str.length > num) {
// truncatedStr = str.slice(0, (num - 2)) + '...'
// }
// return truncatedStr;
// };
function truncate(str, num){
truncateStr = str.slice(0, (num - 2)) + '...';
return truncateStr
}
Это то, что я вижу в консоли:
Вот полное JS (angularjs 1.6.9):
var app = angular.module('myApp', []);
app.controller('karinasSandboxCtrl', function($scope)
{
// create some lines of data
$scope.records = [
{
"Name" : "Peter Rabbit",
"Age" : "34",
"Hobbies" : "Peter Rabbit: A big long list of things that I like to do inserted here so it can be truncated. Add a few more so it goes over 90 characters and needs to be truncated.",
"NumberOne" : ".01",
"NumberTwo" : "2828.09",
"NumberThree" : "33829749.00",
"NumberFour" : "379234738.0922",
"ContentToMakeLonger1" : "blah-de blah blah blah",
"ContentToMakeLonger2" : "doop-dee-doo",
"ContentToMakeLonger3" : "blooooop",
"ColumnOne" : "One",
"ColumnTwo" : "Two Two",
"ColumnThree" : "Three Three Three",
"ColumnFour" : "Four Four Four Four",
"Notes" :"Peter Rabbit: And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!",
} , {
"Name" :"Sailor Moon",
"Age" :"22",
"Hobbies" : "Sailor Moon: A big long list of things that I like to do inserted here so it can be truncated. Add a few more so it goes over 90 characters and needs to be truncated.",
"NumberOne" : ".11",
"NumberTwo" :"28.09",
"NumberThree" : "33829749.00",
"NumberFour":"379234738.0922",
"ContentToMakeLonger1" : "blah-de blah blah blah",
"ContentToMakeLonger2" : "doop-dee-doo",
"ContentToMakeLonger3" : "blooooop",
"ColumnOne" : "One",
"ColumnTwo" :"Two Two",
"ColumnThree" : "Three Three Three",
"ColumnFour":"Four Four Four Four",
"Notes" :"Sailor Moon: And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!",
} , {
"Name" : "Fire Lizard",
"Age" :"987",
"Hobbies" : "Fire Lizard: A big long list of things that I like to do inserted here so it can be truncated. Add a few more so it goes over 90 characters and needs to be truncated.",
"NumberOne" : ".11",
"NumberTwo" :"28.09",
"NumberThree" : "33829749.00",
"NumberFour":"379234738.0922",
"ContentToMakeLonger1" : "blah-de blah blah blah",
"ContentToMakeLonger2" : "doop-dee-doo",
"ContentToMakeLonger3" : "blooooop",
"ColumnOne" : "One",
"ColumnTwo" :"Two Two",
"ColumnThree" : "Three Three Three",
"ColumnFour":"Four Four Four Four",
"Notes" :"Fire Lizard: And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!",
}, {
"Name" : "Tommy Turtle",
"Age" :"24",
"Hobbies" : "Tommy Turtle: A big long list of things that I like to do inserted here so it can be truncated. Add a few more so it goes over 90 characters and needs to be truncated.",
"NumberOne" : ".11",
"NumberTwo" :"28.09",
"NumberThree" : "33829749.00",
"NumberFour":"379234738.0922",
"ContentToMakeLonger1" : "blah-de blah blah blah",
"ContentToMakeLonger2" : "doop-dee-doo",
"ContentToMakeLonger3" : "blooooop",
"ColumnOne" : "One",
"ColumnTwo" :"Two Two",
"ColumnThree" : "Three Three Three",
"ColumnFour":"Four Four Four Four",
"Notes" :"Tommy Turtle: And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!",
} , {
"Name" : "Henry Hilgard",
"Hobbies" : "Henry Hilgard: A big long list of things that I like to do inserted here so it can be truncated. Add a few more so it goes over 90 characters and needs to be truncated.",
"NumberOne" : ".11",
"NumberTwo" :"28.09",
"NumberThree" : "33829749.00",
"NumberFour":"379234738.0922",
"ContentToMakeLonger1" : "blah-de blah blah blah",
"ContentToMakeLonger2" : "doop-dee-doo",
"ContentToMakeLonger3" : "blooooop",
"ColumnOne" : "One",
"ColumnTwo" :"Two Two",
"ColumnThree" : "Three Three Three",
"ColumnFour":"Four Four Four Four",
"Notes" :"Henry Hilgard: And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes! And a TON of notes!",
}
];
angular.element(document).ready(function(){
var tableTD = document.querySelectorAll('table td');
// testing
var str = "Testing the substring thingy here to see if it will work";
document.getElementById("demo").textContent = truncate(str, 20);
// force table content to wrap to next line after 30 characters (limit set in CSS)
for (var i = 0; i < tableTD.length; i++){
var text = tableTD[i].textContent;
//var truncatedText = '';
if(text.length > 30 ) {
tableTD[i].classList.add('wrap');
truncate(text, 60);
//truncatedText = truncate(text, 60);
//return truncatedText;
} else { tableTD[i].classList.add('nowrap'); }
}
// testing
// function truncate(str, num){
// var truncatedStr = '';
// if (str.length > num) {
// truncatedStr = str.slice(0, (num - 2)) + '...'
// }
// return truncatedStr;
// };
function truncate(str, num){
truncateStr = str.slice(0, (num - 2)) + '...';
return truncateStr
}
});
});
Вот таблица HTML:
<table ng-controller="karinasSandboxCtrl">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Hobbies</th>
<th>Number 1</th>
<th>Number 2</th>
<th>Number 3</th>
<th>Number 4</th>
<th>Content To Make Longer 1</th>
<th>Content To Make Longer 2</th>
<th>Content To Make Longer 3</th>
<th>Column One</th>
<th>Column Two</th>
<th>Column Three</th>
<th>Column Four</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in records">
<td>{{x.Name}}</td>
<td>{{x.Age}}</td>
<td>{{x.Hobbies}}</td>
<td>{{x.NumberOne}}</td>
<td>{{x.NumberTwo}}</td>
<td>{{x.NumberThree}}</td>
<td>{{x.NumberFour}}</td>
<td>{{x.ContentToMakeLonger1}}</td>
<td>{{x.ContentToMakeLonger2}}</td>
<td>{{x.ContentToMakeLonger3}}</td>
<td>{{x.ColumnOne}}</td>
<td>{{x.ColumnTwo}}</td>
<td>{{x.ColumnThree}}</td>
<td>{{x.ColumnFour}}</td>
<td>{{x.Notes }}</td>
</tr>
</tbody>
</table>
Вот соответствующая CSS:
td {
padding: 6px 12px;
vertical-align: top;
overflow: hidden;
text-overflow: ellipsis;
max-width: 30ch; // "ch" limits number of characters per line.
&.nowrap {
white-space: nowrap;
}
&.wrap {
white-space: normal;
min-width: 29ch; // so the line will not wrap before 29ch
}
}
Я довольно новичок в javascript, так что я могу быть здесь чего-то не хватает. Я ценю любую помощь:)