2014-10-20 62 views
18

我學習角js,我發現我問題。如何修改和更新角js中的數據錶行?

我創建一個新的項目。

我有一些按鈕編輯,添加,刪除,

如果我點擊我的編輯按鈕不是顯示所有的領域,但我想只顯示當前字段比我點擊更新更新此申請。

我的代碼是在這裏

Anguar

var app = angular.module('addApp', []); 

app.controller('modifyCtrl', ['$scope', function($scope){ 
    $scope.tabelsData= [ 
     {'name':'rohit', 'dob':'15-august-1985', 'emailId':'[email protected]', 'phone':'9999999999', 'address':'Delhi Rohini', 'id':'0' }, 
     {'name':'aman', 'dob':'26-july-1975', 'emailId':'[email protected]', 'phone':'', 'address':'Haryana Sonepat', 'id':'1' }, 
     {'name':'devraj', 'dob':'27-march-1980', 'emailId':'[email protected]', 'phone':'7410258963', 'address':'Punjab AmritSar', 'id':'2' } 
    ]; 


    $scope.modify = function(tableData){ 

     $scope.modifyField = true; 
     $scope.viewField = true; 
    }; 


    $scope.update = function(tableData){ 
     $scope.modifyField = false; 
     $scope.viewField = false; 
    }; 

}]); 

HTML代碼

<div ng-app="addApp"> 

<div class="wraper" ng-controller="modifyCtrl"> 

      <table> 
       <thead> 
        <tr> 
         <th>Name:</th> 
         <th>Date Of Birth</th> 
         <th>Email Id</th> 
         <th>Phone No.</th> 
         <th>Address</th> 
         <th>Action</th> 
        </tr> 
       </thead> 
       <tbody> 
        <tr ng-repeat="tableData in tabelsData"><form> 
         <td> 
          <div ng-hide="viewField">{{tableData.name | uppercase}}</div> 
          <div ng-show="modifyField"><input type="text" ng-model="tableData.name" /></div> 
         </td> 
         <td> 
          <div ng-hide="viewField">{{tableData.dob}}</div> 
          <div ng-show="modifyField"><input type="text" ng-model="tableData.dob" /></div> 
         </td> 
         <td> 
          <div ng-hide="viewField">{{tableData.emailId}}</div> 
          <div ng-show="modifyField"><input type="text" ng-model="tableData.emailId" /></div> 
         </td> 
         <td> 
          <div ng-hide="viewField">{{tableData.phone}}</div> 
          <div ng-show="modifyField"><input type="text" ng-model="tableData.phone" /></div> 
         </td> 
         <td> 
          <div ng-hide="viewField">{{tableData.address}}</div> 
          <div ng-show="modifyField"> 
           <textarea ng-model="tableData.address"></textarea> 
          </div> 
         </td> 
         <td> 
          <button ng-hide="viewField" ng-click="modify(tableData)">Modify</button> 
          <button ng-show="modifyField" ng-click="update(tableData)">Update</button> 
          <button ng-hide="viewField">Add</button> 
          <button ng-hide="viewField">Remove</button> 
         </td></form> 
        </tr> 
       </tbody> 
      </table> 

     </div> 

</div> 

\t \t var app = angular.module('addApp', []); 
 

 
\t \t app.controller('modifyCtrl', ['$scope', function($scope){ 
 
\t \t \t $scope.tabelsData= [ 
 
\t \t \t \t {'name':'rohit', 'dob':'15-august-1985', 'emailId':'[email protected]', 'phone':'9999999999', 'address':'Delhi Rohini', 'id':'0' }, 
 
\t \t \t \t {'name':'aman', 'dob':'26-july-1975', 'emailId':'[email protected]', 'phone':'', 'address':'Haryana Sonepat', 'id':'1' }, 
 
\t \t \t \t {'name':'devraj', 'dob':'27-march-1980', 'emailId':'[email protected]', 'phone':'7410258963', 'address':'Punjab AmritSar', 'id':'2' } 
 
\t \t \t ]; 
 

 

 
\t \t \t $scope.modify = function(tableData){ 
 

 
\t \t \t \t $scope.modifyField = true; 
 
\t \t \t \t $scope.viewField = true; 
 
\t \t \t }; 
 

 

 
\t \t \t $scope.update = function(tableData){ 
 
\t \t \t \t $scope.modifyField = false; 
 
\t \t \t \t $scope.viewField = false; 
 
\t \t \t }; 
 

 
\t \t }]); 
 
\t \t
table td, table th{ 
 
    
 
    border:solid 1px red; 
 
    padding:5px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 

 
<div ng-app="addApp"> 
 

 
<div class="wraper" ng-controller="modifyCtrl"> 
 

 
\t \t \t <table> 
 
\t \t \t \t <thead> 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <th>Name:</th> 
 
\t \t \t \t \t \t <th>Date Of Birth</th> 
 
\t \t \t \t \t \t <th>Email Id</th> 
 
\t \t \t \t \t \t <th>Phone No.</th> 
 
\t \t \t \t \t \t <th>Address</th> 
 
\t \t \t \t \t \t <th>Action</th> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t </thead> 
 
\t \t \t \t <tbody> 
 
\t \t \t \t \t <tr ng-repeat="tableData in tabelsData"><form> 
 
\t \t \t \t \t \t <td> 
 
\t \t \t \t \t \t \t <div ng-hide="viewField">{{tableData.name | uppercase}}</div> 
 
\t \t \t \t \t \t \t <div ng-show="modifyField"><input type="text" ng-model="tableData.name" /></div> 
 
\t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t <td> 
 
\t \t \t \t \t \t \t <div ng-hide="viewField">{{tableData.dob}}</div> 
 
\t \t \t \t \t \t \t <div ng-show="modifyField"><input type="text" ng-model="tableData.dob" /></div> 
 
\t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t <td> 
 
\t \t \t \t \t \t \t <div ng-hide="viewField">{{tableData.emailId}}</div> 
 
\t \t \t \t \t \t \t <div ng-show="modifyField"><input type="text" ng-model="tableData.emailId" /></div> 
 
\t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t <td> 
 
\t \t \t \t \t \t \t <div ng-hide="viewField">{{tableData.phone}}</div> 
 
\t \t \t \t \t \t \t <div ng-show="modifyField"><input type="text" ng-model="tableData.phone" /></div> 
 
\t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t <td> 
 
\t \t \t \t \t \t \t <div ng-hide="viewField">{{tableData.address}}</div> 
 
\t \t \t \t \t \t \t <div ng-show="modifyField"> 
 
\t \t \t \t \t \t \t \t <textarea ng-model="tableData.address"></textarea> 
 
\t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t <td> 
 
\t \t \t \t \t \t \t <button ng-hide="viewField" ng-click="modify(tableData)">Modify</button> 
 
\t \t \t \t \t \t \t <button ng-show="modifyField" ng-click="update(tableData)">Update</button> 
 
\t \t \t \t \t \t \t <button ng-hide="viewField">Add</button> 
 
\t \t \t \t \t \t \t <button ng-hide="viewField">Remove</button> 
 
\t \t \t \t \t \t </td></form> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t </tbody> 
 
\t \t \t </table> 
 

 
\t \t </div> 
 

 
</div>

回答

21

如果你只想要一個行上顯示點擊其相應的修改按鈕,輸入你有兩個選擇:

1)將布爾到每個tabelsData陣列的JSON指標。

2)製作容納這些布爾值的鏡像陣列。

有兩個獨立的布爾在這種情況下是沒有用的,因爲每一個被撥動的基礎上處理:

這裏是幹什麼的做法第二把交椅,因爲我想你想你的數據保持相同的核心代碼:

JS:

$scope.editingData = {}; 

for (var i = 0, length = $scope.tabelsData.length; i < length; i++) { 
    $scope.editingData[$scope.tabelsData[i].id] = false; 
} 

$scope.modify = function(tableData){ 
    $scope.editingData[tableData.id] = true; 
}; 


$scope.update = function(tableData){ 
    $scope.editingData[tableData.id] = false; 
}; 

HTML:

<tbody> 
    <tr ng-repeat="tableData in tabelsData"> 
     <td> 
      <div ng-hide="editingData[tableData.id]">{{tableData.name | uppercase}}</div> 
      <div ng-show="editingData[tableData.id]"><input type="text" ng-model="tableData.name" /></div> 
     </td> 
     <td> 
      <div ng-hide="editingData[tableData.id]">{{tableData.dob}}</div> 
      <div ng-show="editingData[tableData.id]"><input type="text" ng-model="tableData.dob" /></div> 
     </td> 
     <td> 
      <div ng-hide="editingData[tableData.id]">{{tableData.emailId}}</div> 
      <div ng-show="editingData[tableData.id]"><input type="text" ng-model="tableData.emailId" /></div> 
     </td> 
     <td> 
      <div ng-hide="editingData[tableData.id]">{{tableData.phone}}</div> 
      <div ng-show="editingData[tableData.id]"><input type="text" ng-model="tableData.phone" /></div> 
     </td> 
     <td> 
      <div ng-hide="editingData[tableData.id]">{{tableData.address}}</div> 
      <div ng-show="editingData[tableData.id]"> 
       <textarea ng-model="tableData.address"></textarea> 
      </div> 
     </td> 
     <td> 
      <button ng-hide="editingData[tableData.id]" ng-click="modify(tableData)">Modify</button> 
      <button ng-show="editingData[tableData.id]" ng-click="update(tableData)">Update</button> 
      <button ng-hide="viewField">Add</button> 
      <button ng-hide="viewField">Remove</button> 
     </td> 
    </tr> 
</tbody> 

我提出的示例: http://plnkr.co/edit/lXq1WB

0

這裏是angular2一個例子:

fichier.html: 
    <ng2-toasty [position]="'top-left'"></ng2-toasty> 
    <label for="trainingInput" class="mr-2">{{ 'LABEL.FORMATION' | translate }} :</label> 
    <table class="table table-hover table-striped table-sortable table-bordered"> 
     <thead> 
      <tr> 
       <th *ngFor="let column of columns" [class]="selectedClass(column.variable)" (click)="changeSorting(column.variable)" translate> 
        {{column.display}} 
       </th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr *ngFor="let object of data | orderBy : convertSorting(); let rowIndex = index"> 
       <td *ngFor="let column of columns" class="{{column.variable}}-td"> 
        <div *ngIf="!toUpdates[object['id']]" >{{object[column.variable] | format: column.filter}}</div> 
        <div *ngIf="toUpdates[object['id']]"><input type="text" [(ngModel)]="object[column.variable]" ></div> 
       </td> 
       <td class="text-center"> 
        <i *ngIf="!toUpdates[object['id']]" class="fa fa-pencil-square-o edit-formation" aria-hidden="true" (click) = "editFormation(object)"></i> 
        <i *ngIf="toUpdates[object['id']]" class="fa fa-check-square-o save-edit-form" (click)="updateFormation(object)"></i> 
        <i class="fa fa-times" aria-hidden="true" (click)="deleteFormation(object['id'])"></i> 
       </td> 
      </tr> 
      <tr [hidden]="isDisabled()"> 
       <td><input type="text" class="form-control" placeholder="Année" #years></td> 
       <td><input type="text" class="form-control" placeholder="Formation" #label></td> 
       <td><input type="text" class="form-control" placeholder="Durée" #duration></td> 
       <td class="text-center align-middle"> 
       <i class="fa fa-plus-circle fa-2x" (click)="addFormation(years.value, label.value, duration.value)"></i> 
       </td> 
      </tr> 
     </tbody> 
    </table> 

fichier.ts: 
import {Component, Injector, Input, OnChanges, OnInit} from '@angular/core'; 
import { Http, Headers, RequestOptions, URLSearchParams } from '@angular/http'; 
import DynamicComponent from '../dynamic-component'; 
import Formation from './formation'; 
import {ToastyService, ToastyConfig, ToastOptions, ToastData} from 'ng2-toasty'; 


@Component({ 
    moduleId: module.id, 
    selector: 'formations-selector', 
    templateUrl: './formations-template.html', 
    styleUrls: ['./formations-template.css'], 
}) 
export default class FormationsComponent{ 

    candidate: any = null; 
    listFormations: any = null; 
    candidateDetails: any = null; 
    columns: any[]; 
    sort: any; 
    data: any[]; 
    toUpdates: {}; 

    constructor(private injector: Injector, private http: Http,private toastyService: ToastyService, private toastyConfig: ToastyConfig) { 
     this.candidateDetails = this.injector.get('candidateDetails'); 
     this.candidate = this.candidateDetails.candidate; 
     this.listFormations = this.candidateDetails.listFormations; 
     this.columns = this.listFormations.columns; 
     this.sort = this.listFormations.sort; 
     this.data = this.listFormations.data; 
     this.toastyConfig.theme = 'material'; 
     this.toUpdates = {}; 
    } 

    ngAfterViewInit(){ 
     $(document).ready(function() { 
      /* 
      $('.edit-formation').click(function() { 
       var dad = $(this).parent().parent(); 
       dad.find('td .duration-span').hide(); 
       dad.find('td.duration-td').append('<input type="text" class="form-control" placeholder="Durée" value="'+dad.find('td .duration-span').html()+'" id = "duration-update" #durationu>'); 
       dad.find('td .label-span').hide(); 
       dad.find('td.label-td').append('<input type="text" class="form-control" placeholder="Formation" id="label-update" value="'+dad.find('td .label-span').html()+'" #labelu>'); 
       dad.find('td .years-span').hide(); 
       dad.find('td.years-td').append('<input type="text" class="form-control" placeholder="Année" id="years-update" value="'+dad.find('td .years-span').html()+'" #yearsu>'); 

       dad.find('td.years-td').append('<i class="fa fa-check-square-o save-edit-form hidden" (click)="updateFormation(1, years.value, label.value, durationu)"></i>'); 


       dad.find('td .edit-formation').addClass("hidden"); 
       dad.find('td .save-edit-form').removeClass("hidden"); 

      }); 
      */ 
      /* 
      $('.save-edit-form').click(function() { 
       var dad = $(this).parent().parent(); 
       dad.find('td .save-edit-form').addClass("hidden"); 
       dad.find('td .edit-formation ').removeClass("hidden"); 

       dad.find('td .duration-span').show(); 
       $('#duration-update').remove(); 
       dad.find('td .label-span').show(); 
       $('#label-update').remove(); 
       dad.find('td .years-span').show(); 
       $('#years-update').remove(); 
      }); 
      */ 
     }); 
} 


    //Action déclenché lors d'un changement de société du candidat : on met à jour la liste des métiers associés 
    onChangeCompaniesInput(value) { 

    } 

    isDisabled(isDisabled) { 
     //return (isDisabled || !this.candidateDetails.isUserAuthorized) ? true : false; 
    } 

    selectedClass(columnName): string{ 
     return columnName == this.sort.column ? 'sort-' + this.sort.descending : ''; 
    } 

    changeSorting(columnName): void{ 
     var sort = this.sort; 
     if (sort.column == columnName) { 
     sort.descending = !sort.descending; 
     } else { 
     sort.column = columnName; 
     sort.descending = false; 
     } 
    } 

    convertSorting(): string{ 
     return this.sort.descending ? '-' + this.sort.column : this.sort.column; 
    } 

    onChangeMainFormaion(value): void{ 
     console.log(value); 
    } 

    deleteFormation(idFormation): void{ 
     let headers  = new Headers('Content-Type', 'application/json'); 
     let params: URLSearchParams = new URLSearchParams(); 

     this.http.post('/api/formations/'+idFormation+'/deleteFormation', params).toPromise() 
      .then(
        res => 
        { 
         if(res.status == 200){ 
          this.toastyService.success({ 
           title: "Success", 
           msg: "La formation a etait supprmié avec Succès", 
           showClose: true, 
           timeout: 5000, 
           theme: 'default', 
          }); 
         }else{ 
          this.toastyService.error({ 
           title: "Error", 
           msg: "Une erreur est survenue, veuillez réessayer ultérieurement", 
           showClose: true, 
           timeout: 5000, 
           theme: 'default', 
          }); 
         } 
        } 
       ).catch(this.handleError); 
    } 

    editFormation(tableData): void{ 
     this.toUpdates[tableData['id']]= true; 
    } 

    updateFormation(tableData): void { 
     this.toUpdates[tableData['id']]= false; 
     console.log(tableData); 
    } 

    addFormation(years: string, label: string, durration: string, main: boolean = false): void{ 
     let headers  = new Headers('Content-Type', 'application/json'); 
     let params: URLSearchParams = new URLSearchParams(); 
     params.append('years', years); 
     params.append('label', label); 
     params.append('durration', durration); 
     params.append('main', main); 

     //let formation = new Formation(years, label, durration, false); 
     return this.http.post('/api/formations/'+this.candidate.id+'/addFormation', params).toPromise() 
      .then(
        res => 
        { 
         if(res.status == 200){ 
          this.toastyService.success({ 
           title: "Success", 
           msg: "La formation a etait ajouter avec Succès", 
           showClose: true, 
           timeout: 5000, 
           theme: 'default', 
          }); 
         }else{ 
          this.toastyService.error({ 
           title: "Error", 
           msg: "Une erreur est survenue, veuillez réessayer ultérieurement", 
           showClose: true, 
           timeout: 5000, 
           theme: 'default', 
          }); 
         } 
        } 
       ).catch(this.handleError); 
    } 

    private handleError(error: any) { 
     let errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error'; 
     console.error(errMsg); 

     return Promise.reject(errMsg); 
    } 

} 
相關問題