【问题标题】:change the color of whole row on checkbox checked angular 4更改复选框选中角度 4 上整行的颜色
【发布时间】:2019-07-21 08:36:47
【问题描述】:

如何使整行可点击,以及当我点击复选框时如何更改行颜色?

这是我的 html 文件

<section class="others">
<div class="sub-header">Others</div>
<p class="text-center" *ngIf="otherTests.length === 0">No Tests Available</p>
<app-custom-accordion [closeOthers]="true">
<ngb-panel [disabled]="true" *ngFor="let testPanel of otherTests" id=". {{testPanel.Id}}" [title]="testPanel.Name">
  <ng-template ngbPanelTitle>
    <div class="action-items">
      <span class="material-icons fav" [class.favorited]="testPanel.Favorite" (click)="onFavoriteClick(testPanel)"></span>
      <span class="icon-set" [ngClass]="{'same-day-2x': isSameDay(testPanel.Code), 'next-day-2x': isNextDay(testPanel.Code)}"></span>
      <label class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" [name]="testPanel.Id + '-' + testPanel.Moniker" [ngModel]="panelIds.indexOf(testPanel.Id) > -1"
        (ngModelChange)="onPanelCheckboxUpdate($event, testPanel)" [id]="testPanel.Id + '-' + testPanel.Moniker">
        <span class="custom-control-indicator"></span>
      </label>
    </div>
  </ng-template>
</ngb-panel>

这是我用于更改复选框的 Ts 文件

onPanelCheckboxUpdate($event: boolean, panel: TestOrderPanel) {
let testPanelIds = panel.Tests.map(test => test.Id);
// 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();
}

这是我的 app-custom-accordion 组件

 <div class="card">
 <ng-template ngFor let-panel [ngForOf]="panels">
<div role="tab" id="{{panel.id}}-header" [class]="'card-header ' + 
 (panel.type ? 'card-' + panel.type: type ? 'card-' + type : '')"
  [class.active]="isOpen(panel.id)">
  <a href (click)="!!toggle(panel.id)" [attr.tabindex]=" . 
 (panel.disabled 
  ? '-1' : null)" [attr.aria-expanded]="isOpen(panel.id)"
    [attr.aria-controls]="(isOpen(panel.id) ? panel.id : null)" 
 [attr.aria-disabled]="panel.disabled">{{panel.title}}</a>
  <ng-template [ngTemplateOutlet]="panel.titleTpl?.templateRef"></ng- 
  template>
  <!-- expansion arrows -->
  <div *ngIf="arrowExpand" (click)="toggle(panel.id)" [attr.aria- 
  expanded]="isOpen(panel.id)">
    <span class="material-icons expand"></span>
  </div>

 </div>
 <div id="{{panel.id}}" role="tabpanel" [attr.aria-labelledby]="panel.id + '-header'" class="card-block" *ngIf="isOpen(panel.id) && panel.contentTpl">
  <ng-template [ngTemplateOutlet]="panel.contentTpl?.templateRef"></ng-template>
      </div>
    </ng-template>
  </div>

单击复选框时如何更改整行的颜色 就像当复选框被选中时,整行应该是黑暗的或其他什么,当未选中时应该去以前的颜色,即白色 谁能帮忙?谢谢

【问题讨论】:

    标签: javascript angular typescript row panel


    【解决方案1】:

    Angular 的 ngStyleNgClass 指令可用于您要查找的内容。您需要将复选框的状态存储在相关行可以到达的某个变量中,然后您可以执行以下操作:

    <some-element [ngClass]="{'class-wanted-when-checked' : checkboxIsChecked}">...</some-element>
    

    或者这个:

    <some-element [ngStyle]="{'background-color': checkboxIsChecked ? "blue" : "white"}">...</some-element>
    

    【讨论】:

    • 什么是一些元素? @泰勒教堂
    • some-element 只是一个示例元素,因为我不确定您希望在哪里应用类或样式。将 [ngClass] 或 [ngStyle] 添加到您认为最适合的任何元素,以获得您想要的外观。
    • 我声明了一个已检查的变量并将其与 $event 相等,它是真或假 [ngStyle]="{'background-color': checked == true? 'blue' : 'white'}"像这样,但是当我选中一个复选框时,即使它们没有被选中,它也会改变每个复选框的颜色
    • 如果您需要每个复选框影响不同的事物,请将它们存储在不同的变量中,或者给每个复选框一个索引号或 ID,并将它们的选中状态存储在数组或对象中。
    猜你喜欢
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 2021-02-09
    • 2018-06-30
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多