简体   繁体   English

如何将jQuery省略号应用于jQuery数据表行

[英]How to apply jQuery ellipsis to jQuery datatable row

I want to appends ellipsis (…) to the end of row of datatable(in this comment column). 我想将省略号(...)附加到数据表行的末尾(在此注释列中)。 For this I have added jQuery ellipsis js. 为此我添加了jQuery省略号js。 how should I specify height to jQuery data table row so that it only show 2 line. 我应该如何为jQuery数据表行指定高度,以便它只显示2行。 Right now height is adjusted according to length of text. 现在根据文字长度调整高度。

This is my jQuery table 这是我的jQuery表

<div id="comments">
 <c:choose>
 <c:when test="${null ne comments and not empty comments}">
  <table id="dataTable2" cellpadding="0" cellspacing="0" border="0" class="display" style="width:100%;">
    <thead><tr><th>Id</th>
        <th width="15%">User</th> 
        <th width="15%">Role</th>       
        <th width="45%">Comment</th></tr></thead>
    <tbody>
        <c:forEach items="${comm}" var="comm" varStatus="status">
            <tr><td>${comment.commentId}</td>
            <td width="15%">${comm.userFullName}</td>
            <td width="15%">${comm.userRoleName}</td>
            <td width="45%" style="height:20px" class="ellipsis multiline">${comm.CommentAbbreviation}</td></tr>
        </c:forEach>
    </tbody>
  </table>
 </c:when></c:choose>
</div>

jquery.autoellipsis.js jquery.autoellipsis.js

(function($) {
$.fn.ellipsis = function()
{
    return this.each(function()
    {
    var el = $(this);

        if(el.css("overflow") == "hidden")
        {
        var text = el.html();
        var multiline = el.hasClass('multiline');
        var t = $(this.cloneNode(true))
                .hide()
                .css('position', 'absolute')
                .css('overflow', 'visible')
                .width(multiline ? el.width() : 'auto')
                .height(multiline ? 'auto' : el.height());

el.after(t);

function height() { return t.height() > el.height(); };
function width() { return t.width() > el.width(); };

var func = multiline ? height : width;

while (text.length > 0 && func())
{
        text = text.substr(0, text.length - 1);
        t.html(text + "...");
}

el.html(t.html());
t.remove();
            }
        });
};
})(jQuery);

css class css课

.ellipsis {
        white-space: nowrap;
        overflow: hidden;
}

.ellipsis.multiline {
        white-space: normal;
}

How should I set height to datatable row ? 我应该如何设置数据行的高度?

This worked great for me 这对我很有用

.dataTable th, .dataTable td {
max-width: 200px;
min-width: 70px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

I found a solution. 我找到了解决方案。 It isn't the most correct way but it works. 这不是最正确的方式,但它的工作原理。 I wrapped the data within the in a div and I have modified following line. 我将数据包装在div中,并修改了以下行。

<td width="45%" style="height:20px" class="ellipsis multiline">${comm.CommentAbbreviation}</td>

Replace by 替换为

<td width="45%"><div class="ellipsis multiline" style="height: 35px;">${comm.CommentAbbreviation}</div></td>

It works for me 这个对我有用

.td-limit {
    max-width: 70px;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}

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

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