如何在更新数据时修正角度

时间:2019-07-02 05:46:08

标签: php angular

我无法更新数据。我的代码有什么问题?我花了很多时间,试图解决这个问题,但是什么也没有发生。我感到非常复杂,因为我对此没有任何指导。我是处理此类应用程序的初学者,所以我现在需要指导者来解决此问题

HTML

<tbody *ngIf="ediTradingPartners?.length">
  <tr *ngFor="let data of ediTradingPartners">

    <td>
      <div *ngIf="!showMyContainer">
        {{ data.tp_name }}
      </div>
      <textarea class="cell-data" *ngIf="showMyContainer">{{ data.tp_name }}</textarea>
    </td>
    <td>
      <div *ngIf="!showMyContainer">
        {{ data.interchange_id }}
      </div>
      <textarea class="cell-data" value="PUT" *ngIf="showMyContainer">{{ data.interchange_id }}</textarea>
    </td>
    <td>
      <div *ngIf="!showMyContainer">
        {{ data.creation_date }}
      </div>
      <textarea class="cell-data" *ngIf="showMyContainer"> {{ data.creation_date }} </textarea>
    </td>
    <td>

      <div *ngIf="!showMyContainer">
        {{ data.created_by }}
      </div>
      <textarea class="cell-data" *ngIf="showMyContainer"> {{ data.created_by }} </textarea>
    </td>
    <td>
      <div *ngIf="!showMyContainer">
        {{ data.update_date }}
      </div>
      <textarea class="cell-data" *ngIf="showMyContainer">{{ data.update_date }}</textarea>

    </td>
    <td>
      <div *ngIf="!showMyContainer">
        {{ data.updated_by }}
      </div>
      <textarea class="cell-data" *ngIf="showMyContainer"> {{ data.updated_by }}</textarea>
    </td>
    <td>
      <div *ngIf="!showMyContainer">
        {{ data.tmx_interchange_id }}
      </div>
      <textarea *ngIf="showMyContainer" class="cell-data">{{ data.tmx_interchange_id }}</textarea>
    </td>

    <td class="btn-group" n>
      <button type="button" class="btn btn-success" (click)="showMyContainer=!showMyContainer" *ngIf="showMyContainer" (click)="save(data)">        
        <i class="fa fa-save"></i>
      </button>
      <button type="button" (click)="showMyContainer=!showMyContainer" class="btn btn-warning">     
        <i class="fa fa-edit"></i>
      </button>
      <button type="button" class="btn btn-danger">
        <i class="fa fa-trash"></i>
      </button>

    </td>

  </tr>
</tbody>

TypeScript

export class ListOfPartnersComponent implements OnInit {
  editForm: FormGroup;
  ediTradingPartners: EdiTradingPartner[];

  id: string;
  tp_name: string;
  interchange_id: string;
  tmx_interchange_id: string;
  data: any;

  mode: boolean;
  showMyContainer: boolean = false;

  constructor(private ediTradingPartnerService: EdiTradingPartnerService,
    private router: Router,
    private formBuilder: FormBuilder,
    private plat: PlatformLocation) {
    this.id = "";
    this.tp_name = "";
    this.interchange_id = "";
    this.tmx_interchange_id = "";
  }
  get f() {
    return this.editForm.controls;
  }
  ngOnInit() {

    this.ediTradingPartnerService.getEdiTradingPartners()
      .subscribe(EdiTradingPartner => this.ediTradingPartners = EdiTradingPartner);

    if (localStorage.getItem("userType") != "guest") {
      this.mode = true;
    }

  }
  getUser(EdiTradingPartnerId) {
    this.ediTradingPartnerService.getEdiTradingPartnerById(parseInt(EdiTradingPartnerId))
      .subscribe(data => {
        this.ediTradingPartners = data;
        console.log(this.ediTradingPartners)
        this.f.id.setValue(this.ediTradingPartners[0].id);
        this.f.interchange_id.setValue(this.ediTradingPartners[0].tp_name);
        this.f.tp_name.setValue(this.ediTradingPartners[0].interchange_id);
        this.f.tmx_interchange_id.setValue(this.ediTradingPartners[0].tmx_interchange_id);

      });
  }

  save(data): void {

    this.interchange_id = data.interchange_id;
    this.tp_name = data.tp_name;
    this.tmx_interchange_id = data.tmx_interchange_id;


    this.ediTradingPartnerService
      .updateEdiTradingPartner(this.tp_name, this.interchange_id, this.tmx_interchange_id)
      .subscribe(() => this.reloadComp());
  }

  reloadComp(): void {

    this.router.navigateByUrl('', {
      skipLocationChange: true
    }).then(() =>
      this.router.navigate(["/ediTradingPartners"]));
  }
}

服务

export class EdiTradingPartnerService {

  EdiTradingPartner: EdiTradingPartner;

  constructor(private http: HttpClient, private h1: Http) {}

  baseUrl: string = 'http://cbintranet3/edireport/editracker-api/index.php/edi_trading_partner';

  // Get all EDI Trading Partners
  getEdiTradingPartners() {
    return this.http.get < EdiTradingPartner[] > (this.baseUrl);
  }

  add(tp_name, interchange_id, tmx_interchange_id) {

    var headers = new HttpHeaders();
    headers.append('Content-Type', 'application/X-www-form=urlencoded');
    console.log(EdiTradingPartner);


    // Add an EDI Trading Partner


    return this.http.post(this.baseUrl + '/add', {
      tp_name: tp_name,
      interchange_id: interchange_id,
      tmx_interchange_id: tmx_interchange_id
    }, {
      responseType: 'text'
    });

  }

  // Delete an EDI Trading Partner
  deleteEdiTradingPartner(edi_trading_partner_id: number) {

    return this.http.delete(this.baseUrl + '/' + edi_trading_partner_id)
  }

  getEdiTradingPartnerById(id: number) {
    ///
    return this.h1.get(this.baseUrl + '/' + id)
      .map((data: Response) => {
        return data.json();
      })
  }

  updateEdiTradingPartner(interchange_id, tp_name, tmx_interchange_id) {

    var headers = new HttpHeaders();
    headers.append('Content-Type', 'application/X-www-form=urlencoded');
    let EdiTradingPartnerId = localStorage.getItem("EdiTradingPartnerId");

    return this.http.put(this.baseUrl + '/update/' + parseInt(EdiTradingPartnerId), {
      interchange_id: interchange_id,
      tp_name: tp_name,
      tmx_interchange_id: interchange_id
    }, {
      responseType: 'text'
    });
  }
}

API

<?php

  // Call database connection
  require_once('dbconnect.php');

// Display all records
  $app->get('/edi_trading_partner', function($req, $res) {

    $query = "SELECT *
          FROM edi_trading_partner
          order by id";
    $result = mysql_query($query);

    while($row = mysql_fetch_assoc($result)) {

      $data[] = $row;
    }

    if(isset($data)) {

      return $res->withJson($data);
      return json_encode($data, JSON_PRETTY_PRINT);
    }

    mysql_close($conn);
  });
  // Display single row
  $app->get('/edi_trading_partner/select/{id}', function($res) {

    $id = $res->getAttribute('id');


    $query = "SELECT *
          FROM edi_trading_partner
          WHERE id=$id";

    $result = mysql_query($query);

    while($row = mysql_fetch_assoc($result)) {

      $data[] = $row;
    }

    if(isset($data)) {

      return json_encode($data);
      return $res->withJson($data);
    } else {

      return json_encode($err_msg);
    }

    mysql_close($conn);

  });

  // Add new trading partner
  $app->post('/edi_trading_partner/add', function() {

    $adddata=json_decode(file_get_contents("php://input"));
    if(sizeof($adddata)!=0){

      $tp_name=$adddata->tp_name;
      $interchange_id=$adddata->interchange_id;
      $tmx_interchange_id=$adddata->tmx_interchange_id;
      $num = 0;
 $count = "SELECT * FROM edi_user order by id";
     $result1 = mysql_query($count);
    while($row = mysql_fetch_assoc($result1)) {
      $num++;
    }
    $num++;

      $query = "INSERT into edi_trading_partner (
            interchange_id,
            tp_name,
            tp_location,
            flag,
            creation_date,
            created_by,
            update_date,
            updated_by,
            tmx_interchange_id)
            values (
            '$interchange_id', 
            '$tp_name', 
            '$tp_location', 
            '$flag',
            '$creation_date',
            '$created_by', 
            '$update_date',
            '$updated_by',
            '$tmx_interchange_id')";

      $result = mysql_query($query) or die(json_encode(array('error' => 'error lang'), JSON_PRETTY_PRINT));

      echo json_encode(array('success' => 'added'));

      if(isset($result)) {
        header('Content-Type: application/json');
        echo json_encode($result);
      }

    mysql_close($conn);

    }
  });

  // Delete trading partner
  $app->map(['GET', 'DELETE'],'/edi_trading_partner/delete/{edi_trading_partner_id}', function($req, $res, $args) {

    $edi_trading_partner_id = $req->getAttribute('edi_trading_partner_id');

    $query = "DELETE from edi_trading_partner
          WHERE edi_trading_partner_id='$edi_trading_partner_id'";

    $result = mysql_query($query);

    if(!$result) {

      return json_encode(
        array("message" => "Unable to delete this trading partner!")
      );
    } else {

      return json_encode(
        array("message" => "Trading partner successfully deleted!")
      );


    }
  });
  //Update a EDI
  $app->map(['GET', 'PUT'], '/edi_trading_partner/update/{id}', function($req, $res, $args) {

  $updatedata=json_decode(file_get_contents("php://input"));
  if(sizeof($updatedata)!=0){

   $id = $req->getAttribute('id');
   $tp_name=$updatedata->interchange_id;
   $interchange_id=$updatedata->tp_name; 
   $tmx_interchange_id=$updatedata->tmx_interchange_id;
  $query = "UPDATE edi_trading_partner SET interchange_id = '$interchange_id', tp_name = '$tp_name',, tmx_interchange_id = '$tmx_interchange_id' WHERE id='$id'";

  $result = mysql_query($query) or die(json_encode(array('error' => 'error lang'), JSON_PRETTY_PRINT));


  if(isset($result)) {
        header('Content-Type: application/json');
        echo json_encode($result);
      }
    mysql_close($conn);
  }
});

有人可以帮我吗?我觉得这段代码太复杂了。如果您愿意帮助我,请检查我的代码。谢谢

0 个答案:

没有答案