简体   繁体   English

如何使用 angular 和 Z1E35BCBA2DBA1089C7BD0805D457 中的按钮更改 json 文件中的 boolean 值?

[英]How to change boolean value within json file using a button in angular & typescript?

My question is, how do I write a function in my table-viewer.component.ts file to change the value from false to true within the JSON file based on whether the user clicks the cancelled button?我的问题是,如何在 table-viewer.component.ts 文件中编写 function 以根据用户是否单击取消按钮将 JSON 文件中的值从 false 更改为 true?

I have a JSON file containing the following details.我有一个包含以下详细信息的 JSON 文件。

db.json db.json

"firstName": "Hamza",
"lastName": "Gallagher",
"carReg": "FG67 POI",
"parkingNumber": "00003",
"date": "2021-01-04",
"cancelled": false

etc.. ETC..

And I am displaying them in a table with angular, which shows the following: Image of table我将它们显示在带有 angular表格中,该表格显示如下:

Table-viewer.component.html Table-viewer.component.html

    <table class="table table-striped table-bordered table-hover">
  <thead>
  <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Car Registration</th>
    <th>Parking Number</th>
    <th>Date</th>
    <th>Status</th>
    <th></th>
  </tr>
  </thead>

  <tbody>
 <tr *ngFor="let booking of bookings">

   <td>{{booking.firstName}}</td>
   <td>{{booking.lastName}}</td>
   <td>{{booking.carReg}}</td>
   <td>{{booking.parkingNumber}}</td>
   <td>{{booking.date}}</td>
   <td>
     <div *ngIf="booking.cancelled" class="red">Cancelled</div>
     <div *ngIf="!booking.cancelled" class="green">Booked</div>
   </td>
   <td>
     <button class="btn btn-danger mr-2" (click)="cancelBooking()">Cancel</button>
   </td>
 </tr>

table-viewer.component.ts table-viewer.component.ts

export class TableViewerComponent implements OnInit {
  bookings: Booking[] = [];

  getBookings(): void {
    this.bookingService.getAll().subscribe((book: Booking[]) => {
      this.bookings = book;
    });
  }

  constructor(private bookingService: BookingService) {
  }

  ngOnInit(): void {
    this.getBookings();
  }

  cancelBooking(): void {
  //  Help
  }

}

booking.ts预订.ts

export interface Booking {
  'firstName': string;
  'lastName': string;
  'carReg': string;
  'parkingNumber': string;
  'date': string;
  'cancelled': boolean;
}

Table-viewer.component.html Table-viewer.component.html

<table class="table table-striped table-bordered table-hover">
     ...
     <td>
       <button class="btn btn-danger mr-2" (click)="cancelBooking(booking: Booking)">Cancel</button>
     </td>
  </tr>

table-viewer.component.ts table-viewer.component.ts

export class TableViewerComponent implements OnInit {
   ...

  cancelBooking(booking): void {
     booking.cancelled = !booking.cancelled 
     // or booking.cancelled = false; or whatever you want to do
   }
  }

Now, you have the change in the bookings array.现在,您在 bookings 数组中进行了更改。 Next, you should implement a service to rewrite a json file.接下来,您应该实现一个服务来重写 json 文件。

https://stackoverflow.com/a/52242184/11218448 https://stackoverflow.com/a/52242184/11218448

I think it is as simple as:我认为这很简单:

    cancelBooking(booking:Booking){
 booking.cancelled = true;
}

And just pass it in the view to the method只需将它在视图中传递给方法

Please use this code in your componenet.ts请在您的componenet.ts中使用此代码

cancelBooking(index): void {
  //  Help
  this.bookings[index].cancelled = true;
  }

Template: Change the loop code with below code:模板:使用以下代码更改循环代码:

<tr *ngFor="let booking of bookings; let i = index">

   <td>{{booking.firstName}}</td>
   <td>{{booking.lastName}}</td>
   <td>{{booking.carReg}}</td>
   <td>{{booking.parkingNumber}}</td>
   <td>{{booking.date}}</td>
   <td>
     <div *ngIf="booking.cancelled" class="red">Cancelled</div>
     <div *ngIf="!booking.cancelled" class="green">Booked</div>
   </td>
   <td>
     <button class="btn btn-danger mr-2" (click)="cancelBooking(i)">Cancel</button>
   </td>
 </tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据 JSON 文件中的布尔值自动更改文本 - How to automatically change text depending on boolean value in JSON file 如何根据 JSON 文件中的布尔值自动更改 img - How to automatically change an img depending on boolean value in JSON file 如何使用 angular 写入或更改 json 文件中的值 - how to write or change a value in json file with angular 如何使用算术更改 JSON 文件的值? - How to change the value of a JSON file using arithmetics? 如何将本地 JSON 读入 Angular typescript 文件 - How to read a local JSON into Angular typescript file 无法访问 JSON 数组中的内部 JSON 值 - Typescript(使用 Angular 8) - Unable to access inner JSON value in JSON array - Typescript(Using Angular 8) Angular 2,使用打字稿创建字典 <key, value> 并加载json文件以将数据放入其中 - Angular 2 , create dictionary using typescript <key, value> and load json file to put data in it angular 中的 typescript 中的 json 文件的迭代? - Iteration of json file in typescript in angular? 如何使用angular4从appsettings.json到打字稿文件中读取键值 - How to read key values from appsettings.json to typescript file using angular4 如何使用 TypeScript ZC31C335EF37283C71DEZ1 从 JSON 文件中仅检索一个 object? - How to retrieve just one object from JSON file using TypeScript Angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM