Use the expanded attribute of the mat-expansion-panel element and when condition is met it set to true. In the following example a filter pipe it is used and the result is placed in the filteredBookmarks variable which is checked in the condition - [expanded]="filteredBookmarks.length === 1" :

<mat-expansion-panel
  *ngFor="let bookmark of bookmarks | bookmarkFilter: filterText as filteredBookmarks; index as i"
  [expanded]="filteredBookmarks.length === 1">
  <mat-expansion-panel-header *ngIf="i<15">
    <div class="p-3">
      <h5 class="card-title">
        <a href=""
           [innerHTML]="bookmark.name | slice:0:100 | highlightHtml: filterText"
           target="_blank"
           (click)="addToHistoryService.promoteInHistoryIfLoggedIn(true, bookmark)"
           (auxclick)="addToHistoryService.onMiddleClickInDescription(true, $event, bookmark)"
        >
          see innerhtml
        </a>
        <sup class="external-link-hint"><i class="fas fa-external-link-alt"></i></sup>
      </h5>
      <h6 class="card-subtitle mb-2 text-muted url-under-title"
          [innerHTML]="bookmark.location | slice:0:120 | highlightHtml: filterText"
      >
        see innerhtml
      </h6>
    </div>
  </mat-expansion-panel-header>

  <ng-template matExpansionPanelContent>
    <app-bookmark-text [bookmark]="bookmark" [showMoreText]="true"
                       (click)="addToHistoryService.onClickInDescription(true, $event, bookmark)"
                       (auxclick)="addToHistoryService.onMiddleClickInDescription(true, $event, bookmark)">
    </app-bookmark-text>
  </ng-template>
</mat-expansion-panel>

Project: codever - File: hot-keys-dialog.component.html

See it in action at www.codever.land:

Copy-to-clipboard-demo


Shared with from Codever.land. 👉 Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

To access the filtered results of the pipe just define it as a variable via the as keyword. In the following example the bookmarkFilter filters the list of bookmarks based on some text search, and I need to check the filtered results contain only one element [showMoreText] = "filteredBookmarks.length === 1 "(to display the fool content of the bookmark)

<div class="mt-3" *ngFor="let bookmark of bookmarks | bookmarkFilter: filterText as filteredBookmarks">
  <app-bookmark-list-element
    [showMoreText] = "filteredBookmarks.length === 1 || bookmarks.length === 1"
    [bookmark]="bookmark"
    [userData$]="userData$"
    [queryText]="queryText"
    [filterText]="filterText"
    [isSearchResultsPage]="isSearchResultsPage"
  >
  </app-bookmark-list-element>
</div>

See it in action at www.codever.land:

Copy-to-clipboard-demo


The source code for Codever is available on Github


Shared with from Codever.land. 👉 Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

Use the click event to pass the text to the handling function, in this case copyToClipboard(bookmark.location)

<span class="btn-light btn-sm" (click)="copyToClipboard(bookmark.location)" title="Copy link to clipboard">
  <i class="far fa-copy copy-link"></i><span class="copy-btn-text"></span>
</span>

To programmatically copy a string use the Clipboard which service copies text to the user’s clipboard. We use a setTimeout to visually inform the user for a brief moment that the copy was successful

import { Clipboard } from '@angular/cdk/clipboard';

export class BookmarkListElementComponent {

copyLinkButtonText = '';

  constructor(private router: Router,
              private clipboard: Clipboard) {}

  copyToClipboard(location: string) {
    const copied = this.clipboard.copy(location);
    if (copied) {
      this.copyLinkButtonText = ' Copied';
      setTimeout(() => this.copyLinkButtonText = '', 1300);
    }

  }
}

See it in action at www.codever.land:

Copy-to-clipboard-demo

See the reference link (official docs) how to use it for longer texts.

Reference - https://material.angular.io/cdk/clipboard/overview


Shared with from Codever.land. 👉 Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

Use DefaultValue annotation parameter (accepts strings) alongside the QueryParam annotation

@GET
@Path("/bookmarks")
@Produces(MediaType.APPLICATION_JSON)
@Operation(
    summary = "Return bookmarks from repository",
    description = "Return bookmarks from repository")
@ApiResponses({
    @ApiResponse(responseCode = "200", description = "OK"),
    @ApiResponse(responseCode = "403", description = "Forbidden")
})
@RolesAllowed(ADMIN_ROLE)
public void getAllBookmarks(
    @Parameter(description = "max number of returned bookmarks")
    @DefaultValue(Integer.MAX_VALUE + "")
    @QueryParam("maxResult") Integer maxResult) {
  bookmarksService.getBookmarks(maxResult);
}

Shared with from Codever.land. 👉 Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

Given the following Superhero class:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import lombok.Getter;
import lombok.Setter;

@XmlRootElement(name = "super-hero")
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
class SuperHero {
  @XmlElement(name = "name")
  String name;

  @XmlElement(name = "super-power")
  String superPower;
}

You can convert an XML String to a SuperHero instance with the following code:

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
class TestXmlStringToObjectUnmarshalling {

  static final String superHeroXml =
      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
          + "<super-hero>"
          + "  <name>Superman</name>"
          + "  <super-power>Flight</super-power>"
          + "</super-hero>";

  @Test
  void testXmlUnmarshalling() throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(SuperHero.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    StringReader reader = new StringReader(superHeroXml);
    SuperHero superHero = (SuperHero) unmarshaller.unmarshal(reader);

    assertEquals("Flight", superHero.getSuperPower());
  }
}

Note:

  • create a JAXBContext which includes the SuperHero class - JAXBContext.newInstance(SuperHero.class)
  • create a JAXB Unmarshaller and apply the unmarshal method to a StringReader wrapping the text (it could be also a FileReader or any other Reader for that matter)

Shared with from Codever.land. 👉 Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.