JavaScript List

Pico Library

This library has less than 2 KBytes

The LemonadeJS List is a library to create elements with search and pagination. It helps transform an array of objects into a list of HTML elements based on a defined template. It brings search and pagination natively, and it is possible to bind extended methods to extend its features.

Documentation

Installation

npm install @lemonadejs/list

Attributes

AttributeDescription
data: ArrayData should be an array of objects.
pagination?: NumberEnable the pagination and define the number of items per page.
search?: BooleanEnable the search.
onsearch?: FunctionWhen a search happens.
onchangepage?: FunctionWhen the user changes the page.

Important points

  • Reserved self properties: This library will create a item for each position of the data array. Each item will have its own self and a few reserved properties such as self.el and self.parent.
  • Template: The content of the component all code here is the template which will serve to create the items from the list. It is important you have only one root element.
  • React/Vue Template: In both React and Vue, it is essential to pass the template as a property. This requirement arises from the necessity for LemonadeJS to render the template in the component. This necessity is explained in detail in the examples below.

Examples

Basic example

Create quick reactive javascript lists with search and pagination.

live
<html>
<script src="https://cdn.jsdelivr.net/npm/lemonadejs/dist/lemonade.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@lemonadejs/list/dist/index.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lemonadejs/list/dist/style.min.css"/>
<div id='root' class="cards"></div>
<script>
function Component() {
    const self = this;
    self.rows = [
        { name:"bulbasaur", id: 1 },
        { name:"ivysaur", id: 2 },
        { name:"venusaur", id: 3 },
        { name:"charmander", id: 4 },
        { name:"charmeleon", id: 5 },
        { name:"charizard", id: 6 },
        { name:"squirtle", id: 7 },
        { name:"wartortle", id: 8 },
        { name:"blastoise", id: 9 },
        { name:"caterpie", id: 10 },
    ];

    return render => render`<div>
        <List :data="self.rows" :search="true" :pagination="5">
            <div class="card">
                <img src="https://lemonadejs.com/templates/default/img/pokemon/{{self.id}}.svg" style="width: 40px" />
                <div>{{self.name}}</div>
            </div>
        </List>
    </div>`;
}
// Render component
lemonade.render(Component, document.getElementById('root'));
</script>
</html>

Example with operations

The next example implements two operations on the array of objects.

live
<html>
<script src="https://cdn.jsdelivr.net/npm/lemonadejs/dist/lemonade.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@lemonadejs/list/dist/index.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lemonadejs/list/dist/style.min.css" />
<div id='root'></div>
<script>
// Create basic component
const Countries = function() {
    const self = this;

    self.countries = [
        { "name": "Australia" },
        { "name": "Austria" },
        { "name": "Chile" },
    ];

    self.add = function() {
        // Add the country
        self.countries.push({ name: self.name.value });
        // Refresh the array
        self.refresh('countries');
    }

    self.delete = function(e, s) {
        // Find and delete the position based on the self-given
        self.countries.splice(self.countries.indexOf(s), 1);
        // Refresh the array
        self.refresh('countries');
    }

    return render => render`<div>
        <List :data="self.countries" search="false">
            <div class="p4">
                <span style="width: 200px; display: inline-block">{{self.name}}</span>
                <input type="button" onclick="self.delete" value="Delete" />
            </div>
        </List>
        <div class="p4">
            <input type="text" :ref="self.name" placeholder="Add new item" />
            <input type="button" value="Add" onclick="self.add" />
        </div>
    </div>`;
}
lemonade.render(Countries, document.getElementById('root'));
</script>
</html>

Custom CSS for this example

.cards .list-content {
    display: flex;
    max-width: 600px;
}
.card {
    width: 80px;
    height: 80px;
    margin: 5px;
    text-align: center;
    font-size: 0.8em
}
.card img {
    width: 40px;
    height: 40px;
    object-fit: contain;
}