简体   繁体   English

在文本缩短/延长的同时扩展/折叠表格行

[英]Expanding/collapsing table rows while the text shortens/lengthens

I have, for example, 3 rows from a table. 例如,我有一张表中的3行。 Each of the rows gets its content from a database. 每行都从数据库获取其内容。 By default when the row is collapsed, the text should be shortened - 默认情况下,当行折叠时,应缩短文本-

trs[i].innerText = trs[i].innerText.substring(0,25) + '...';

When a user clicks on the div it expands, and if clicked again - collapses. 当用户单击div时,它会展开,如果再次单击,则会折叠。

if(tr.style.height == "150px")
        {
             tr.style.height = "20px";
        }
    else {
        tr.style.height = "150px";
    }

So far, so good, but I want the text to be shortened only if the div is collapsed. 到目前为止,还不错,但是我希望仅当div折叠时才缩短文本。 With my solution when the page loads the text is shortened, but when the row is expanded it remains shortened. 使用我的解决方案,页面加载时,文本缩短了,但是当行扩展时,文本却缩短了。 How can I fix this? 我怎样才能解决这个问题? I can only think of when the row expands to call an AJAX function which returns the original data, but I don't think it's the best possible way. 我只能想到行何时扩展以调用返回原始数据的AJAX函数,但是我认为这不是最好的方法。 Thanks in advance. 提前致谢。

First you'd have to store the full text before shortening it. 首先,您必须先存储全文,然后再缩短它。

trs[i].fullText = trs[i].innerText;
trs[i].innerText = trs[i].innerText.substring(0,25) + '...';

Then you can restore it in the handler: 然后,您可以在处理程序中将其还原:

tr.innerText = tr.fullText;
tr.style.height = "150px";

Here's a jsFiddle . 这是一个jsFiddle I'm using jQuery to select the elements, but you already have the correct tr's anyway. 我正在使用jQuery选择元素,但是无论如何您已经有了正确的tr。 If you were using jQuery you could also use $(tr).data("fullText", $(tr).text()) which avoids some minor memory leaks with adding properties to DOM elements in old versions of Internet Explorer. 如果您使用的是jQuery,则还可以使用$(tr).data("fullText", $(tr).text()) ,这可以通过在旧版本的Internet Explorer中为DOM元素添加属性来避免一些较小的内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM