How to apply CSS dynamically on selected items only [Angular]

丶灬走出姿态 提交于 2020-01-05 04:07:07

问题


I'm learning Angular. I've created a custom month-picker from the scratch. It selects a range of months, no dates and no weeks. I wanted to highlight the range of months that'd been selected. You can think of primeng month picker but with range. I tried this, this and many other solutions but I failed. Here is my code:

monthpicker.component.html

<div class="my-table-div dropdown-content">
  ...
  <br>
  <div *ngFor="let row of matrix" class="my-table">
    <span *ngFor="let x of row">
      <span class="month-name" (click)="onClick(x)" [class.extraStyle]="someProperty">
        {{ x }}
      </span>
    </span>
  </div>
</div>

monthpicker.component.ts

  ...
  clickCount: number =0; /*to determine lower bound and upper bound selection*/
  someProperty:boolean=false;
  flag:boolean= false;

  ...
  arr = [
    'Jan', 'Feb', 'Mar', 'Apr',
    'May', 'Jun', 'JuL', 'Aug',
    'Sep', 'Oct', 'Nov', 'Dec'
  ];
  ...

  n = 4;
  matrix = Array
    .from({ length: Math.ceil(this.arr.length / this.n) }, (_, i) => i)
    .map(i => this.arr.slice(i * this.n, i * this.n + this.n));

  ...

  onClick(x) {
    this.clickCount++;
    console.log("Month: " + x);
    if(this.clickCount%2!=0) {
      this.lowerMonth= x;
      this.lowerYear=this.year;
    }
    console.log("Year: " + this.lowerYear);
    console.log("Clicked "+this.clickCount +" times.");
    if(this.clickCount%2==0) {
      this.upperMonth=" "+x;
      this.upperYear =this.year;
      if(this.flag) {
        this.someProperty=true;
      }
      else {
        this.someProperty=false;
      }
    } 
  }

And here is the css I'm applying monthpicker.component.css

.extraStyle {
    background-color: #1474A4 !important;
    color: white !important;
    text-align: center !important;
}

And here is the result:

See, CSS is applied to all the months even though the selection is from Jan to Jul only.

Is there any way that I can apply css to selected index numbers only. Because index is starting from 0 to 11 and is kind of fixed for each month. Ok, This is what I thought, I'm sorry I may be completely wrong. Please correct me and help me implement that.


回答1:


You can add isSelected key in row array. Like this :

matrix: any = Array.from(
    { length: Math.ceil(this.arr.length / this.n) },
    (_, i) => i
  ).map(i =>
    this.arr.slice(i * this.n, i * this.n + this.n).map(x => ({
      monthName: x,
      isSelected: false
    }))
  );

than use the isSelected key in html like this :

<div *ngFor="let row of matrix" class="my-table">
    <span *ngFor="let x of row">
      <span class="month-name" (click)="onClick(x); x.isSelected = !x.isSelected" [ngClass]="{'extraStyle' : x.isSelected }">
        {{ x.monthName }}
      </span>
    </span>
</div>


来源:https://stackoverflow.com/questions/59154020/how-to-apply-css-dynamically-on-selected-items-only-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!