【问题标题】:Filter data based on multiple parameters in Angular根据Angular中的多个参数过滤数据
【发布时间】:2020-04-09 02:21:43
【问题描述】:

我在表单中有一个对象数组,其中可以有 2 个以上的对象。 这是我从服务器获取的数据。

[
        {processId : 1, processName : "process1", country : "germany", companyCode:"IB" , companyHiringType:"FRE", masterClauses:[
      {clauseName : "tst dummy", description : "dummy desc",  clauseId : 1, parentClause : null, titleDisplayFlag :false, textFlag: false, clauseType:"" , processid: 1, subClauseId:null, subClauseSequence:null ,texts :[
        {text : "dummy",textId : 1, sequenceNo :1}
      ]}
    ]},

        {processId : 2, processName : "process2", country : "Finland", companyCode:"IL" , companyHiringType:"Lat", masterClauses:[
      {clauseName : "test1", description : "test1Demo",  clauseId : 1, parentClause : null, titleDisplayFlag :false, textFlag: false, clauseType:"" , processid: 2, subClauseId:null, subClauseSequence:null ,texts :[
        {text : "dummy text",textId : 1, sequenceNo: 1}
      ]}
    ]}
  ]
  ]

我的 html

中有一个表单
<form #filterForm="ngForm" (ngSubmit)="addFilter(filterForm)" autocomplete="off">
      <h4 class="h4Header">Filter by
        <a href="javascript:void(0)"  (click)=closeRightMenu($event) class="filterClose icon x-24 close-icon float-right"></a>
      </h4>
      <!-- -->
      <mat-accordion class="cus-accordion">


        <mat-expansion-panel>
          <mat-expansion-panel-header>
            <mat-panel-title>Company</mat-panel-title>
          </mat-expansion-panel-header>
          <mat-radio-group required [ngModel]="selected" class="custom-radio block-radio-group"  aria-label="Select an option"  name="companyCode">
            <mat-radio-button class="block-radio-button"  *ngFor="let com of companyA;index as i"  [value]="com"  disableRipple="true">{{com}}</mat-radio-button>
          </mat-radio-group>
        </mat-expansion-panel>

        <mat-expansion-panel>
          <mat-expansion-panel-header>
            <mat-panel-title>Country</mat-panel-title>
          </mat-expansion-panel-header>
          <mat-radio-group required [ngModel]="selected" class="custom-radio block-radio-group"  aria-label="Select an option"  name="country">
            <mat-radio-button class="block-radio-button"  *ngFor="let com of country;index as i"  [value]="com.countryName"  disableRipple="true">{{com.countryName}}</mat-radio-button>
          </mat-radio-group>
        </mat-expansion-panel>

        <mat-expansion-panel>
          <mat-expansion-panel-header>
            <mat-panel-title>Process</mat-panel-title>
          </mat-expansion-panel-header>
          <mat-radio-group required [ngModel]="selected" class="custom-radio block-radio-group"  aria-label="Select an option"  name="companyHiringType">
            <mat-radio-button class="block-radio-button"  *ngFor="let com of hiring;index as i"  [value]="com"  disableRipple="true">{{com}}</mat-radio-button>
          </mat-radio-group>
        </mat-expansion-panel>

        <mat-expansion-panel>
          <mat-expansion-panel-header>
            <mat-panel-title>Module</mat-panel-title>
          </mat-expansion-panel-header>
          <mat-radio-group required [ngModel]="selected" class="custom-radio block-radio-group"  aria-label="Select an option"  name="processName">
            <mat-radio-button class="block-radio-button"  *ngFor="let com of moduleA;index as i"  [value]="com.processName"  disableRipple="true">{{com.processName}}</mat-radio-button>
          </mat-radio-group>
        </mat-expansion-panel>
    </mat-accordion>
    <div class="bottomUtil">
      <div class="row nomarLR padTB16">
        <div class="col-6 padR8">
          <button mat-flat-button class="primary-btn btnfullWdth" type="submit">Apply</button>
        </div>
        <div class="col-6 padL8">
          <button mat-flat-button class="secondary-btn btnfullWdth" (click)="testingButton()">Reset</button>
        </div>

      </div>
    </div>
    </form>

用户从单选按钮中选择值,其后显示的数据应基于选择的单选按钮。 例如,如果用户选择

processName : "process1", country : "germany", companyCode:"IB" and companyHiringType:"FRE"

我要显示的数据是

clauseName : "tst dummy", description : "dummy desc" and from the nested array I should get text : "dummy"

有没有一种方法可以根据 4 个参数过滤数据?

【问题讨论】:

  • 您好,如果您在代码中准备了一个堆栈闪电战,那将非常有帮助。
  • (stackblitz.com/edit/angular-tt9oxu) 这是我正在做的事情,当您选择单选按钮后保存时,控制台有一个对象,我想用它来过滤我的结果。如果您希望我更新代码,请告诉我。我正在使用单选按钮作为表单来推送 ser.services.ts 中 finalArray 中的数据,并且我想使用控制台中的对象进行过滤。
  • 我不确定我是否完全理解您想要过滤的内容。例如,在您的对象数组中,如果选择了该收音机,您只想显示countrygermany 的对象?另外,该对象数组与您的表单有何关系,对不起,我无法理解。
  • 我想显示数据,例如,国家是德国,流程名称是 process1,companyCode 是 IB,companyHiringType 是 FRE,所有这些。就像其中一个在线购物网站一样,您可以在其中选择多个过滤器并相应地显示产品
  • 您好!在您的 stackblitz 中,您从服务器获取的数据在哪里? (你把它存放在哪里?)

标签: javascript html arrays angular typescript


【解决方案1】:

根据理解您的代码/问题

这是来自服务器的数据

var data = [
  {
    processId: 1,
    processName: "process1",
    country: "germany",
    companyCode: "IB",
    companyHiringType: "FRE",
    masterClauses: [
      {
        clauseName: "tst dummy",
        description: "dummy desc",
        clauseId: 1,
        parentClause: null,
        titleDisplayFlag: false,
        textFlag: false,
        clauseType: "",
        processid: 1,
        subClauseId: null,
        subClauseSequence: null,
        texts: [
          {
            text: "dummy",
            textId: 1,
            sequenceNo: 1
          }
        ]
      }
    ]
  },
  {
    processId: 2,
    processName: "process2",
    country: "Finland",
    companyCode: "IL",
    companyHiringType: "Lat",
    masterClauses: [
      {
        clauseName: "test1",
        description: "test1Demo",
        clauseId: 1,
        parentClause: null,
        titleDisplayFlag: false,
        textFlag: false,
        clauseType: "",
        processid: 2,
        subClauseId: null,
        subClauseSequence: null,
        texts: [
          {
            text: "dummy text",
            textId: 1,
            sequenceNo: 1
          }
        ]
      }
    ]
  }
];

让我假设您的 filterObject 应该如下所示(它会被选中或不被选中)

var filterObj = {
  country: "germany",
  companyCode: null,
  companyHiringType: null,
  processName: "process1"
};

您选择或不选择单选按钮的位置(如果未选择,则该属性的值将为空)

这里是过滤数据的逻辑

var f = data.filter(a => {
  return (filterObj.country == null || filterObj.country == a.country)
    && (filterObj.companyCode == null || filterObj.companyCode == a.companyCode)
    && (filterObj.companyHiringType == null || filterObj.companyHiringType == a.companyHiringType)
    && (filterObj.processName == null || filterObj.processName == a.processName);
});

这是你的结果

console.log(f);

希望这会对你有所帮助。

如果您需要更多信息或对此有什么不清楚或有什么问题,请告诉我。

注意:我只给出了基于 4 个参数(如果参数/属性有值)过滤数据的代码

谢谢

【讨论】:

  • 这里的“数据”,是我必须在某个地方定义的数组还是别的东西
  • 哦,对不起,我忘了声明数据变量,在你的情况下,它将来自服务器,但我已经编辑答案查看,如果需要更多信息,请告诉我
  • 嗨,kushal,我已经更新了代码。请您帮我解决一下 [this] (stackoverflow.com/questions/59379976/…) 在这种情况下我们可以使用您的过滤方法吗?
  • 嗨 Menime 请检查stackoverflow.com/questions/59379976/… 并尝试使用该解决方案,使用后告诉我。谢谢
猜你喜欢
  • 1970-01-01
  • 2019-06-26
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-18
  • 1970-01-01
  • 2018-08-24
相关资源
最近更新 更多