Angular Collapsible Panel showcasing brief summary

I am struggling to implement an accordion feature in Angular that displays a brief description of the content initially, and when clicked on, reveals the full description. I need to truncate the content and display it in the header. Despite my attempts, I have been unsuccessful in doing so. In the provided code snippet, I have utilized "announcement.title" for the heading, but I would like to incorporate a portion of "announcement.content" as well. My goal is to trim the content to a certain number of characters and display it in the heading. Upon clicking, the entire content should be visible. Any assistance on how to accomplish this task would be greatly appreciated.

HTML code :

<accordion>
    <div *ngFor="let announcement of announcements; let i = index">            
            <accordion-group [heading]="announcement.title">
                <div class="announcement-body" [innerHTML]="announcement.content"> {{ announcement?.content }}
                </div>
            </accordion-group>     
    </div>
</accordion>

TS file:

announcements: any[] = [];
 ngOnInit() {
    this.showAnnouncements();
  }
  showAnnouncements() {
    this.configService.getAnnouncements().then(
      (data: any) => {
        for (const key in data) {
          this.announcements.push({
            title: data[key].title,
            content: data[key].content
          });
        }
      }
    ).catch((error) => {
      console.log(error);
    });
  }

enter image description here

Furthermore, I require the title and truncated content to be displayed in the heading on separate lines with distinct fonts.

Answer №1

To enhance the title, you can attach a segment of your content by utilizing the string slice method. For instance:

this.announcements.push({
    title: data[key].title + data[key].content.slice(0,10),
    content: data[key].content
});

This approach will include the initial 10 characters of your content string in the title.

If you prefer a specific length, you could implement something like this:

title: data[key].title + data[key].content.slice(0,MaxLength - data[key].title.length)

In the latter scenario, it is important to ensure that your title remains within the maximum length or safeguard the slice against becoming negative.

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

Having trouble getting jQuery css() function to work properly?

<a href="https://www.timeatthebar.co.uk" target="_blank"><img id="icon-img" src="assets/img/tabicon2.png" class="position-absolute top-50 start-50 translate-middle" style="max-width:113px; top ...

Tab Focus discrepancy observed in Mozilla browser

Tab Focus issue with elements on Mozilla Firefox: <div class="editor-field"> <div> <%: Html.TextBox(model => model.AddressLine1, new { maxLength = 30, style = "width:300px", tabinde ...

Create functionality for editing and reviewing content within an HTML webpage

Looking to add edit and review modes to my webpage. In edit mode, users can drag and drop divs to different locations or resize them. Once changes are made, users may want to review their work. I have included a link for the user to switch to review mode ...

Performing calculations for given IndexedFaceSet of coordIndex in X3D

I am looking to design an X3D arch using HTML, similar to this example: I have tried using a cylinder shape to create the arch, but I found it insufficient as I couldn't define the thickness by setting its solid value to 'false'. I have see ...

Update the background image every minute with a smooth transition effect

I am currently in the process of developing a personal dashboard that requires dynamic background images to change every minute. To achieve this functionality, I have integrated the [Pixabay API][1] and formulated the following API request: https://pixaba ...

What is the best way to deactivate certain JavaScript functions on my webpage when accessed through a mobile device?

Is there a way to disable specific JavaScript functions when accessing my website from a mobile device? While I can achieve this using CSS only, certain buttons require JavaScript functionality. Below is the internal JavaScript code: var menuBtn = docume ...

PHP - Printing out various embedded HTML quotes

My goal is to display the following line of HTML using PHP echo "<a class='fa fa-ban fa-2x cancelClass' onClick='cancelClass('$id', '$formattedDate', '$time')'></a><p class='text-center ...

What is the best way to monitor and react to individual changes in a form array within an Angular application?

constructor(private stockService: StockService, private fb: FormBuilder, public dialog: MatDialog, public snackBar: MatSnackBar, private supplierService: SupplierService, private productService: ProductService) { this.stockForm = this.fb.group ({ //fo ...

Angular 4: Troubleshooting Navigation Bar Dropdown Dysfunction

As a novice starting out with Angular 4, I decided to work on creating some simple components as a part of my learning journey. One of the components I focused on was implementing a bootstrap navbar. Below is the code for the Navbar: <nav class="na ...

Attach a floating div to multiple elements throughout a webpage

Looking for the optimal method to place a div in various locations on a webpage while anchoring it from the bottom left. The div should be able to be attached to any element on the page for relative positioning, specifically intended for a tooltip. Each ...

Receiving an UNDEFINED INDEX error when trying to send data from an HTML form to a

I've created an HTML form that is styled using CSS for weekly work data entry. <form action="wwinsert.php" method="post"> <fieldset> <legend>Weekly Work</legend> <p><label class="lab1" for="StoreNumber">Store:</ ...

Issues with expanding all nodes in the Angular Treeview function by Nick Perkins in London are causing difficulties

Currently utilizing the angular treeview project found here: https://github.com/nickperkinslondon/angular-bootstrap-nav-tree After examining the functionality, it seems that this treeview is lacking search capabilities. To address this limitation, I deci ...

What is the process for extracting the paths of component files from an Angular ngModule file?

I've been on the lookout for automation options to streamline the process of refactoring an Angular application, as doing it manually can be quite tedious. We're working on reducing our app's shared module by extracting components/directive ...

Tips on how to change an image tag into a CSS selector

I need to change this tag to a WebElemet using CSS Selector instead of Xpath. I attempted with Xpath as shown below: panelSectionTitle.find( By.cssSelector( "img.aw-layout-right[style='']" ) ) <img style="" class="aw-layout-right" height="2 ...

Increasing the font size by 1px for each element within a specified parent element

Is there a method to automatically increase the font size of all elements within the .wrap container by 1px, without having to adjust each one individually? .wrap{ margin-top: 169px; .element1{ font-size:31px; } .element2{ ...

There appears to be a problem with the syntax in the JSON data at position 0, as it is detecting

WARNING: core.es5.js?0445:1084 WARNING SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous> SCENARIO: The goal is to automatically select the option that the user previously chose and prevent them from voting twi ...

Tips for Aligning Images of Various Sizes in the Center of a div

My images are displayed in a horizontal line, but some have different sizes. I want to center all the images within their container div for a better look. Is there a way to do this automatically with CSS so that the images remain centered regardless of siz ...

Change HTML to PDF with the ability to split text at page breaks

I recently attempted to convert an HTML/CSS document into a PDF file using the pdflayer.com API. Everything seemed to be working well, but I encountered a problem with line breaks splitting lines, as shown in this photo: https://i.sstatic.net/1tqKM.jpg I ...

Creating Angular Application with Full Screen Layout and Custom Toolbar

Angular 4.4.4 Angular Material 2.0.0-beta.12 Currently, I am in the process of designing a desktop layout using Angular Material Toolbar at the top of the screen with the router-outlet content below to occupy the remaining screen height. The main goal is ...

Designing a multi-platform web browser application for Android, iOS, and Windows 10

I've developed several web applications using HTML5 technology and now I'm interested in creating my own custom web browser specifically tailored for my apps/sites. I want it to have a native appearance, but essentially just wrap around my web ap ...