简体   繁体   English

如果我们有更多记录而没有使用 angular8 中断,如何更改下拉值

[英]How to change the dropdown value, if we have more records without having break using angular8

I have used Angular8 in my project, here if i have 2-3 records then Previous and Next works fine, but if i have more records, then when i click on previous/next, then it break the code and struck in the middle.我在我的项目中使用了 Angular8,如果我有 2-3 条记录,那么上一个和下一个工作正常,但是如果我有更多记录,那么当我单击上一个/下一个时,它会破坏代码并击中中间。 Can anyone help, how to solve this issue.任何人都可以帮忙,如何解决这个问题。 I have attached working demo of my code.我附上了我的代码的工作演示。

TS: TS:

  public changeDropdown(value) {
    const entry = this.dropdownValue.findIndex(x => x.noteId === parseInt(value));
    this.selectedDropdownValue = this.dropdownValue[entry].noteId;
  }

  public previousNextValue(value) {
      let previousValue = this.selectedDropdownValue;
      previousValue = value ? ++previousValue : --previousValue;
      this.changeDropdown(previousValue);
  } 

HTML: HTML:

 <div class="card-footer text-right">
                    <button class="btn btn-outline-primary" type="button" (click)="previousNextValue(false)"
                    [disabled]="selectedDropdownValue<=1"><i class="fas fa-chevron-left"></i>
                        Previous</button> 
                    <button class="btn btn-outline-primary ml-1" type="button" (click)="previousNextValue(true)"
                    [disabled]="selectedDropdownValue==dropdownValue.length">Next <i class="fas fa-chevron-right"></i></button>
                </div>

DEMO 演示

The problem is that you are looking for an ID that doesnt exist, and also you are using the same Variable for value and for The index, wich is causing troubles, you have to create another variable for setting the previousValue.问题是您正在寻找一个不存在的 ID,并且您使用相同的变量作为值和索引,这会造成麻烦,您必须创建另一个变量来设置 previousValue。 Here's an example.这是一个例子。

selectedDropdownValue:number = 0;
  globalPreviousValue: number = 0;
  public dropdownValue = [
    {"noteId":1,"noteTypeId":1,"noteTypeName":null,"noteSubject":"subject -1","createdDate":"2020-08-07T15:21:48.71","createdBy":"Admin","noteText":null,"dropdownSubject":"08/07/2020 Admin subject -1"},{"noteId":2,"noteTypeId":2,"noteTypeName":null,"noteSubject":"subject -2 test","createdDate":"2020-08-07T15:25:38.553","createdBy":"Admin","noteText":null,"dropdownSubject":"08/07/2020 Admin subject -2 test"},{"noteId":3,"noteTypeId":2,"noteTypeName":null,"noteSubject":"subject -3 test","createdDate":"2020-08-11T11:26:36.007","createdBy":"Admin","noteText":null,"dropdownSubject":"08/11/2020 Admin subject -3 test"},{"noteId":10,"noteTypeId":3,"noteTypeName":null,"noteSubject":"test","createdDate":"2020-08-12T11:50:14.653","createdBy":"Admin","noteText":null,"dropdownSubject":"08/12/2020 Admin test"},{"noteId":11,"noteTypeId":5,"noteTypeName":null,"noteSubject":"tested","createdDate":"2020-08-12T14:36:43.41","createdBy":"Admin","noteText":null,"dropdownSubject":"08/12/2020 Admin tested"},
    {"noteId":12,"noteTypeId":135,"noteTypeName":"Email sent to Agent","noteSubject":"test","createdDate":"2020-08-12T17:25:15.71","createdBy":"Admin","noteText":null,"dropdownSubject":"08/12/2020 Admin test"},
    {"noteId":13,"noteTypeId":136,"noteTypeName":"Fax to Agent","noteSubject":"tested","createdDate":"2020-08-12T17:29:59.97","createdBy":"Admin","noteText":null,"dropdownSubject":"08/12/2020 Admin tested"}]
               
  public changeDropdown(value) {
    this.globalPreviousValue = value
    this.selectedDropdownValue = this.dropdownValue[value].noteId;
  }

  public previousNextValue(value) {
      let previousValue = this.globalPreviousValue;
      previousValue = value ? ++previousValue : --previousValue;
      this.changeDropdown(previousValue);
  } 

Also Change the Validation in your HTML in order work as desired.还要更改 HTML 中的验证,以便按需要工作。

<div class="card-footer text-right">
                    <button class="btn btn-outline-primary" type="button" (click)="previousNextValue(false)"
                    [disabled]="globalPreviousValue<=1"><i class="fas fa-chevron-left"></i>
                        Previous</button> 
                    <button class="btn btn-outline-primary ml-1" type="button" (click)="previousNextValue(true)"
                    [disabled]="globalPreviousValue==dropdownValue.length-1">Next <i class="fas fa-chevron-right"></i></button>
                </div>

Working Demo 工作演示

You need to refactor your logic from value to index of array like您需要将逻辑从值重构为数组的索引,例如

export class AppComponent {
  selectedDropdownValue:number = 0;
  selectedIndex: number = -1;
  public dropdownValue = []; // data
               
  public previousNextValue(selectedIndex) {
    this.selectedIndex = selectedIndex;
    this.selectedDropdownValue = this.dropdownValue[selectedIndex].noteId;
  }
  public changeDropdown(selectedDropdownValue){
    this.selectedDropdownValue = selectedDropdownValue;
    this.selectedIndex = this.dropdownValue.findIndex(x => x.noteId === parseInt(selectedDropdownValue));

  }
}

And your view for next and previous will be like你对下一个和上一个的看法就像

<select class="custom-select w-100" name="noteTypeId" (change)="changeDropdown($event.target.value)" [value]="selectedDropdownValue">
    <option value="0" selected disabled>Select Note Details</option>
    <option *ngFor="let notesItem of dropdownValue" [value]="notesItem.noteId">
        {{notesItem.dropdownSubject}}
    </option>
</select>

<div class="card-footer text-right">
    <button class="btn btn-outline-primary" type="button" (click)="previousNextValue(selectedIndex - 1)"
    [disabled]="selectedIndex < 1"><i class="fas fa-chevron-left"></i>
        Previous</button> 
    <button class="btn btn-outline-primary ml-1" type="button" (click)="previousNextValue(selectedIndex + 1)"
    [disabled]="selectedIndex >= dropdownValue.length - 1">Next <i class="fas fa-chevron-right"></i></button>
</div>

DEMO演示

暂无
暂无

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

相关问题 如何根据单击angular8中的上一个/下一个按钮来更改下拉值 - How to change the dropdown value based on click of Previous/Next button in angular8 如何使用 Angular8 检测输入的变化 - How to detect change in input using Angular8 如何使用 Angular8 将第一行值复制到 rest 值 - How to copy the first row value to rest values using Angular8 如果其他控件没有值,如何使复选框保持禁用状态,如果控件在 angular8 中有值则启用 - How to make checkbox remain disabled if other control doesnt have value and enable if control has value in angular8 如何使用angular8切换行 - how to toggle the row using angular8 我们有一个2下拉列表,在选择第一个下拉列表的多个值时具有相同的值,第二个下拉值是自动选择的 - We have a 2 dropdown list having same value on selection of multiple value of first dropdown second dropdown value is auto selected 我们如何在 Angular8 的 2 个按钮之间切换 class - how can we toggle class between 2 buttons in Angular8 如何使用angular8根据上一个和下一个按钮单击使表单中的数据发生变化 - How to make the data in form to change based on previous and next button click using angular8 如何使用 angular8 和 bootstrap4 检测保存和标签更改事件 - How to detect save and tab change event using angular8 with bootstrap4 我们如何清除 angular 中的 angular 下拉列表值? - How do we clear an angular dropdown list value in angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM