javascript - 取消选中复选框正在从前端角度 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.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.splice和Array.prototype.filter以了解这是如何发生的。
推荐阅读
- c# - DataGrid ItemsSource绑定不刷新单元格值更改/用户编辑
- r - 为什么 R 过滤不适用于特定值?
- flutter - Flutter:我没有从应用内购买中获得翻译产品
- html - 覆盖先例 css 的媒体查询
- c - 与随机 3D 框的射线相交长度
- ios - Xcodebuild 命令失败
- asp.net-core - 我在注册时设置重定向 url 以响应来自 identityserver4 的页面的问题
- git - GIT 变基问题
- python - 如何将字典一一追加到json文件中?
- reactjs - 在 Redux 中,为什么要检查所有 reducer 是否要执行一个案例?