### JavaScript for Table Row Generation and Clusterize.js Initialization
Source: https://context7.com/nexts/clusterize.js/llms.txt
Provides JavaScript functions to generate table rows from data and initialize Clusterize.js for table rendering. It includes a function to map data objects to HTML table rows and an example of initializing Clusterize.js with table-specific options like `tag: 'tr'`, and also demonstrates how to implement sortable columns by updating the clusterized content.
```javascript
// Generate table rows
function generateTableRows(data) {
return data.map(function(item) {
return '
' +
'
' + item.id + '
' +
'
' + item.name + '
' +
'
' + item.email + '
' +
'
' + item.status + '
' +
'
';
});
}
// Initialize with table data
var tableData = [];
for (var i = 0; i < 50000; i++) {
tableData.push({
id: i + 1,
name: 'User ' + i,
email: 'user' + i + '@example.com',
status: ['active', 'inactive', 'pending'][i % 3]
});
}
var clusterize = new Clusterize({
rows: generateTableRows(tableData),
scrollId: 'scrollArea',
contentId: 'contentArea',
tag: 'tr' // Explicitly set for tables
});
// Sortable columns
document.querySelectorAll('th').forEach(function(th, index) {
th.addEventListener('click', function() {
var sorted = [...tableData].sort(function(a, b) {
var keys = ['id', 'name', 'email', 'status'];
return String(a[keys[index]]).localeCompare(String(b[keys[index]]));
});
clusterize.update(generateTableRows(sorted));
});
});
```
--------------------------------
### CSS for Clusterize.js Styling and Layout
Source: https://context7.com/nexts/clusterize.js/llms.txt
Includes essential CSS for Clusterize.js to ensure proper styling and layout, especially for spacer elements and no-data rows. The provided CSS manages scroll height and appearance, with examples for custom styling of list items and hiding spacer rows.
```css
/* clusterize.css provides these base styles */
.clusterize-scroll {
max-height: 300px;
overflow: auto;
}
.clusterize-content {
outline: 0;
}
.clusterize-no-data {
text-align: center;
padding: 20px;
color: #999;
}
/* Custom styling example */
.clusterize-content li {
padding: 10px 15px;
border-bottom: 1px solid #eee;
}
.clusterize-content li:nth-child(odd) {
background-color: #f9f9f9;
}
/* Hide the spacer rows */
.clusterize-extra-row {
visibility: hidden;
}
```
--------------------------------
### Append Data to Clusterize.js
Source: https://context7.com/nexts/clusterize.js/llms.txt
Demonstrates how to add new rows to the end of the existing dataset using the `append()` method. This is commonly used for implementing infinite scrolling or loading more data dynamically. Examples include appending single or multiple rows, and a basic infinite scroll pattern.
```javascript
// Append single row
clusterize.append(['
'
]);
// Infinite scroll implementation
var loading = false;
scrollElement.addEventListener('scroll', function() {
var scrollBottom = this.scrollTop + this.clientHeight;
var scrollHeight = this.scrollHeight;
if (scrollBottom >= scrollHeight - 100 && !loading) {
loading = true;
fetchMoreData().then(function(newRows) {
clusterize.append(newRows);
loading = false;
});
}
});
```
--------------------------------
### Get Scroll Progress from Clusterize.js
Source: https://context7.com/nexts/clusterize.js/llms.txt
Returns the current scroll progress of the Clusterize.js instance as a percentage (0-100). This is useful for implementing scroll-based features like progress indicators or 'back to top' buttons.
```javascript
scrollElement.addEventListener('scroll', function() {
var progress = clusterize.getScrollProgress();
document.getElementById('progressBar').style.width = progress + '%';
document.getElementById('progressText').textContent = Math.round(progress) + '%';
});
document.getElementById('jumpToMiddle').addEventListener('click', function() {
var totalHeight = clusterize.getRowsAmount() * rowHeight;
scrollElement.scrollTop = totalHeight * 0.5;
});
scrollElement.addEventListener('scroll', function() {
var backToTop = document.getElementById('backToTop');
backToTop.style.display = clusterize.getScrollProgress() > 20 ? 'block' : 'none';
});
```
--------------------------------
### Get Row Count from Clusterize.js
Source: https://context7.com/nexts/clusterize.js/llms.txt
Returns the total number of rows currently managed by the Clusterize.js instance. This method is useful for displaying item counts, checking if data is loaded, or providing pagination information.
```javascript
var count = clusterize.getRowsAmount();
document.getElementById('counter').textContent = 'Total items: ' + count;
if (clusterize.getRowsAmount() === 0) {
showEmptyState();
} else {
hideEmptyState();
}
var total = clusterize.getRowsAmount();
console.log('Showing ' + Math.min(200, total) + ' of ' + total + ' items');
```
--------------------------------
### Initialize Clusterize.js with Options
Source: https://context7.com/nexts/clusterize.js/llms.txt
Demonstrates how to create a new Clusterize instance using various configuration options. This includes specifying data, DOM element IDs or references, and advanced settings like rows per block, blocks per cluster, and callback functions. It requires a scrollable container and a content area in the HTML.
```javascript
// HTML structure required:
//
//
//
//
//
// Basic initialization with row data
var clusterize = new Clusterize({
rows: [
'
Row 1
',
'
Row 2
',
'
Row 3
',
// ... thousands more rows
],
scrollId: 'scrollArea',
contentId: 'contentArea'
});
// Alternative: using element references instead of IDs
var clusterize = new Clusterize({
rows: rows,
scrollElem: document.getElementById('scrollArea'),
contentElem: document.getElementById('contentArea')
});
// Full configuration with all options
var clusterize = new Clusterize({
rows: generateRows(50000), // Array of HTML strings
scrollId: 'scrollArea',
contentId: 'contentArea',
rows_in_block: 50, // Number of rows in each block (default: 50)
blocks_in_cluster: 4, // Number of blocks in a cluster (default: 4)
show_no_data_row: true, // Show message when no data (default: true)
no_data_class: 'clusterize-no-data', // CSS class for no-data row
no_data_text: 'No data available', // Text shown when empty
keep_parity: true, // Maintain odd/even row classes (default: true)
tag: 'li', // Tag name for rows (auto-detected if null)
callbacks: {
clusterWillChange: function() {
console.log('Cluster is about to change');
},
clusterChanged: function() {
console.log('Cluster has changed');
},
scrollingProgress: function(progress) {
console.log('Scroll progress:', progress.toFixed(2) + '%');
}
}
});
// Helper function to generate test data
function generateRows(count) {
var rows = [];
for (var i = 0; i < count; i++) {
rows.push('
Item #' + i + ' - ' + randomContent() + '
');
}
return rows;
}
```
--------------------------------
### Linking Clusterize.js CSS File
Source: https://context7.com/nexts/clusterize.js/llms.txt
Demonstrates how to include the Clusterize.js CSS file in an HTML document to apply the necessary styles for the library's functionality, including scrollable areas and custom element appearances.
```html
```
--------------------------------
### Update Clusterize.js Data
Source: https://context7.com/nexts/clusterize.js/llms.txt
Shows how to replace the entire dataset of a Clusterize instance with new data using the `update()` method. This is useful for applying filters, sorting, or loading entirely new sets of information. The scroll position is maintained if possible.
```javascript
// Update with completely new data
var newData = [];
for (var i = 0; i < 10000; i++) {
newData.push('
New Item ' + i + '
');
}
clusterize.update(newData);
// Update with filtered data
var filteredRows = originalRows.filter(function(row) {
return row.toLowerCase().indexOf(searchTerm) !== -1;
});
clusterize.update(filteredRows);
// Update with sorted data
var sortedRows = [...originalRows].sort(function(a, b) {
var textA = a.replace(/<[^>]+>/g, '');
var textB = b.replace(/<[^>]+>/g, '');
return textA.localeCompare(textB);
});
clusterize.update(sortedRows);
```
--------------------------------
### HTML Structure for Tables with Clusterize.js
Source: https://context7.com/nexts/clusterize.js/llms.txt
Defines the necessary HTML structure for implementing tables with Clusterize.js. This includes a scrollable container, a table element with a header, and a tbody with a specific ID for content rendering. Clusterize.js will automatically manage the rows within the tbody.
```html
ID
Name
Email
Status
```
--------------------------------
### Prepend Rows to Clusterize.js List
Source: https://context7.com/nexts/clusterize.js/llms.txt
Adds new rows to the beginning of the existing data set in a Clusterize.js instance. This method is useful for real-time updates like chat messages or feed entries. It accepts an array of HTML strings representing the new rows.
```javascript
clusterize.prepend(['
New row at the top
']);
clusterize.prepend([
'
Newest item 1
',
'
Newest item 2
'
]);
websocket.onmessage = function(event) {
var newEntry = '
' + event.data + '
';
clusterize.prepend([newEntry]);
};
```
--------------------------------
### Refresh Clusterize.js Layout
Source: https://context7.com/nexts/clusterize.js/llms.txt
Recalculates row heights and re-renders the current cluster in Clusterize.js. This should be called when row dimensions may have changed due to CSS updates or window resizing. The `force` parameter can be used for a complete refresh.
```javascript
document.body.classList.toggle('compact-mode');
clusterize.refresh(true);
window.addEventListener('resize', function() {
clusterize.refresh();
});
function expandRow(index) {
rows[index] = rows[index].replace('collapsed', 'expanded');
clusterize.update(rows);
clusterize.refresh(true);
}
```
--------------------------------
### Destroy Clusterize.js Instance
Source: https://context7.com/nexts/clusterize.js/llms.txt
Removes event listeners and cleans up the Clusterize.js instance. The `clean` parameter determines whether to clear the content from the DOM. This is essential for proper cleanup, especially when reinitializing or removing the list.
```javascript
clusterize.destroy(true);
clusterize.destroy(false);
function removeList() {
clusterize.destroy(true);
document.getElementById('scrollArea').remove();
}
clusterize.destroy(true);
clusterize = new Clusterize({
rows: newRows,
scrollId: 'scrollArea',
contentId: 'contentArea',
rows_in_block: 100
});
```
--------------------------------
### Clear All Rows from Clusterize.js List
Source: https://context7.com/nexts/clusterize.js/llms.txt
Removes all rows from the Clusterize.js list and optionally displays a configured 'no-data' message. This is equivalent to updating the list with an empty array. It's useful for clearing search results or resetting the view.
```javascript
clusterize.clear();
// Example: Clear and reset search
document.getElementById('resetBtn').addEventListener('click', function() {
document.getElementById('searchInput').value = '';
clusterize.clear();
});
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.