Interactive demonstration of row selection functionality with custom action buttons
Ctrl+A Select All | Escape Clear Selection | Delete Delete Selected
{}
Click the sections below to view implementation details and code examples for each feature.
TablixJS now includes a built-in selectAllRows() method in the API for easy implementation:
// Method 1: Using built-in selectAllRows() API
const selectedCount = table.selectAllRows();
console.log(`Selected ${selectedCount} rows`);
// Method 2: With keyboard shortcut
document.addEventListener('keydown', (e) => {
if (e.key === 'a' && (e.ctrlKey || e.metaKey)) {
e.preventDefault();
table.selectAllRows();
}
});
// Alternative: Manual implementation using existing APIs
function selectAllRows() {
const allData = table.getData();
const allIds = allData.map(row => row.id.toString());
table.selectRows(allIds);
}
<button onclick="table.selectAllRows()">
Select All Rows
</button>
table.selectAllRows() returns the number of rows selecteddataIdKey option to specify which field to use as row IDCreate powerful custom actions that operate on selected rows:
// 1. Get selected data
const selectedData = table.getSelectedData();
const selectedIds = table.getSelectedIds();
// 2. Validate selection
if (selectedData.length === 0) {
alert('No rows selected');
return;
}
// 3. Process the data
selectedData.forEach((row, index) => {
console.log(`Processing row ${index + 1}:`, row);
// Your custom logic here
});
// 4. Update table if needed
table.loadData(updatedData);
// Update buttons when selection changes
table.eventManager.on('afterSelect', (event) => {
const count = event.selectedData.length;
updateActionButtons(count);
});
function updateActionButtons(selectedCount) {
const buttons = ['exportBtn', 'deleteBtn', 'processBtn'];
buttons.forEach(buttonId => {
document.getElementById(buttonId).disabled = selectedCount === 0;
});
}
table.getSelectedData() - Array of selected row objectstable.getSelectedIds() - Array of selected row IDstable.getSelectionCount() - Number of selected rowstable.isRowSelected(id) - Check if specific row is selectedtable.clearSelection() - Clear all selectionstable.selectAllRows() - Select all visible/filtered rows (NEW!)Send selected data to PHP scripts for server-side processing:
async function exportToPHP() {
const selectedData = table.getSelectedData();
try {
const response = await fetch('/api/process-data.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
action: 'export_employees',
data: selectedData,
timestamp: new Date().toISOString()
})
});
const result = await response.json();
if (result.success) {
alert(`Processed ${selectedData.length} rows successfully!`);
}
} catch (error) {
console.error('Server error:', error);
}
}
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-Type');
try {
$input = json_decode(file_get_contents('php://input'), true);
if ($input['action'] === 'export_employees') {
$selectedData = $input['selectedData'];
// Process each selected row
foreach ($selectedData as $employee) {
// Save to database
$stmt = $pdo->prepare("INSERT INTO processed_employees
(name, department, salary) VALUES (?, ?, ?)");
$stmt->execute([
$employee['name'],
$employee['department'],
$employee['salary']
]);
}
echo json_encode([
'success' => true,
'message' => 'Employees processed successfully',
'count' => count($selectedData)
]);
}
} catch (Exception $e) {
echo json_encode([
'success' => false,
'message' => $e->getMessage()
]);
}
?>
Add keyboard shortcuts to improve user experience:
document.addEventListener('keydown', (e) => {
// Only handle when no input is focused
const activeElement = document.activeElement;
if (activeElement && (activeElement.tagName === 'INPUT' ||
activeElement.tagName === 'TEXTAREA')) {
return;
}
switch (e.key) {
case 'a':
case 'A':
if (e.ctrlKey || e.metaKey) {
e.preventDefault();
selectAllRows();
}
break;
case 'Escape':
e.preventDefault();
table.clearSelection();
break;
case 'Delete':
if (table.getSelectionCount() > 0) {
deleteSelectedRows();
}
break;
}
});
<div class="keyboard-help">
<p>
<kbd>Ctrl+A</kbd> Select All |
<kbd>Escape</kbd> Clear Selection |
<kbd>Delete</kbd> Delete Selected
</p>
</div>
<style>
kbd {
background-color: #f8f9fa;
border: 1px solid #ccc;
border-radius: 3px;
padding: 2px 5px;
font-family: monospace;
}
</style>