When a ListView item is clicked, a label will display text with text wrapping specific to the selected item in the list

Within the listview items, there is a label that should expand when clicked. For example, initially it only shows one line of text. Upon clicking on the label, it should expand to show 10 lines of text.

Current Issue:

At present, when I click on the first listview item label, it expands perfectly to display 10 lines using textWrap. However, it also automatically expands the label of the fourth list item.

Desired Outcome:

When clicking on the first listview item label, only that specific label should expand with textWrap. The expansion should not affect the label in the fourth list item.

Below is the relevant code snippet:

Typescript file:

onClickList(args: EventData, index: number): void {

  let labelArg = <Label>args.object;

  var listview: ListView = <ListView>this.page.getViewById("listviewId");

  listview.refresh();

  labelArg.textWrap = true;

  listview.refresh();  

}  

HTML file:

<ListView id="listviewId" [items]="_myFeedsList" class="list-group">
  <ng-template let-item="item" let-i="index">
    <StackLayout id="stackId">
      ....... .......
      <Label textWrap="false" id="labelID" [text]="item.data" (tap)="onClickList($event, i)"></Label>

    </StackLayout>
  </ng-template>
</ListView>

Answer №1

If you're looking to easily toggle the textWrap value of a tapped item, you can use a simple solution:

Typescript file :

toggle(event) {
    if (event.object.textWrap) {
      event.object.textWrap = false;
    } else {
      event.object.textWrap = true;
    }
}

HTML file, update the line with the following code :

<Label [textWrap]="false" [id]="'label' + i" [text]="item.data" (tap)="toggle($event)"></Label>

You'll notice that I've made the Label id dynamic, allowing for easier selection later on. While this change doesn't affect functionality, it does make the code cleaner compared to assigning the same id to each one.

Have a look at the working example here: https://github.com/mickaeleuranie/nativescript-stackoverflow-46910984

Answer №2

Although I don't consider myself a NativeScript 'guru', let me provide you with a simplified explanation. In your code, ensure you have a property or field dedicated to storing the index of the clicked label. Initialize this property with -1 as the default value. Upon each label click event, update this property with the index value (i in your context). Then, adjust the visibility or appearance of the label based on whether it matches the stored property value. I hope this clarifies things for you.

selected = -1;

<Label textWrap="false" id="labelID" [text]="item.data" (tap)="selected = i; onClickList($event, i)"></Label>

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

Is it possible to utilize a JSON file as a JavaScript object without directly importing it into the webpack compiled code?

While initiating the bootstrap process for my Angular hybrid app (which combines Angular 7 and AngularJS), I am aiming to utilize a separate config JSON file by storing it as a window variable. Currently, I have the following setup: setAngularLib(AngularJ ...

The access token generated by the Angular Keycloak adapter for Spring Boot backend authorization is not valid

The access tokens generated by Angular's keycloak adapter are invalid for communicating with a Spring Boot backend application secured by the same Keycloak realm. The localhost addresses were replaced with example.com to avoid issues with spam filters ...

Is it possible to refresh the component view using the service?

I am interested in developing a NotificationService that will be utilized to showcase a notification from another section. My inquiry is, how can I refresh the view of a component using a service? My ultimate goal is to have the capability to embed a comp ...

What is the best way to alter an Rxjs event in order to track pressed keys?

I found a helpful solution for hotkeys using this RXjs method. In my setup, I am using the condition below to detect leftShift + m keys: const toggle$ = shortcut([KeyCode.ShiftLeft, KeyCode.KeyM]); The issue I'm facing is that it only triggers when ...

What is the best way to create a distinct slug using TypeScript and mongoose?

After scouring through numerous modules, I couldn't find one that worked with TypeScript, including mongoose-slug-generator and mongoose-slug-plugin. ...

React with Typescript: Potential occurrence of an undefined object

While working with React and TypeScript, I encountered the error Object is possibly 'undefined' when using language.toLowerCase().includes(selectedCategory). To address this issue, I implemented a check as shown below. Although this resolved th ...

Using react-confetti to create numerous confetti effects simultaneously on a single webpage

I'm looking to showcase multiple confetti effects using the react-confetti library on a single page. However, every attempt to do so in my component seems to only display the confetti effect on the last element, rather than all of them. The canvas fo ...

Utilizing Angular's reactive forms to populate an array field with an array of data

I am currently using Angular 6 along with Reactive form to create a form. Within the ngOnInit() function, I have set up a form group in the following manner: this.landingPageForm = this.formBuilder.group({ title: new FormControl('', [ Val ...

typescript import { node } from types

Exploring the possibilities with an electron application developed in typescript. The main focus is on finding the appropriate approach for importing an external module. Here is my typescript configuration: { "compilerOptions": { "target": "es6", ...

What is the best way to attach an attribute to a element created dynamically in Angular2+?

After reviewing resources like this and this, I've run into issues trying to set attributes on dynamically generated elements within a custom component (<c-tabs>). Relevant Elements https://i.stack.imgur.com/9HoC2.png HTML <c-tabs #mainCom ...

Is it possible to dynamically retrieve an element's style from @ViewChild in an Angular 2 component without needing an active approach?

For instance, there's an element in the template that uses a local variable #targetElement to retrieve its current width whenever needed. However, I prefer not to calculate the style programmatically. I attempted using a setter with the @ViewChild ann ...

Ways to set a default selection for an md-radio-button in md-radio-groups

My button group consists of 3 radio buttons for filtering data, and I would like to have a specific button selected by default when the page loads. Below is the code snippet: <md-radio-group ng-model="status" aria-label="filter" ng-model="status" name ...

Discover the method for displaying a user's "last seen at" timestamp by utilizing the seconds provided by the server

I'm looking to implement a feature that displays when a user was last seen online, similar to how WhatsApp does it. I am using XMPP and Angular for this project. After making an XMPP request, I received the user's last seen time in seconds. Now, ...

Forgot to include a semicolon in your code? If you attempted to parse SCSS using the regular CSS parser, give it another shot but this time with the

I am currently working on an Angular 14 project and one of my tasks involves changing the default font to our company's specific font. We are utilizing Angular material components and styles, so it is important for me to not only set the default font ...

When attempting to import css-animator in Angular2/Typescript, a 404 error is displayed, indicating that the

Recently, I delved into the world of Angular2 and decided to experiment with some animations using css-animator package.json "dependencies": { "@angular/common": "2.0.0-rc.3", "@angular/compiler": "2.0.0-rc.3", "@angular/core": "2.0.0-rc.3", ...

Row Model for Server-Side Processing

Looking to maintain the default loading overlay while serverSideRowModel functions are invoked in my Angular app, and prevent the loading spinner from showing up during data fetching. ...

Ways to access the types of function parameters

Obtaining a method's parameter types using ReflectAPI is simple: Reflect.getMetadata('design:paramtypes', target, propertyKey); However, the challenge arises when trying to retrieve a function's parameter types as it constantly return ...

React Native component that enables users to both input their own text and select options from a dropdown menu as they type

Looking to develop a component that combines Text Input and FlatList to capture user input from both manual entry and a dropdown list. Has anyone successfully created a similar setup? I'm facing challenges in making it happen. Here's the scenari ...

The page fails to load when redirected, however, it briefly displays the page upon clicking the back button before navigating back to the previous page

Currently, I am working with Angular 2 and encountering an issue when attempting to click on a link. The loading bar continues to progress without displaying the data or HTML content on the page. Furthermore, if I try to go back to the previous page, the i ...

Angular 9 introduces a new feature where canActivate now supports Observable<boolean> which provides a more robust error handling mechanism for failed

Currently, I am working with angular9 and rxjs6 while implementing canActivate: Observable feature. However, I encountered an error when attempting to use catchError, as shown in the image below: Is there a solution to fix this issue? I have already tried ...