JavaScript Library That Truncates Text, List or Table By Chars, Elements or Rows

JavaScript Library That Truncates Text, List or Table By Chars, Elements or Rows

JavaScript library that truncates text, list or table by chars, elements or row and shows/hides text blocks, elements or table row with Show More and Show Less.

show More/Less

JavaScript library that truncates text, list or table by chars, elements or row and shows/hides text blocks, elements or table row with Show More and Show Less.

Demo

See the demo - example

Clone the repo and install dependencies

git clone
cd show-more

yarn
// or
npm i

Watch/Build the app

Watch the app, just call:

yarn dev
// or
npm run dev

Build app:

yarn prod
// or
npm run prod

How to use it:

Add css and js library to html

<link rel="stylesheet" href="style.css">
<script src="showMore.min.js"></script>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/tomik23/[email protected]/docs/style.css">
<script src="https://cdn.jsdelivr.net/gh/tomik23/[email protected]/docs/showMore.min.js"></script>

For text → live example:

<div class="element" data-config='{ "type": "text", "limit": 120, "more": "→ show more", "less": "← less" }'>
  Lorem ipsum, dolor ...
  ...
</div>

For list → live example:

<ul class="element" data-config='{ "type": "list", "limit": 5, "element": "li", "more": "↓ show more", "less": "↑ less", "number": true }'>
  <li>item 1</li>
  <li>item 2</li>
  ...
</ul>

For table → live example:

<table class="element" data-config='{ "type": "table", "limit": 4, "more": "↓ show more", "less": "↑ less", "number": true }'>
  ...
</table>

Only show more button → live example:

<div class="element links-style" data-config='{ "type": "list", "limit": 5, "more": "→ show more" }'>
  <a href="#">Administracja biur,</a>
</div>

Only ellipsis → live example:

<div class="element" data-config='{ "type": "text", "limit": 100, "ellipsis": " ..." }'>
  It is a long established fact that a reader will be distracted by the readable content of a page when looking
  at its layout.
</div>

Show the number next to the button → live example:

<ul class="element display-inline" data-config='{ "type": "list", "limit": 3, "element": "li", "more": "→ show more", "less": "← less", "number": true }'>
  <li>Usługi murarskie i tynkarskie,</li>
  ...
</ul>
element description
your-class name of the class after which we want to add support for showing/hiding text, list or table
data-config embedding JSON in the html, the entire configuration of a particular element
type we have three type after which it will be hidden [text, list or table]
limit text after how many characters to hide the text and insert show more/less
list or table after how many elements/rows hide the rest and insert show more/less
after this parameter checks how much text is after the trimmed text the limit parameter, if the text is less than the after parameter does not add a more/less button`^.
element on the parameter we will create an html element and put in the text show more/less
more/less is the text and chars that appears after the text, list or table e.g. > show more and < show less
number number of hidden items to show more/less e.g. -> show more 3, only works for list and table
ellipsis show only the ellipsis
onAction callback function

^ Let's say we have 20 records with text and we determine that the text is to be trimmed after 100 characters in each record, it may happen that in several records the text is very short and has 110 characters, so show more/less will appear after 100 characters and after clicking an additional 10 characters, it will look funny. To prevent this, we add the "after": 50 parameter, which means that the hidden text must be at least 50 characters. Otherwise, show more/less will not appear. The same after can be applied to lists, elements and table records

Number of records counted in the table tr based on all tr of thead, tbody and tfoot

Callback function

document.addEventListener('DOMContentLoaded', function() {
  // text, table, list, elelemnts
  new ShowMore('.element', {
    onAction: (type, object) => {
      // type = less/more and full object
      console.log(type, object);
    }
  });
});

Download Details:

Author: tomik23

Demo: https://tomik23.github.io/show-more/

Source Code: https://github.com/tomik23/show-more

javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

JavaScript Shopping Cart - Javascript Project for Beginners

JavaScript Shopping Cart - javascript shopping cart tutorial for beginnersBuy me a coffee 🍺 https://www.paypal.com/paypalme/ziddahSource Code: https://bit....

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

Data Types In JavaScript

JavaScript data types are kept easy. While JavaScript data types are mostly similar to other programming languages; some of its data types can be unique. Here, we’ll outline the data types of JavaScript.

Introduction With Basic JavaScript

Introduction With Basic JavaScript - Unlike most programming languages, the JavaScript language has no concept of input or output. It is designed to run as a scripting language in a host environment, and it is up to the host environment to provide mechanisms for communicating with the outside world.

JavaScript Memory Management System

The main goal of this article is help to readers to understand that how memory management system performs in JavaScript. I will use a shorthand such as GC which means Garbage Collection. When the browsers use Javascript, they need any memory location to store objects, functions, and all other things. Let’s deep in dive that how things going to work in GC.