简体   繁体   English

PrimeNG p日历和自动完成值和验证

[英]PrimeNG p-Calendar & AutoComplete Values and Validation

I am using primeNG p-Calendar Control and AutoComplete. 我正在使用primeNG p日历控件和自动完成。 Here is my Code (blunker) (click on edit then save") I have two issues: 这是我的代码(更笨拙) (单击“编辑”,然后单击“保存”)。我有两个问题:

  • I can't get the newly selected Date for whatever reason..no matter what I do I always get the old date. 无论出于什么原因,我都无法获取新选择的日期。无论我做什么,我总是会得到旧日期。
  • I need to be able to check if both auto-complete or calendar are empty. 我需要能够检查自动填写或日历是否为空。 if anyone is empty "save" button should be disabled and the span (required) should appear next to the empty field 如果任何人为空,则应禁用“保存”按钮,并且跨度(必填)应显示在空字段旁边

HTML HTML

<p-table #dt [value]="iBsaFollowup" dataKey="id" [paginator]="true" [rowsPerPageOptions]="[10,50,100]" [rows]="10">

    <ng-template pTemplate="header">
        <tr>
            <th width="5%">eRSA ID</th>
            <th width="15%">Access For</th>
            <th width="17%">Followup DT</th>
            <th width="33%">Comment</th>
            <th width="20%">BSA Name</th>
<th width="10%">Action</th>         

        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-row>
        <tr>
            <td>{{row.ersaID}}</td>
            <td>{{row.accessFor}}</td>


            <td>

                <div>
                    <p-calendar name="followupDate" [(ngModel)]="row.followupDate" [dataType]="date" [showIcon]="true"></p-calendar> <span style="margin-left:35px"></span>
          <span  style="color:crimson; font-size:8pt">Required</span>
                </div>
            </td>
            <td>
                <div>{{row.comment}}</div>

            </td>


            <td>
                                                    <div *ngIf="!row.isBSAEditable">{{row.bsaName}}</div>
                                             <div *ngIf="row.isBSAEditable">
                    <p-autoComplete name="bsaNameList" [(ngModel)]="bsaListVal" [suggestions]="iBsaList" (completeMethod)="searchBsaList($event)"
                     [style]="{'width':'85%'}" [inputStyle]="{'width':'85%'}" field="name" dataKey="id" [dropdown]="true"></p-autoComplete>
           <span  style="color:crimson; font-size:8pt">Required</span>
                    </div>

            </td>
            <td>
        <button  (click)="onSelectedFollowupdRow(row)">Edit</button>
<button (click)="editRow(row)" >Save</button>

        </tr>
    </ng-template>

</p-table>

Component 零件

bsaListVal: IBsaList;
  iBsaList: IBsaList[];
  originalBsaList: IBsaList[];
  iBsaFollowup: any[];

  ngOnInit(): void {
    this.getBSAFollowup();
    this.GetBsaList();
  }
  searchBsaList(event) {
    this.iBsaList = this.originalBsaList;
    this.iBsaList = this.iBsaList
      .filter(data => data.name.toString()
        .toLowerCase()
        .indexOf(event.query.toString().toLowerCase()) !== -1);

  }

  GetBsaList() {
    var data = {"result":[{"id":"1","name":"Mike S"},{"id":"2","name":"John B"},{"id":"3","name":"Adam P"},{"id":"4","name":"Sam J"},{"id":"5","name":"Carolyn G"},{"id":"6","name":"Steve C"}]};
    this.iBsaList = data.result;
    this.originalBsaList = data.result;
  }

  getBSAFollowup() {
    var data = { "result":[{"id":"x","ersaID":"XXX","bsaID":"5","followupDate":"10/20/2017","active":true,"comment":"test this rsss 2","accessFor":"XXXX","bsaName":"Carolyn G"},{"id":"X","ersaID":"XXXX","bsaID":"1","followupDate":"10/27/2017","active":true,"comment":"test this rsss 4","accessFor":"XXXXX","bsaName":"Mike S"}]};

    this.iBsaFollowup = data.result;
    this.iBsaFollowup.map(row => {
      row.isBSAEditable = false;
    });
  }
onSelectedFollowupdRow(row) {
      //  console.log('one row select ');

        this.iBsaFollowup.filter(row => row.isBSAEditable).map(r => { r.isBSAEditable = false; return r })
        row.isBSAEditable = true;
        this.bsaListVal = {
            id: row.id,
            name: row.bsaName
        };

    }
      editRow(row) {

     console.log(' BSAid:' + this.bsaListVal.id);
         console.log('BSA name:' + this.bsaListVal.name);
         console.log('date  :' + row.followupDate);

        // with this you set all the rows as no editable, so you can change from row to row with the edit button
       //print selected bsa ID (auto complete ID)
     }
}

export interface IBsaList {
  id: string,
  name: string
}

********************************************************UPDATE*************************************************** now i get the new date (but i need it to be formatted //mm/dd/yyy) here is also the updated code (blunker) HTML ************************************************** ****** UPDATE ******************************************* ********现在我得到了新的日期(但是我需要将其格式化// mm / dd / yyy),这里也是更新的代码(笨拙的) HTML

<p-table #dt [value]="iBsaFollowup" dataKey="id" [paginator]="true" [rowsPerPageOptions]="[10,50,100]" [rows]="10">

    <ng-template pTemplate="header">
        <tr>
            <th width="5%">eRSA ID</th>
            <th width="15%">Access For</th>
            <th width="17%">Followup DT</th>
            <th width="33%">Comment</th>
            <th width="20%">BSA Name</th>
<th width="10%">Action</th>         

        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-row>
        <tr>
            <td>{{row.ersaID}}</td>
            <td>{{row.accessFor}}</td>


            <td>

                <div>
                    <p-calendar name="followupDate" [(ngModel)]="newfollowupDate"  [showIcon]="true"></p-calendar> <span style="margin-left:35px"></span>
          <span  style="color:crimson; font-size:8pt">Required</span>
                </div>
            </td>
            <td>
                <div>{{row.comment}}</div>

            </td>


            <td>
                                                    <div *ngIf="!row.isBSAEditable">{{row.bsaName}}</div>
                                             <div *ngIf="row.isBSAEditable">
                    <p-autoComplete name="bsaNameList" [(ngModel)]="bsaListVal" [suggestions]="iBsaList" (completeMethod)="searchBsaList($event)"
                     [style]="{'width':'85%'}" [inputStyle]="{'width':'85%'}" field="name" dataKey="id" [dropdown]="true"></p-autoComplete>
           <span  style="color:crimson; font-size:8pt">Required</span>
                    </div>

            </td>
            <td>
        <button  (click)="onSelectedFollowupdRow(row)"  >Edit</button>
<button (click)="editRow(row)" [disabled]="isValid(row)">Save</button>

        </tr>
    </ng-template>

</p-table>

Component 零件

 bsaListVal: IBsaList;
  iBsaList: IBsaList[];
  originalBsaList: IBsaList[];
  iBsaFollowup: any[];
  newfollowupDate:Date;

  ngOnInit(): void {
    this.getBSAFollowup();
    this.GetBsaList();
  }
  searchBsaList(event) {
    this.iBsaList = this.originalBsaList;
    this.iBsaList = this.iBsaList
      .filter(data => data.name.toString()
        .toLowerCase()
        .indexOf(event.query.toString().toLowerCase()) !== -1);

  }

  GetBsaList() {
    var data = {"result":[{"id":"1","name":"Mike S"},{"id":"2","name":"John B"},{"id":"3","name":"Adam P"},{"id":"4","name":"Sam J"},{"id":"5","name":"Carolyn G"},{"id":"6","name":"Steve C"}]};
    this.iBsaList = data.result;
    this.originalBsaList = data.result;
  }

  getBSAFollowup() {
    var data = { "result":[{"id":"x","ersaID":"XXX","bsaID":"5","followupDate":"10/20/2017","active":true,"comment":"test this rsss 2","accessFor":"XXXX","bsaName":"Carolyn G"},{"id":"X","ersaID":"XXXX","bsaID":"1","followupDate":"10/27/2017","active":true,"comment":"test this rsss 4","accessFor":"XXXXX","bsaName":"Mike S"}]};

    this.iBsaFollowup = data.result;
    this.iBsaFollowup.map(row => {
      row.isBSAEditable = false;
    });
  }

  isValid(row) {
   if (this.newfollowupDate && this.bsaListVal) {
     return false;
   }
   return true;
 }
onSelectedFollowupdRow(row) {
      //  console.log('one row select ');

        this.iBsaFollowup.filter(row => row.isBSAEditable).map(r => { r.isBSAEditable = false; return r })
        row.isBSAEditable = true;
        this.newfollowupDate=row.followupDate;
        this.bsaListVal = {
            id: row.id,
            name: row.bsaName
        };

    }
      editRow(row) {

     console.log(' BSAid:' + this.bsaListVal.id);
         console.log('BSA name:' + this.bsaListVal.name);
         console.log('date  :' + this.newfollowupDate);

        // with this you set all the rows as no editable, so you can change from row to row with the edit button
       //print selected bsa ID (auto complete ID)
     }
}

export interface IBsaList {
  id: string,
  name: string
}

For the pCalendar, remove [dataType]="date" then it will send the correct date when you hit save. 对于pCalendar,删除[dataType]="date"然后在您单击保存时它将发送正确的日期。 Think this is due to your model property followupDate is not a Date type. 认为这是由于您的模型属性followupDate不是Date类型所致。

For validation is pretty much how you would do any other validations, for simplicity sake (not 100% working, affects other rows, but you get the idea): 进行验证几乎就是您将执行任何其他验证的方式,为简单起见(并非100%正常工作会影响其他行,但您可以理解):

isValid(row) {
   if (row.followupDate && this.bsaListVal) {
     return false;
   }
   return true;
 }

<button (click)="editRow(row)" [disabled]="isValid(row)">Save</button>

Hope that helps. 希望能有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM