Show more/less text in a table automatically according to text length

走远了吗. 提交于 2019-12-08 06:05:19

问题


I have a table with two columns, the second one sometimes contains big text so I want to show only the first 100 characters and put a show more link to display the remaining text. You can see here what Table I am working on http://jsfiddle.net/j11mj21x/.

For that I am using a code provided in this link (http://viralpatel.net/blogs/dynamically-shortened-text-show-more-link-jquery/), I put it in a file show_less_more.js :

 (function($) {
$.fn.shorten = function (settings) {

    var config = {
        showChars: 100,
        ellipsesText: "...",
        moreText: "more",
        lessText: "less"
    };

    if (settings) {
        $.extend(config, settings);
    }

    $(document).off("click", '.morelink');

    $(document).on({click: function () {

            var $this = $(this);
            if ($this.hasClass('less')) {
                $this.removeClass('less');
                $this.html(config.moreText);
            } else {
                $this.addClass('less');
                $this.html(config.lessText);
            }
            $this.parent().prev().toggle();
            $this.prev().toggle();
            return false;
        }
    }, '.morelink');

    return this.each(function () {
        var $this = $(this);
        if($this.hasClass("shortened")) return;

        $this.addClass("shortened");
        var content = $this.html();
        if (content.length > config.showChars) {
            var c = content.substr(0, config.showChars);
            var h = content.substr(config.showChars, content.length - config.showChars);
            var html = c + '<span class="moreellipses">' + config.ellipsesText + ' </span><span class="morecontent"><span>' + h + '</span> <a href="#" class="morelink">' + config.moreText + '</a></span>';
            $this.html(html);
            $(".morecontent span").hide();
        }
    });

};

})(jQuery);
$(document).ready(function(){$(".descriptionText").shorten();});

I am using it like this:

    <script src="show_less_more.js"></script>"

The HTML is like this for every row:

    <tr>
      <th>
        course1
      </th>
      <td> <div class="descriptionText">Description of the course</div></td>
    </tr>

I have also added the CSS for the more and less links:

        a {
        color: #0254EB
        }
        a:visited {
        color: #0254EB
        }
        a.morelink {
        text-decoration:none;
        outline: none;
        }
        .morecontent span {
        display: none;
        }

When I do this in sfiddle it works pretty good as you can see here http://jsfiddle.net/j11mj21x/2/ However, I get nothing when rendering my table with python. I think the problem is that I don't succeed to load my JS page into the html page, because when I click on it it give nothing. Here is what is rendered in my html page:

 ....
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script><!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"><!-- Optional theme -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css"><!-- Latest compiled and minified JavaScript --><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js">
</script><script src="showLessMore.js"></script>
....

Can anyone tell me what could be the problem behind this because I have the "show_less_more.js" in the same folder as the file generator which in python?

Thank you in advance !


回答1:


Here's a simple example of doing this relying more on CSS.

$(function() {

  $('a').click(function() {
    $('div').removeClass('ellipsis');
  });
  
});
div {
  border: solid 1px orange;
  width: 200px;
  overflow: hidden;
}

.ellipsis {
  white-space: nowrap;
  text-overflow: ellipsis;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class='ellipsis'>Some really really long text that just is way too long so we must use ellipses to shorten it until the user wants to see it all.</div>
<br>
<a href='#'>Show All</a>


来源:https://stackoverflow.com/questions/28653528/show-more-less-text-in-a-table-automatically-according-to-text-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!