【问题标题】:How to update record onclick of button for particular user?如何更新特定用户的按钮点击记录?
【发布时间】:2020-03-09 11:51:28
【问题描述】:

我是角度方面的新手。我正在使用角度 CLI 8.1.0。我的 sql 表中有用户列表,每个用户都有“状态”列。我通过rest api和php mysql在mat表上列出了用户列表。单击任何记录后,它的相应详细信息将在 url 参数的帮助下显示在新页面上。现在我的新页面上有 2 个按钮“批准”和“拒绝”。当我单击批准该用户的状态列应从“待定”更改为“批准”(拒绝相同)。我的后端代码在邮递员上运行良好。但无法在角度上实现

approve.component.html

<button mat-stroked-button class="b1" (click)="approve()">Approve</button>
    <button mat-stroked-button class="b2" (click)="reject()">Reject</button>

approve.component.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { ApiService } from 'src/app/api.service';

@Component({
  selector: 'app-approval',
  templateUrl: './approval.component.html',
  styleUrls: ['./approval.component.css']
})
export class ApprovalComponent implements OnInit {

  constructor(private activatedRoute:ActivatedRoute,private apiService:ApiService,private router:Router) { }

  id:any;
  result:any;

  ngOnInit() 
  {
    this.id=this.activatedRoute.snapshot.paramMap.get('id');
    //console.log(this.id);

    this.activatedRoute.queryParamMap.subscribe((queryParams:Params)=>{
      let vendorId=this.id;      
      this.apiService.getVendorById(vendorId)
      .subscribe(data=>{
        this.result = data[0];      

      });
    });
  }
  approve()
  {

    this.router.navigate(["/home/vendor-action"]);
  }
  reject()
  {
    this.router.navigate(["/home/vendor-action"]);
  }
}

api.service.ts

import { Injectable, Output,EventEmitter } from '@angular/core';
import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { Users } from './users';
import { Observable } from 'rxjs';

import { Displayvendor } from './adminpanel/home/vendor-action/displayvendor';

@Injectable({
  providedIn: 'root'
})
export class ApiService {
  redirectUrl:string;
  baseUrl:string="http://localhost/repos/Sportaz-repo/VaamozWeb/VaamozBusiness/RestApi/VaamozStore/AdminStore/angular_admin/php";
  @Output() getLoggedInName: EventEmitter<any> = new EventEmitter();

  private static URL = 'http://localhost/repos/Sportaz-repo/VaamozWeb/VaamozBusiness/RestApi/VaamozStore/AdminStore/angular_admin/php/index.php';

  constructor(private httpClient : HttpClient) { }

  getVendorById(data)
  {
    return this.httpClient.get('http://localhost/repos/Sportaz-repo/VaamozWeb/VaamozBusiness/RestApi/VaamozStore/AdminStore/angular_admin/php/index.php?id='+data);
  }

  updateById(data)
  {
    return this.httpClient.get('http://localhost/repos/Sportaz-repo/VaamozWeb/VaamozBusiness/RestApi/VaamozStore/AdminStore/angular_admin/php/index.php?id='+data);
  }
}

index.php

<?php
    $conn=mysqli_connect("localhost","root","root","angdb");

    $request=$_SERVER['REQUEST_METHOD'];

    $data=array();
    switch($request)
    {
        case 'GET':
            response(getData());
            break;

        case 'PUT':
            response(updateData());

        default:
            #code...
            break;
    }

    function getData()
    {
        global $conn;

        if(@$_GET['id'])
        {
            @$id=$_GET['id'];

            $where="AND id=".$id;
        }
        else
        {
            $id=0;
            $where="";
        }


        $query=mysqli_query($conn,"select * from vendor where status='pending' ".$where);
        while($row=mysqli_fetch_assoc($query))
        {
            $data[]=array("id"=>$row['id'],"changeColumn"=>$row['changeColumn'],"type"=>$row['type'],"timestamp"=>$row['timestamp'],"status"=>$row['status'],"name"=>$row['name']);
        }
        return $data;
    }

    function updateData()
    {
        global $conn;
        parse_str(file_get_contents('php://input'),$_PUT);

        if(@$_GET['id'])
        {
            @$id=$_GET['id'];

            $where="where id=".$id;
        }
        else
        {
            $id=0;
            $where="";
        }

        $query=mysqli_query($conn,"update vendor set status='".$_PUT['status']."'".$where);

        if($query==true)
        {
            $data[]=array("Message"=>"Updated");
        }
        else
        {
            $data[]=array("Message"=>"Not updated");
        }
        return $data;
    }

    function response($data)
    {
        echo json_encode($data);
    }
?>

【问题讨论】:

    标签: angular typescript frontend


    【解决方案1】:

    问题是您没有将任何数据传递到后端,也没有在服务器上调用 PUT 方法来更新状态。

    首先,修复您的组件,使其将正确的状态代码传递给服务并等待操作完成,然后再重定向到新页面。例如,

    async approve()
    {
        await this.apiService.updateData(this.id, { status: 'Approve' });
        this.router.navigate([ "/home/vendor-action" ]);
    }
    
    async reject()
    {
        await this.apiService.updateData(this.id, { status: 'Reject' });
        this.router.navigate([ "/home/vendor-action" ]);
    }
    

    其次,更新您服务的updateData 方法以允许有效负载。

    【讨论】:

    • 预期 1 个参数,但显示 2.ts(2554) 错误
    【解决方案2】:

    对于批准和拒绝这两种方法,您应该通过 id 和新状态调用 REST API 服务

    例如

    approve() {
        this.apiService.updateById(this.id, {status:'approve'})
        .subscribe((data:any)=> {
            if (data.Message == "Updated") { // check if the result is sucess then navigat to
                this.router.navigate(["/home/vendor-action"]);
            }
        });
    }
    reject() {
        this.apiService.updateById(this.id, {status:'reject'})
        .subscribe((data:any)=> {
            if (data.Message == "Updated") { // check if the result is sucess then navigat to
                this.router.navigate(["/home/vendor-action"]);
            }
        });
    }
    

    并将您的服务方法也编辑成这样

    updateById(id,payload)
     {
        let url = `http://localhost/repos/Sportaz-repo/VaamozWeb/VaamozBusiness/RestApi/VaamozStore/AdminStore/angular_admin/php/index.php?id=${id}`
        return this.http.put(url, payload);
     }
    

    或者您可以将其作为您的 php 中的参数接收

    updateById(id,obj) {
        let url = `http://localhost/repos/Sportaz-repo/VaamozWeb/VaamozBusiness/RestApi/VaamozStore/AdminStore/angular_admin/php/index.php?id=${id}`
        return this.httpClient.put(url, {}, {params: { status:obj.status } }  )
    }
    

    【讨论】:

    • 关于批准方法预期有 1 个参数,但出现 2.ts(2554) 错误即将到来。当我删除“批准”时,如果条件“属性'结果'不存在类型'对象'.ts(2339)”错误显示
    • 现在只有这个错误显示“对象”类型上不存在属性“结果”.ts(2339)
    • 第一个错误,您必须更改方法updateById 以接受 2 个参数或仅将一个参数作为对象传递。并且第二个错误将data 更改为(data:any) 并且此行仅在导航前检查条件并且如果不需要它可以忽略它
    • 构造函数(私有激活路由:激活路由,私有 apiService:ApiService,私有路由器:路由器){ } id:any;数据:任何;结果:任何;写了但还是一样的错误
    • 不,我的意思是 observable 的数据参数,我更新了帖子,再检查一遍
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    • 2020-01-14
    • 2021-02-25
    相关资源
    最近更新 更多