Tabulator手动/高亮校验模式下指定单元格校验失败的拦截方案问询
Let’s break down how to handle your two core requirements—intercepting via the validationFailed event and blocking cell actions in manual validation mode. I’ve worked through similar scenarios with Tabulator before, so here’s a practical, actionable approach:
1. Intercepting User Actions Using the validationFailed Event
When using Highlight or Manual validation modes, the validationFailed event triggers every time a cell fails its validation rules. You can hook into this event to check your specific conditions, then block the user’s intended action (like moving focus away from the cell or submitting the row).
Here’s a concrete example:
We’ll target a specific column (e.g., the email column) and prevent the user from leaving the cell until they fix the validation error.
// Initialize your Tabulator table const table = new Tabulator("#example-table", { columns: [ { title: "Full Name", field: "name", validator: "required" }, { title: "Email Address", field: "email", validator: ["required", "email"], validationMode: "highlight" // Or "manual" }, { title: "Age", field: "age", validator: "numeric" } ], // Hook into the validation failure event validationFailed: function(cell, value, validators) { // Check if this is the specific cell/column we want to enforce if (cell.getField() === "email") { // Force focus back to the invalid cell to block navigation cell.getElement().focus(); // Show a clear user message (customize this as needed) alert("Please enter a valid email before moving to another field!"); // Return false to suppress default Tabulator behaviors for the failure return false; } } });
Key takeaways here:
- Use
cell.getField()orcell.getColumn().getTitle()to target specific cells/columns cell.getElement().focus()ensures the user can’t leave the invalid cell until they resolve the issue- Returning
falsefrom the event handler can override default Tabulator actions tied to validation failures
2. Blocking Cell Actions in Manual Validation Mode
In manual mode, validation doesn’t run automatically—you need to trigger it explicitly. To block actions like saving a row or navigating away from a cell, run validation first, then check the result before allowing the action to proceed.
Example 1: Block row save on validation failure
// Helper function to validate a row and check results function validateRow(row) { // Trigger manual validation on the row const validationResults = row.validate(); // Check if any cells failed validation const hasInvalidFields = Object.values(validationResults).some(result => !result.valid); if (hasInvalidFields) { alert("Please fix all invalid fields before saving!"); // Focus the first invalid cell to guide the user const firstInvalidCell = row.getCells().find(cell => !cell.validate().valid); if (firstInvalidCell) { firstInvalidCell.edit(true); // Enter edit mode firstInvalidCell.getElement().focus(); } return false; // Block the save action } return true; // Allow save to proceed } // Initialize table with manual validation mode const table = new Tabulator("#example-table", { columns: [ // Your column definitions here, with validation rules { title: "Actions", formatter: () => "<button class='save-btn'>Save Row</button>", cellClick: (e, cell) => { const row = cell.getRow(); // Validate before saving if (validateRow(row)) { // Proceed with your save logic console.log("Saving row data:", row.getData()); } } } ], validationMode: "manual" // Set global manual validation mode });
Example 2: Block cell navigation during editing
If you want to prevent users from moving to another cell while editing an invalid field, combine manual validation with the cellBlur event:
cellBlur: function(cell) { // Trigger validation when the cell loses focus const validationResult = cell.validate(); if (!validationResult.valid) { // Refocus the cell to block navigation cell.getElement().focus(); alert("Please correct this field before moving on!"); return false; } }
内容的提问来源于stack exchange,提问作者Sachin

