Display checkboxes next to the items in Angular ListBox.
A live Angular example of jqxListBox, the jQWidgets ListBox component, with its complete source code below. It is one of 18 jqxListBox demos for Angular.
checkboxes property – Sets or gets whether the listbox should display a checkbox next to each item.height property – Sets or gets the listbox's height.source property – Sets or gets the item's source.width property – Sets or gets the listbox's width.checkChange event – This event is triggered when the user checks or unchecks an item.checkIndex method – Checks a list item when the 'checkboxes' property value is true.getCheckedItems method – Gets the checked ListBox items.To run it in your own app, install the package with npm install jqwidgets-ng, add these files to a standalone Angular CLI app, and add jqx.base.css and a theme to the styles in angular.json. Getting started with jqxListBox shows the setup step by step.
import { Component, ElementRef, AfterViewInit, viewChild } from '@angular/core';
import { jqxListBoxComponent } from 'jqwidgets-ng/jqxlistbox';
import { CommonModule } from '@angular/common';
import { jqxListBoxModule } from 'jqwidgets-ng/jqxlistbox';
@Component({
imports: [CommonModule, jqxListBoxModule],
selector: 'app-root',
templateUrl: './app.html'
})
export class App implements AfterViewInit {
readonly myListBox = viewChild.required<jqxListBoxComponent>('jqxListBox');
readonly Events = viewChild.required<ElementRef>('Events');
readonly CheckedItems = viewChild.required<ElementRef>('CheckedItems');
ngAfterViewInit(): void {
this.myListBox().checkIndex(0);
this.myListBox().checkIndex(1);
this.myListBox().checkIndex(2);
this.myListBox().checkIndex(5);
}
source: string[] = [
"Affogato",
"Americano",
"Bicerin",
"Breve",
"Café Bombón",
"Café au lait",
"Caffé Corretto",
"Café Crema",
"Caffé Latte",
"Caffé macchiato",
"Café mélange",
"Coffee milk",
"Cafe mocha",
"Cappuccino",
"Carajillo",
"Cortado",
"Cuban espresso",
"Espresso",
"Eiskaffee",
"The Flat White",
"Frappuccino",
"Galao",
"Greek frappé coffee",
"Iced Coffee",
"Indian filter coffee",
"Instant coffee",
"Irish coffee",
"Liqueur coffee"
];
checkChange(event: any): void {
let eventsContainer = this.Events().nativeElement;
let checkedItemsContainer = this.CheckedItems().nativeElement;
if (event.args.checked) {
eventsContainer.innerHTML = 'Checked: ' + event.args.label;
}
else {
eventsContainer.innerHTML = 'Unchecked: ' + event.args.label;
}
let items = this.myListBox().getCheckedItems();
let checkedItems = '';
for (let i = 0; i < items.length; i++) {
if (i < items.length - 1) {
checkedItems += items[i].label + ', ';
}
else checkedItems += items[i].label;
}
checkedItemsContainer.innerHTML = checkedItems;
};
}
<jqxListBox #jqxListBox
(onCheckChange)="checkChange($event)"
[source]="source"
[checkboxes]="true"
[width]="200"
[height]="250">
</jqxListBox>
<div style="font-size: 13px; font-family: Verdana; margin-top: 20px;" #Events></div>
<div style="font-size: 13px; font-family: Verdana; margin-top: 10px;" #CheckedItems></div>