Trying to use USWDS tables in my Angular project, functionality missing unless table is visible on page load. #5365
-
I include USWDS as a package dependency in my package.json. In my angular.json, I include something like this:
For the uswds sortable table on my home page, everything looks great: the uswds.min.js created the appropriate sort buttons and such on my table. However, I have other pages / components thata are not initially visible. When I navigate to them, they are not given the sort headers buttons and such because the script has already run one time. If I manually copy the contents of uswds.min.js and paste and run them in chrome's console, the headers appear. What is the best way to have the table functionality script(s) ran whenever a new table that is made visible sometime after initial page load and thus the uswds.min.js script has already ran? This in the readme sounds relevant:
But I haven't been able to make any import of this sort work. For example, "import USWDS from "uswds/src/js/components";" results in this error: "Could not find a declaration for module [...]" EDIT: I'm using uswds 2.14.0. EDIT TWO: I've gotten past this issue with the help below, thanks. Here is roughly what I look like now: Define types in types.d.ts
Import inside my component
Use it
I do have a new issue now, however, see below, in which clicking a sort button on a table that's nested inside another table results in toggleSort executing twice. Haven't solved that issue, but anyway I've gotten past the initial issue that this thread was about. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 12 replies
-
Its been a little while since I've used USWDS in Angular, but I have been using in React, and in order to get it to work properly in react, I have to call the table.on() and table.off() functions appropriately when the table component loads/unloads to enable sorting functionality. There is an example here (scroll down to the Angular section): https://designsystem.digital.gov/documentation/developers/#js-customization-2. This sort of thing also needs to be done on other more dynamic components like Accordion, Tooltip, etc. |
Beta Was this translation helpful? Give feedback.
You might need to define types like I've done here: https://github.com/MetroStar/comet/blob/main/packages/comet-uswds/src/uswds/types.d.ts