How can we achieve a programmatic single selection in PrimeNG Tree v16 with checkbox selection option?

Currently, I am working on implementing the PrimeNG Tree component in Angular with selectionMode set to "checkbox". My goal is to achieve a programmatic single selection where selecting a child node automatically unselects all other child and parent nodes except for the one directly associated with the selected child node.

Despite my efforts to deselect all nodes before selecting a new one in my Angular component code, it seems that previously selected parent nodes remain selected. I have included the snippet of my code below:

export class TreeCheckboxDemo implements OnInit {
  files!: TreeNode[];
  selectedFiles!: TreeNode[];

  constructor(private nodeService: NodeService) {}

  ngOnInit() {
    this.nodeService.getFiles().then((data) => (this.files = data));
  }

  public onNodeSelect(event: any) {
    if (this.selectedFiles.length > 1) {
      this.selectedFiles = null;
    }
    this.selectedFiles = [event.node];
  }
}

To provide more context, I have created a StackBlitz example. Any assistance or additional code examples would be highly appreciated. Thank you!

Answer №1

It's possible that you're facing this issue because the nodes lack a common parent. Defining a shared parent could potentially solve this problem, as it would allow for proper propagation of unchecks and emits to reach all the necessary nodes.

I've made an attempt to address the issue in part; hopefully, it brings some improvement.

Check out my solution on stackblitz

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can Angular developers properly implement token refreshing in their applications?

Recently, I've been struggling with implementing a logic in my code. I have a specific requirement: Whenever there is a signed request (signed - means it has a JWT token for authenticated users) made to the API backend, the API backend may respond w ...

Utilizing TypeDoc to Directly Reference External Library Documentation

I am working on a TypeScript project and using TypeDoc to create documentation. There is an external library in my project that already has its documentation. I want to automatically link the reader to the documentation of this external library without man ...

TypeORM: establishing many-to-one relationships between different types of entities

Trying to represent a complex relationship in TypeORM: [ItemContainer]-(0..1)---(0..n)-[ContainableItem]-(0..n)---(0..1)-[ItemLocation] In simpler terms: A ContainableItem can exist either within an ItemContainer or at an ItemLocation. Although it cannot ...

Angular is throwing an error stating that the property 'json' cannot be found on the type 'Object'

After updating my Angular app to version 7 and switching to httpClient, I encountered the following error: Property 'json' does not exist on type 'Object' at line let act = data.json().find(x => x.ActivityId == activityId); I sus ...

Customizable parameters in a React component

I am encountering two issues with the code provided below: interface MyForm { age: number; email: string; name: string; } function Form< T, ComponentProps extends { name: string; onChange: (event: React.ChangeEvent) => void; } &g ...

How can we strengthen the type checking when defining the sorting function for json properties?

When dealing with a json structure from a service that contains .attributes json values, I often find myself needing to sort either by id from the DTO or by several attributes from the IContactsAttributes. The microservice returns this specific structure: ...

Trouble with implementing a custom attribute directive in Angular 4 and Ionic 3

Hello, I am currently working on implementing a search input field focus using a directive that has been exported from directives.module.ts. The directives.module is properly imported into the app.module.ts file. However, when attempting to use the direc ...

A guide on utilizing Material UI Fade for smoothly fading in a component when a text field is selected

I am facing an issue with a text field input and a helper component. My goal is to have the helper component fade in when a user focuses on the input field. The helper component is wrapped as follows: <Fade in={checked}> <DynamicHelperText lev ...

Searching through all values can be done by following these steps

Need help with implementing a search feature that can search all values in Angular2. Here's the current code snippet: import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'filter' }) export class FilterPipe implem ...

Adjusting the alignment of button text in an Angular Kendo grid

I am currently utilizing the grid view feature of Kendo UI for Angular. While I have buttons on the grid, the issue is that the text within the buttons is not centered properly despite applying styles such as text-align:center. Here is the template for my ...

Unfortunately, my capabilities do not allow me to execute the command 'ng build --configuration production

This is the issue that I am facing and need assistance: Error: src/app/app.component.html:1:1 - error NG8001: 'fuse-progress-bar' is not recognized as a valid element: If 'fuse-progress-bar' is an Angular component, please ensure that ...

When the NativeScript ScrollView component is initialized, it automatically scrolls to a specific section

I included a ScrollView in my component as shown below: <ScrollView height="108" orientation="horizontal" sdkToggleNavButton> . . . some content </ScrollView> The scroll width is approximately 120 ...

The imported js elements from Bootstrap are failing to function properly despite being successfully included

I want to utilize the collapse feature from Bootstrap in my project. To do so, I have installed jQuery and popper.js: "styles": [ "node_modules/bootstrap/dist/css/bootstrap.min.css", "node_modules/hover.css/css/hover.css", "node_modules/hambur ...

What is the process for creating a unique angular module using npm?

I've successfully created and published an npm module. Now, I'm facing issues in integrating it into my app using webpack to compile the angular universal setup. For Angular universal repository: https://github.com/angular/universal-starter/tre ...

"Sorry, there was an issue with AmStockCharts when trying to update the chart: Unable to assign a value to the '

Currently, I am using Angular 4.3.6 along with TypeScript 2.4.2 for my project. The issue that I am facing involves reading data from a socket and attempting to add it to the dataprovider. I came across an example at: While implementing a serial chart, q ...

Hot Module Replacement loop restarts with .Net Core version 2.1.2 and Angular 6 in the presence of enabled Https

To implement HTTPS on .Net Core 2.1.2, I made the following changes: //https app.UseHttpsRedirection(); app.UseHsts(); After that, I activated the SSL option in my project's properties under the "debug" tab. Everything was f ...

Limit users to entering either numbers or letters in the input field

How can I enforce a specific sequence for user input, restricting the first two characters to alphabets, the next two to numbers, the following two to characters, and the last four to numbers? I need to maintain the correct format of an Indian vehicle regi ...

Utilizing custom hooks for passing props in React Typescript

I have created a unique useToggler custom hook, and I am attempting to pass toggle props from it to the button child in the Header component. However, when I try using toggle={toggle}, I encounter this error: Type '{toggle: () => void;}' is ...

Ag-grid's external filtering feature allows users to easily apply

Initially, I attempted to integrate an external filter, but encountered issues where it wasn't functioning properly, resulting in the grid being stuck in a loading state. As a result, both the filter and grid are currently inaccessible. Currently, I ...

Draggable bar charts in Highcharts allow users to interact with the data by clicking

I'm working on creating a chart that allows for setting values by clicking and dragging. While the dragging functionality is working fine, I've run into an issue with the click event. When I click to set a new value, the draggable feature acts er ...