Skip to content

List

The List component provides a powerful way to display and manage lists of data with features like sorting, filtering, pagination, and search. It can work with both HTML content and JSON data sources.

This component requires:

  • Metro UI core
  • DOM module
  • Hooks module (for debounced search)
  • Pagination component (for pagination functionality)
<ul data-role="list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<ul data-role="list" data-show-pagination="true" data-show-search="true" data-items="10">
<li>Item 1</li>
<li>Item 2</li>
<!-- More items -->
</ul>
<ul data-role="list" data-source="data.json" data-show-activity="true"></ul>
<ul data-role="list"
data-sort-class="sort-name"
data-sort-dir="asc"
data-filter-class="filter-category">
<li>
<span class="sort-name">Charlie</span>
<span class="filter-category">Category A</span>
</li>
<li>
<span class="sort-name">Alpha</span>
<span class="filter-category">Category B</span>
</li>
<li>
<span class="sort-name">Bravo</span>
<span class="filter-category">Category A</span>
</li>
</ul>
<script>
var listData = {
"template": "<div class='item'><%name%>: <%value%></div>",
"data": [
{"name": "Item 1", "value": 100},
{"name": "Item 2", "value": 200},
{"name": "Item 3", "value": 300}
]
};
</script>
<ul data-role="list" id="my-list"></ul>
<script>
$(function(){
var list = Metro.getPlugin('#my-list', 'list');
list.setData(listData);
});
</script>
ParameterTypeDefaultDescription
listDeferrednumber0Deferred initialization time in milliseconds
templateBeginTokenstring<%Beginning token for templates
templateEndTokenstring%>Ending token for templates
paginationDistancenumber5Number of pagination buttons to show
paginationShortModebooleantrueIf true, uses short pagination mode
thousandSeparatorstring”,“Separator for thousands in numbers
decimalSeparatorstring”,“Separator for decimals in numbers
itemTagstring”li”HTML tag for list items
defaultTemplateTagstring”div”Default tag for template items
sortClassstringnullCSS class to use for sorting
sortDirstring”asc”Sort direction (“asc” or “desc”)
sortInitialbooleantrueIf true, sorts the list on initialization
filterClassstringnullCSS class to use for filtering
filterstring/functionnullFilter function or string
filterStringstring""Initial filter string
filtersstringnullAdditional filters
sourcestringnullURL for JSON data source
showItemsStepsbooleanfalseIf true, shows items per page selector
showSearchbooleanfalseIf true, shows search field
showListInfobooleanfalseIf true, shows list information
showPaginationbooleanfalseIf true, shows pagination
showActivitybooleantrueIf true, shows activity indicator during loading
muteListbooleantrueIf true, mutes the list during operations
itemsnumber-1Number of items per page (-1 for all)
itemsStepsstring”all, 10,25,50,100”Options for items per page selector
itemsAllTitlestring”Show all”Title for “show all” option
listItemsCountTitlestring”Show entries:“Title for items count selector
listSearchTitlestring”Search:“Title for search field
listInfoTitlestring”Showing $1 to $2 of $3 entries”Template for list info
paginationPrevTitlestring”Prev”Title for previous page button
paginationNextTitlestring”Next”Title for next page button
activityTypestring”cycle”Type of activity indicator
activityStylestring”color”Style of activity indicator
activityTimeoutnumber100Timeout for activity indicator
searchWrapperstringnullSelector for custom search wrapper
rowsWrapperstringnullSelector for custom rows wrapper
infoWrapperstringnullSelector for custom info wrapper
paginationWrapperstringnullSelector for custom pagination wrapper
searchThresholdnumber500Threshold for search debounce in milliseconds
clsComponentstring""Additional CSS class for component
clsListstring""Additional CSS class for list
clsListItemstring""Additional CSS class for list items
clsListTopstring""Additional CSS class for top block
clsItemsCountstring""Additional CSS class for items count block
clsSearchstring""Additional CSS class for search block
clsListBottomstring""Additional CSS class for bottom block
clsListInfostring""Additional CSS class for info block
clsListPaginationstring""Additional CSS class for pagination block
clsPaginationstring""Additional CSS class for pagination
clsTemplateTagstring""Additional CSS class for template tags

Deletes items matching the value or function.

var list = Metro.getPlugin('#my-list', 'list');
// Delete by value
list.deleteItem("Item 1");
// Delete by function
list.deleteItem(function(item){
return item.textContent.includes("Item");
});

Redraws the list.

var list = Metro.getPlugin('#my-list', 'list');
list.draw();

Sorts the list.

var list = Metro.getPlugin('#my-list', 'list');
// Sort by class "sort-name" in ascending order and redraw
list.sorting("sort-name", "asc", true);

Filters the list.

var list = Metro.getPlugin('#my-list', 'list');
list.filter("search term");

Sets the data for the list.

var list = Metro.getPlugin('#my-list', 'list');
list.setData({
"data": [
{"name": "Item 1", "value": 1},
{"name": "Item 2", "value": 2}
],
"template": "<div><%name%>: <%value%></div>"
});

Loads data from a URL.

var list = Metro.getPlugin('#my-list', 'list');
list.loadData("data.json");

Goes to the next page.

var list = Metro.getPlugin('#my-list', 'list');
list.next();

Goes to the previous page.

var list = Metro.getPlugin('#my-list', 'list');
list.prev();

Goes to the first page.

var list = Metro.getPlugin('#my-list', 'list');
list.first();

Goes to the last page.

var list = Metro.getPlugin('#my-list', 'list');
list.last();

Goes to a specific page.

var list = Metro.getPlugin('#my-list', 'list');
list.page(3); // Go to page 3

Adds a filter function.

var list = Metro.getPlugin('#my-list', 'list');
list.addFilter(function(item){
return item.textContent.includes("Important");
}, true);

Removes a filter by key.

var list = Metro.getPlugin('#my-list', 'list');
list.removeFilter(0, true);

Removes all filters.

var list = Metro.getPlugin('#my-list', 'list');
list.removeFilters(true);

Gets all filters.

var list = Metro.getPlugin('#my-list', 'list');
var filters = list.getFilters();

Gets the index of the main filter.

var list = Metro.getPlugin('#my-list', 'list');
var filterIndex = list.getFilterIndex();

Gets the indexes of all filters.

var list = Metro.getPlugin('#my-list', 'list');
var filterIndexes = list.getFiltersIndexes();

Destroys the component.

var list = Metro.getPlugin('#my-list', 'list');
list.destroy();
EventDescription
onDrawTriggered when the list is drawn
onDrawItemTriggered when an item is drawn
onSortStartTriggered when sorting starts
onSortStopTriggered when sorting stops
onSortItemSwitchTriggered when items are switched during sorting
onSearchTriggered when search is performed
onRowsCountChangeTriggered when the number of rows changes
onDataLoadTriggered when data loading starts
onDataLoadedTriggered when data is loaded
onDataLoadErrorTriggered when data loading fails
onFilterItemAcceptedTriggered when an item passes the filter
onFilterItemDeclinedTriggered when an item is filtered out
onListCreateTriggered when the list is created

The List component uses the following CSS classes for its structure:

  • .list-component - Main container for the list component
  • .list-top - Container for the top section (search and items count)
  • .list-search-block - Container for the search input
  • .list-rows-block - Container for the items count selector
  • .list-bottom - Container for the bottom section (info and pagination)
  • .list-info - Container for list information
  • .list-pagination - Container for pagination controls
  • .list-progress - Container for the activity indicator

You can add custom classes to various parts of the list component using the following options:

  • clsComponent - Additional class for the component container
  • clsList - Additional class for the list element
  • clsListItem - Additional class for list items
  • clsListTop - Additional class for the top section
  • clsItemsCount - Additional class for the items count block
  • clsSearch - Additional class for the search block
  • clsListBottom - Additional class for the bottom section
  • clsListInfo - Additional class for the info block
  • clsListPagination - Additional class for the pagination block
  • clsPagination - Additional class for the pagination component
  • clsTemplateTag - Additional class for template tags
  1. Use the data-sort-class attribute to specify which elements should be used for sorting
  2. For large datasets, enable pagination with data-show-pagination="true"
  3. When loading data from a remote source, show the activity indicator with data-show-activity="true"
  4. Use templates for consistent rendering of complex list items
  5. Consider using custom filters for advanced filtering requirements
  6. Use the data-format attribute on elements to specify how values should be formatted (e.g., “money”, “date”, “number”)
  7. For better performance with large lists, set a reasonable number of items per page
  8. Use custom wrappers to place search, pagination, or info sections in different parts of your page