Browse Source

加入了星星打分

yuebuzu-creater 3 months ago
parent
commit
4bd1b5d717

+ 9 - 0
wisdom-app/src/app/edit-rating-star/edit-rating-star.component.html

@@ -0,0 +1,9 @@
+
+<div class="star-rating">
+  <ng-container *ngFor="let star of starList; let i = index">
+    <ion-icon 
+      [name]="star ? 'star' : 'star-outline'" 
+      (click)="rate(i)">
+    </ion-icon>
+  </ng-container>
+</div>

+ 10 - 0
wisdom-app/src/app/edit-rating-star/edit-rating-star.component.scss

@@ -0,0 +1,10 @@
+.star-rating {
+    display: flex;
+    cursor: pointer;
+  
+    ion-icon {
+      font-size: 30px; // 调整星星大小
+      color: gold; // 星星颜色
+      margin-right: 5px; // 星星间距
+    }
+  }

+ 22 - 0
wisdom-app/src/app/edit-rating-star/edit-rating-star.component.spec.ts

@@ -0,0 +1,22 @@
+import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
+
+import { EditRatingStarComponent } from './edit-rating-star.component';
+
+describe('EditRatingStarComponent', () => {
+  let component: EditRatingStarComponent;
+  let fixture: ComponentFixture<EditRatingStarComponent>;
+
+  beforeEach(waitForAsync(() => {
+    TestBed.configureTestingModule({
+      imports: [EditRatingStarComponent],
+    }).compileComponents();
+
+    fixture = TestBed.createComponent(EditRatingStarComponent);
+    component = fixture.componentInstance;
+    fixture.detectChanges();
+  }));
+
+  it('should create', () => {
+    expect(component).toBeTruthy();
+  });
+});

+ 56 - 0
wisdom-app/src/app/edit-rating-star/edit-rating-star.component.ts

@@ -0,0 +1,56 @@
+import { CommonModule } from '@angular/common';
+import { Component, OnInit } from '@angular/core';
+import { Input, Output, EventEmitter } from '@angular/core';
+import { IonIcon } from '@ionic/angular/standalone';
+import { addIcons } from 'ionicons';
+import { star } from 'ionicons/icons';
+import { starOutline } from 'ionicons/icons';
+addIcons({star,starOutline})
+
+@Component({
+  selector: 'edit-rating-star',
+  templateUrl: './edit-rating-star.component.html',
+  styleUrls: ['./edit-rating-star.component.scss'],
+  standalone: true,
+  imports: [
+    IonIcon,CommonModule
+  ],
+})
+export class EditRatingStarComponent{
+
+  @Input() score: number = 0; // 默认分值为0
+  @Input() scoreMax: number = 5; // 最大分值
+  starList: boolean[] = []; // 星星状态数组
+
+  @Output() onScoreChange: EventEmitter<number> = new EventEmitter<number>();
+
+  constructor() {
+    this.updateStarList();
+  }
+
+  ngOnChanges() {
+    this.updateStarList();
+  }
+
+  // 更新星星数组
+  private updateStarList() {
+    this.starList = Array(this.scoreMax).fill(false);
+    for (let i = 0; i < this.score; i++) {
+      this.starList[i] = true;
+    }
+  }
+
+  // 打分方法
+  rate(index: number) {
+    if (this.score === index + 1) {
+      // 如果点击的是当前分值,清零
+      this.score = 0;
+    } else {
+      // 否则更新分值
+      this.score = index + 1;
+    }
+    this.updateStarList();
+    this.onScoreChange.emit(this.score); // 触发分值变化事件
+  }
+
+}

+ 7 - 0
wisdom-app/src/app/tab1/tab1.page.html

@@ -27,5 +27,12 @@
     </ul>
   </h1>
 
+  <edit-rating-star 
+    [score]="currentScore" 
+    [scoreMax]="5" 
+    (onScoreChange)="handleScoreChange($event)">
+  </edit-rating-star>
+  <span>{{currentScore}}</span>
+
   <app-explore-container name="智养3"></app-explore-container>
 </ion-content>

+ 16 - 1
wisdom-app/src/app/tab1/tab1.page.ts

@@ -4,6 +4,8 @@ import { ExploreContainerComponent } from '../explore-container/explore-containe
 import { IonButton } from '@ionic/angular/standalone';
 import { IonIcon } from '@ionic/angular/standalone';
 import { EditTagComponent } from '../edit-tag/edit-tag.component';
+import { EditRatingStarComponent } from '../edit-rating-star/edit-rating-star.component';
+
 
 @Component({
   selector: 'app-tab1',
@@ -12,11 +14,24 @@ import { EditTagComponent } from '../edit-tag/edit-tag.component';
   standalone: true,
   imports: [
     IonHeader, IonToolbar, IonTitle, IonContent, ExploreContainerComponent, IonTabButton, IonButton,
-    IonIcon,EditTagComponent
+    IonIcon,EditTagComponent,EditRatingStarComponent
   ],
 })
 export class Tab1Page {
 
+  /**
+   * 星星打分
+   */
+  currentScore:number = 0; // 初始分值
+
+  handleScoreChange(newScore: number) {
+    this.currentScore = newScore;
+    console.log('新分值:', newScore); // 处理分值变化
+  }
+
+  /**
+   * 标签编辑
+   */
   editTags:Array<string> = []
   setTagValue(ev:any){
     console.log("setTagValue:",ev)