簡體   English   中英

取消選擇復選框將從前端角度4中刪除整行

[英]deselecting checkbox is removing whole row from the front-end angular 4

我有這個面板數組來自后端有另一個數組測試。 我已經用復選框將它們映射到我的自定義手風琴上。 我面臨的問題是我應該能夠選擇/取消選擇測試,而不必將其從前端刪除,就像我取消選擇時消失一樣。 我該如何解決這個問題?

您可以從該圖像中看到

https://i.stack.imgur.com/qJUFy.png

這是我的html文件

<ngb-panel *ngFor="let panel of panels" id="{{panel.Id}}" [title]="panel.Name">
    <label class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" [name]="panel.Id + '-' + panel.Moniker" [ngModel]="checkAllTestsSelected(panel)" (ngModelChange)="onPanelCheckboxUpdate($event, panel)" [id]="panel.Id + '-' + panel.Moniker">
        <span class="custom-control-indicator"></span>
    </label>

    </ng-template>
    <ng-template ngbPanelContent>
        <div class="individual-panel" *ngFor="let test of panel.Tests">
            <span class="text-dimmed">{{test.Name}}</span>
            <span *ngIf="panel.Name.includes('ENA') || panel.Name.includes('Celiac')">
                <label class="custom-control custom-checkbox">
                    <input type="checkbox" class="custom-control-input" [name]="test.Id + '-' + test.Code"
                    [ngModel]="testSelectionSession.SelectedPanelIds.indexOf(panel.Id) > -1 || testSelectionSession.SelectedPanelIds.indexOf(test.AssociatedPanel?.Id) > -1"
                    (ngModelChange)="onTestCheckboxUpdate($event, test, panel)" 
                    [id]="test.Id + '-' + test.Code">
                    <span class="custom-control-indicator"></span>
                </label>
            </span>
        </div>

ts文件

checkAllTestsSelected(panel: TestOrderPanel) {
    // get all individual test panels
    let individualTestPanelIds = panel.Tests.reduce((acc, test) => {
        if (test.AssociatedPanel) {
            acc.push(test.AssociatedPanel.Id);
        }
        return acc;
    }, []);

    // check if all individual test panels are selected

    let allIndividualTestsSelected = individualTestPanelIds.reduce(
        (acc: boolean, panelId: number) =>
        acc && this.panelIds.indexOf(panelId) > -1,
        individualTestPanelIds.length > 0 &&
        panel.Tests.length === individualTestPanelIds.length
    );

    // if selected, remove all individual test panels and add the panel group
    if (panel.Tests.length > 0 && allIndividualTestsSelected) {
        this.panelIds = this.panelIds.filter(
            panelId => individualTestPanelIds.indexOf(panelId) === -1
        );
        this.selectedPanels = this.selectedPanels.filter(
            selectedPanel => individualTestPanelIds.indexOf(selectedPanel.Id) === -1
        );
        this.panelIds.push(panel.Id);
        this.selectedPanels.push(panel);
        this.updateSession();
    }
    return this.panelIds.indexOf(panel.Id) > -1;
}


onPanelCheckboxUpdate($event: boolean, panel: TestOrderPanel) {
    let testPanelIds = panel.Tests.reduce((acc, test) => {
        if (test.AssociatedPanel) {
            acc.push(test.AssociatedPanel.Id);
        }

        return acc;
    }, []);

    // Wipe any duplicates
    this.panelIds = this.panelIds.filter(
        panelId => panel.Id !== panelId && testPanelIds.indexOf(panelId) === -1
    );
    this.selectedPanels = this.selectedPanels.filter(
        selectedPanel =>
        panel.Id !== selectedPanel.Id &&
        testPanelIds.indexOf(selectedPanel.Id) === -1
    );

    if ($event) {
        this.panelIds.push(panel.Id);
        this.selectedPanels.push(panel);
    }
    this.updateSession();
}

onTestCheckboxUpdate($event: boolean,
    test: TestOrderPanelTest,
    panel: TestOrderPanel,
    index) {

    let testPanelIds = panel.Tests.reduce((acc, test) => {
        if (test.AssociatedPanel) {
            acc.push(test.AssociatedPanel.Id);
        }

        return acc;
    }, []);
    let associatedTestPanels =
        this.testSelectionSession.IndividualTestPanelsForAll.filter(
            testPanel => testPanelIds.indexOf(testPanel.Id) > -1
        );
    // If the panel is selected and a test within the panel is deselected,
    // remove the panel and back all of the individual tests
    if (this.panelIds.indexOf(panel.Id) > -1 && !$event) {
        this.selectedPanels = this.selectedPanels.filter(
            e => e.Tests.splice(index, 1)
        );
    }

    let clickedTestPanel = associatedTestPanels.find(
        testPanel => (test.AssociatedPanel ? test.AssociatedPanel.Id : -1) ===
        testPanel.Id
    );

    if (clickedTestPanel) {
        // Wipe any duplicates
        this.panelIds = this.panelIds.filter(
            panelId => clickedTestPanel.Id !== panelId
        );
        this.selectedPanels = this.selectedPanels.filter(
            panel => clickedTestPanel.Id !== panel.Id
        );

        // Add individual panel if checkbox selected
        if ($event) {
            this.panelIds = this.panelIds.concat(clickedTestPanel.Id);
            this.selectedPanels = this.selectedPanels.concat(clickedTestPanel);
        }
    }
    this.updateSession();
}

this.panelIds包括面板的ID,this.selectedPanels包括整個面板的數組。

我也創建了一個stackblitz

我的代碼正在做類似的事情stackblitz.com/edit/angular-bsszc9

這是我的頁面看起來像stackblitz的示例

我怎么解決這個問題? 謝謝

刪除

if (this.panelIds.indexOf(panel.Id) > -1 && !$event) {
    this.selectedPanels = this.selectedPanels.filter(
        e => e.Tests.splice(index, 1)
    );
}

這部分什么都不做,只是出於無邏輯的原因以非常隨機的方式刪除復選框和面板(從每個面板中刪除具有相同索引的復選框,如果沒有,則刪除整個面板)

請閱讀: Array.prototype.spliceArray.prototype.filter了解這是怎么發生的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM