To properly render the unmapped fields in table format, you can use the <fw-data-table> component instead of a simple unordered list (<ul>). Below is the updated HTML and JavaScript to achieve a table-based representation for the unmapped fields.
Updated Approach: Display Unmapped Fields in a Table Format
Here is the correct HTML structure along with updated JavaScript to handle rendering in a table format using the <fw-data-table>:
HTML Structure
Replace the <ul> content with an <fw-data-table> to show the fields in table format.
<fw-modal id="unmapped-fields-modal">
<!-- Modal Title -->
<fw-modal-title>Unmapped Mandatory Fields</fw-modal-title>
<!-- Modal Content -->
<fw-modal-content>
<!-- fw-data-table for showing unmapped fields -->
<fw-data-table
id="unmapped-fields-table"
label="Unmapped Fields Table"
is-selectable="true"
is-all-selectable="true">
</fw-data-table>
</fw-modal-content>
<!-- Modal Footer -->
<fw-modal-footer>
<fw-button color="secondary" fwClose>Close</fw-button>
</fw-modal-footer>
</fw-modal>
Explanation of HTML
-
Table Component (fw-data-table):
- Added the
fw-data-table with the ID unmapped-fields-table for rendering data in table format.
- Includes the following attributes:
label: Displays a description for the table (accessible for screen readers).
is-selectable="true": Allows individual row selection.
is-all-selectable="true": Adds functionality to select/deselect all rows.
-
Modal Structure:
- Retains the same modal structure with
fw-modal, fw-modal-title, fw-modal-content, and fw-modal-footer.
JavaScript to Populate the Table
Here’s the updated logic for dynamically populating the <fw-data-table> inside the modal:
// Sample data of mandatory fields (for testing purposes)
const alldata = {
mandatory_fields: {
firstName: "User's First Name",
lastName: "User's Last Name",
emailAddress: "User's Email Address",
userRole: "User's Role in the System",
},
};
// Simulated mapped fields
const selectedMandatoryFields = {
emailAddress: "User's Email Address",
};
function showUnmappedMandatoryFields() {
const unmappedFields = Object.keys(alldata.mandatory_fields).filter(
(fieldName) => !(fieldName in selectedMandatoryFields)
);
if (unmappedFields.length > 0) {
// Get the modal
const modal = document.getElementById("unmapped-fields-modal");
if (modal) {
// Open the modal
modal.setAttribute("is-open", "true");
// Prepare unmapped fields data for the table
const tableData = {
// Define columns for the data table
columns: [
{ key: "fieldName", text: "Field Name" }, // Table column for field names
{ key: "fieldDescription", text: "Field Description" }, // Table column for field descriptions
],
// Define rows dynamically based on unmapped fields
rows: unmappedFields.map((fieldName, index) => ({
id: `${index}`, // Provide a unique ID for each table row
fieldName: fieldName, // Field name (e.g., firstName, lastName)
fieldDescription: alldata.mandatory_fields[fieldName], // Corresponding description (e.g., "User's First Name")
})),
};
// Populate the fw-data-table with the generated data
const dataTable = document.getElementById("unmapped-fields-table");
if (dataTable) {
dataTable.columns = tableData.columns; // Set columns for the table
dataTable.rows = tableData.rows; // Set rows for the table
} else {
console.error("Data table with ID 'unmapped-fields-table' not found.");
}
// Add event listener for modal close behavior
modal
.querySelector("fw-modal-footer")
.addEventListener(
"fwClose",
() => {
modal.removeAttribute("is-open");
console.log("Unmapped fields modal closed.");
},
{ once: true } // Ensure the event listener is executed only once
);
} else {
console.error("Modal element with ID 'unmapped-fields-modal' not found.");
}
} else {
// Handle the case when there are no unmapped fields
console.log("No unmapped fields to display.");
}
}
// For demonstration: Add a button to trigger the modal
document.addEventListener("DOMContentLoaded", () => {
const button = document.createElement("button");
button.innerText = "Show Unmapped Fields";
button.addEventListener("click", showUnmappedMandatoryFields);
document.body.appendChild(button);
});
How It Works
-
Table Columns:
- Defined as an array of objects passed to the
columns attribute of fw-data-table.
- Each column object has:
key: The property name in the row object that the column maps to.
text: The column header, displayed in the table.
-
Table Rows:
- Generated dynamically based on unmapped fields.
- Each row contains:
fieldName: The unmapped field's name (e.g., firstName).
fieldDescription: The corresponding description from alldata.mandatory_fields.
-
Modal and Table Behavior:
- Open the modal programmatically by setting the
is-open attribute (modal.setAttribute("is-open", "true");).
- Populate the table dynamically using JavaScript (
dataTable.columns and dataTable.rows).
-
Modal Footer:
- The close button uses
fwClose, which automatically closes the modal when clicked.
Example Output
With this structure, suppose there are the following unmapped fields:
Sample Data in alldata.mandatory_fields:
{
firstName: "User's First Name",
lastName: "User's Last Name",
emailAddress: "User's Email Address",
userRole: "User's Role in the System"
}
selectedMandatoryFields:
{
emailAddress: "User's Email Address"
}
The resulting table inside the modal will look like this:
| Field Name | Field Description |
|---|
| firstName | User's First Name |
| lastName | User's Last Name |
| userRole | User's Role in the System |
Final Notes
- The
fw-data-table allows you to display data in a structured, user-friendly format.
- You can customize the table further (e.g., add sorting, filtering, or row actions).
- The updated approach combines the power of
fw-data-table with dynamic rendering, ensuring clarity and usability.
Let me know if this resolves your query or if you need additional help! 😊