Create dual list from listview in Angular Listview component

27 Apr 202418 minutes to read

The dual list contains two ListView. This allows you to move list items from one list to another using the client-side
events. This section explains how to integrate the ListView component to achieve dual list.

Use cases

  • Stock exchanges of two different countries
  • Job applications (skill sets)

Integration of Dual List

Here, two ListView components have been used to display the list items. An ej2-button is used to transfer data between
the ListView, and a textbox is used to achieve the UI of filtering support.

The dual list supports:

  • Moving whole data from one list to another.
  • Moving selected data from one list to another.
  • Filtering the list by using a client-side typed character.

In the ListView component, sorting is enabled using the
sortOrder property, and
the select event is triggered
while selecting an item. Here, the select event is triggered to enable and disable button states.

Manipulating data

Moving whole data from the first list to the second list(»)

  • Here, the whole data can be moved from the first ListView to the second by clicking the first button. When clicking the button,
    the whole list items are sliced, and concat with the second ListView. This button is enabled only when the data source
    of the first ListView is not empty.

Moving whole data from the second list to the first list(«)

  • The functionality of the second button is the same as above, and data is transferred from the second list to the first
    list. This button is enabled only when the data source of the second ListView is not empty.

Moving selected item from one list to another list (>) and (<)

  • The Select event is triggered
    when selecting a list item in the ListView. The selected items can be transferred between two lists. These buttons will be
    enabled when selecting an item in lists.

Filtering method

  • The filtering method is used to filter list items when typing a character in the text box. In this
    method, the dataManager has been
    used to fetch data from the data source and display in ListView.

Sorting

  • By using the dual list, list items can be sorted in the ListView component using the
    sortOrder property.
    You can enable sorting in one ListView; in the same order, data can be transferred to another ListView.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ListViewModule } from '@syncfusion/ej2-angular-lists'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'



import { Component, ViewChild } from "@angular/core";
import { enableRipple } from "@syncfusion/ej2-base";
import { DataManager, Query, ODataV4Adaptor } from "@syncfusion/ej2-data";
import { ListViewComponent } from "@syncfusion/ej2-angular-lists";
import { ButtonComponent } from "@syncfusion/ej2-angular-buttons"
enableRipple(true);

@Component({
imports: [
        
        ListViewModule, ButtonModule
    ],


standalone: true,
    selector: 'my-app',
    template: `<div id="text1">
            <input #textbox class="e-input" type="text" id="firstInput" placeholder="Filter" title="Type in a name" (keyup)="onFirstKeyUp($event)" />
              </div>
            <ejs-listview #list1 id='list-1' [dataSource]='firstListData' [fields]='fields' [sortOrder]='Ascending'  (select)="onFirstListSelect()"></ejs-listview>
             <div id="btn">
             <button ejs-button #btn1 id="firstBtn" (click)="firstbtnclick()"> >> </button>
             <button ejs-button #btn2 id="secondBtn" [disabled]=true (click)="secondbtnclick()"> > </button>
             <button ejs-button #btn3 id="thirdBtn" [disabled]=true (click)="thirdbtnclick()"> < </button>
             <button ejs-button #btn4 id="fourthBtn" (click)="fourthbtnclick()"> << </button>
             </div>

            <div id="text2">
            <input #text class="e-input" type="text" id="secondInput" placeholder="Filter" title="Type in a name" (keyup)="onSecondKeyUp($event)" />
            </div>
            <ejs-listview #list2 id='list-2' [dataSource]='secondListData' [fields]='fields' [sortOrder]='Ascending' (select)="onSecondListSelect()"></ejs-listview>
        `,
})

export class AppComponent {
    public fields?: Object;
    public firstListData?:any; secondListData: any;
    Ascending: any;
    constructor(){
    this.firstListData  = [
        { text: "Hennessey Venom", id: "list-01" },
        { text: "Bugatti Chiron", id: "list-02" },
        { text: "Bugatti Veyron Super Sport", id: "list-03" },
        { text: "SSC Ultimate Aero", id: "list-04" },
        { text: "Koenigsegg CCR", id: "list-05" },
        { text: "McLaren F1", id: "list-06" }
    ];

   this.secondListData = [
    { text: 'Aston Martin One- 77', id: 'list-07' },
    { text: 'Jaguar XJ220', id: 'list-08' },
    { text: 'McLaren P1', id: 'list-09' },
    { text: 'Ferrari LaFerrari', id: 'list-10' },
  ];

 this.fields = { text: "text", id: "id" };
  }
    @ViewChild('list1')firstListObj?: ListViewComponent;
    @ViewChild('list2')secondListObj?: ListViewComponent;
    @ViewChild('btn1')firstBtnObj?: ButtonComponent;
    @ViewChild('btn2')secondBtnObj?: ButtonComponent;
    @ViewChild('btn3')thirdBtnObj?: ButtonComponent;
    @ViewChild('btn4')fourthBtnObj?: ButtonComponent;
    @ViewChild('textbox')textboxEle: any;
    @ViewChild('text')textEle: any;
    ngAfterViewInit(){
        this.firstListData = (this.firstListObj as ListViewComponent | any).dataSource.slice();
        this.secondListData = (this.secondListObj as ListViewComponent | any).dataSource.slice();
    }
       //Here, all list items are moved to the second list on clicking move all button
    firstbtnclick() {
        (this.secondListObj as ListViewComponent).dataSource = Array.prototype.concat.call((this.firstListObj as ListViewComponent).dataSource, (this.secondListObj as ListViewComponent).dataSource);
        this.updateFirstListData();
        this.firstListObj?.removeMultipleItems((this.firstListObj as ListViewComponent | any).liCollection);
        this.firstListData = this.firstListData.concat((this.firstListObj as ListViewComponent).dataSource);
        this.secondListData = (this.secondListObj as ListViewComponent | any).dataSource.slice();
        (this.firstBtnObj as ButtonComponent).disabled = true;
        this.onFirstKeyUp((e: any) => {});;
        this.setButtonState();
    }

    //Here, the selected list items are moved to the second list on clicking move button
  secondbtnclick() {
        let e = this.firstListObj?.getSelectedItems();
        (this.secondListObj as ListViewComponent).dataSource = Array.prototype.concat.call((this.secondListObj as ListViewComponent).dataSource, (e as any).data);
        this.firstListObj?.removeItem((e as any).item);
        this.firstListData = (this.firstListObj as ListViewComponent).dataSource;
        this.secondListData = (this.secondListObj as ListViewComponent | any).dataSource.slice();
        this.onFirstKeyUp((e: any) => {});;
        (this.secondBtnObj as ButtonComponent).disabled = true;
        this.setButtonState();
    }

    //Here, the selected list items are moved to the first list on clicking move button
   thirdbtnclick () {
        let e = this.secondListObj?.getSelectedItems();
        (this.firstListObj as ListViewComponent).dataSource = Array.prototype.concat.call((this.firstListObj as ListViewComponent).dataSource, (e as any).data);
        this.secondListObj?.removeItem((e as any).item);
        this.secondListData = (this.secondListObj as ListViewComponent).dataSource;
        this.firstListData = (this.firstListObj as ListViewComponent | any).dataSource.slice();
        this.onSecondKeyUp((e: any) => {});
        (this.thirdBtnObj as ButtonComponent).disabled = true;
        this.setButtonState();

    }

    //Here, all list items are moved to the first list on clicking move all button
   fourthbtnclick() {
        (this.firstListObj as ListViewComponent).dataSource = Array.prototype.concat.call((this.firstListObj as ListViewComponent).dataSource, (this.secondListObj as ListViewComponent).dataSource);
        this.updateSecondListData();
        this.secondListObj?.removeMultipleItems((this.secondListObj as ListViewComponent | any).liCollection);
        this.secondListData = this.secondListData.concat((this.secondListObj as ListViewComponent).dataSource);
        this.firstListData = (this.firstListObj as ListViewComponent | any).dataSource.slice();
        this.onSecondKeyUp((e: any) => {});
        this.setButtonState();

    }

    //Here, the ListView data source is updated to the first list
    updateFirstListData() {
        Array.prototype.forEach.call((this.firstListObj as ListViewComponent | any).liCollection, (list) => {
            this.firstListData.forEach((data: any, index: any) => {
                if (list.innerText.trim() === data.text) {
                    delete this.firstListData[index];
                }
            });
        });
        this.textboxEle.nativeElement.value= '';
        let ds: any[] = [];
        this.firstListData.forEach((data: any) => {
            ds.push(data);
        })
        this.firstListData = ds;

    }

    //Here, the ListView dataSource is updated for the second list
    updateSecondListData() {
        Array.prototype.forEach.call((this.secondListObj as ListViewComponent | any).liCollection, (list) => {
            this.secondListData.forEach((data: any, index: string | number) => {
                if (list.innerText.trim() === data.text) {
                    delete this.secondListData[index];
                }
            });

        });
        this.textEle.nativeElement.value = '';
        let ds: any = [];
        this.secondListData.forEach((data: any) => {
            ds.push(data);
        })
        this.secondListData = ds;

    }
    onFirstListSelect() {
        (this.secondBtnObj as ButtonComponent).disabled = false;
    }
    onSecondListSelect() {
        (this.thirdBtnObj as ButtonComponent).disabled = false;
    }

    //Here, filtering is handled using the dataManager for the first list
    onFirstKeyUp(e: any) {
        let value = this.textboxEle.nativeElement.value;
        let data = new DataManager(this.firstListData).executeLocal(new Query().where('text', 'startswith', value, true));
        if (!value) {
            (this.firstListObj as ListViewComponent).dataSource = this.firstListData.slice();
        } else {
            (this.firstListObj as ListViewComponent | any).dataSource = data;
        }
    }
    //Here, filtering is handled using the dataManager for the second list
     onSecondKeyUp(e: any) {
        let value =this.textEle.nativeElement.value;
        let data = new DataManager(this.secondListData).executeLocal(new Query().where('text', 'startswith', value, true));
        if (!value) {
            (this.secondListObj as ListViewComponent).dataSource = this.secondListData.slice();
        } else {
            (this.secondListObj as ListViewComponent | any).dataSource = data;
        }
    }

    //Here, the state of the button is changed
    setButtonState() {
        if ((this.firstListObj as ListViewComponent | any).dataSource.length) {
            (this.firstBtnObj as ButtonComponent).disabled = false;
        } else {
            (this.firstBtnObj as ButtonComponent).disabled = true;
            (this.secondBtnObj as ButtonComponent).disabled = true;
        }

        if ((this.secondListObj as ListViewComponent | any).dataSource.length) {
            (this.fourthBtnObj as ButtonComponent).disabled = false;
        } else {
            (this.fourthBtnObj as ButtonComponent).disabled = true;
            (this.thirdBtnObj as ButtonComponent).disabled = true;
        }

    }
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));