简体   繁体   English

mat-autocomplete selected mat-option 从 selectedItem 传递多个值

[英]mat-autocomplete selected mat-option pass multiple values from selectedItem

I have a mat-autocomplete that loads table data that is in JSON format and currently maps a single JSON value as the selected "value" when the user selects an item from the autocomplete.我有一个 mat-autocomplete,它加载 JSON 格式的表数据,当用户从自动完成中选择一个项目时,当前将单个 JSON 值映射为选定的“值”。

In addition to this value, I also need to pass some other values from this selected item's JSON node, as these values are being written to a database.除了这个值之外,我还需要从这个选定项目的 JSON 节点传递一些其他值,因为这些值正在写入数据库。 So I need 3 values from this selected object, not just one.所以我需要来自这个选定对象的 3 个值,而不仅仅是一个。

But I'm not sure how to access those additional JSON properties from the existing interface or if I need to modify the interface to allow those values to be passed as well.但我不确定如何从现有接口访问这些额外的 JSON 属性,或者我是否需要修改接口以允许传递这些值。

For example, the table data that populates the autocomplete is below and we are currently using the "codeFileType" as the "value" passed when a user selects an item from the autocomplete table:例如,填充自动完成的表数据如下,我们当前使用“codeFileType”作为用户从自动完成表中选择项目时传递的“值”:

"table-lookups": [
{
  "codeFileType": "NEW_CODE_FILE_TYPE_A",
  "datasource": "A",
  "myAutoCompleteHook": "autocompleLookupTest1",
  "myAutoCompleteParams": [
    {
      "name": "_endpoint",
      "value": "MyCodeFile"
    },
    {
      "name": "_labelKey",
      "value": "code"
    },
    {
      "name": "_valueKey",
      "value": "description"
    }
  ]
},
{
  "codeFileType": "NEW_CODE_FILE_TYPE_B",
  "datasource": "B",
  "myAutoCompleteHook": "autocompleLookupTest2",
  "myAutoCompleteParams": [
    {
      "name": "_endpoint",
      "value": "MyCodeFile"
    },
    {
      "name": "_labelKey",
      "value": "code"
    },
    {
      "name": "_valueKey",
      "value": "description"
    }
  ]
},
{
  "codeFileType": "NEW_CODE_FILE_TYPE_C",
  "datasource": "C",
  "myAutoCompleteHook": "autocompleLookupTest3",
  "myAutoCompleteParams": [
    {
      "name": "_endpoint",
      "value": "MyCodeFile"
    },
    {
      "name": "_labelKey",
      "value": "code"
    },
    {
      "name": "_valueKey",
      "value": "description"
    }
  ]
}

], ],

So, from above JSON, we are mapping the codeFileType as the "value" of the select.因此,从上面的 JSON 中,我们将codeFileType映射为选择的“值”。 I also need to access the selected item's "myAutoCompleteHook" and "myAutoCompleteParams" values as well and pass those along with codeFileType as separate data points.我还需要访问所选项目的“myAutoCompleteHook”和“myAutoCompleteParams”值,并将它们与 codeFileType 作为单独的数据点一起传递。

Here is the HTML format of the mat-option element where we are binding the [value] to the valueKey:这是我们将 [value] 绑定到 valueKey 的 mat-option 元素的 HTML 格式:

<mat-option
 *ngFor="let values of filteredResults$ | async; trackBy: trackByFn; index as i"
 [value]="values[tableConfig?.valueKey]"
 [innerHtml]="buildLabelFromValuesChecked(values, isOptionSelected(values[tableConfig?.valueKey]))">
</mat-option>

You can just pass the index of the selected item, and access the full object from the array using the index number.您可以只传递所选项目的index ,并使用index号访问数组中的完整对象。 Easiest thing ever.有史以来最简单的事情。

<mat-option
 *ngFor="let values of filteredResults$ | async; trackBy: trackByFn; index as i"
 [value]="i"
 [innerHtml]="buildLabelFromValuesChecked(values, isOptionSelected(values[tableConfig?.valueKey]))">
</mat-option>

component.ts:组件.ts:

// function that is assigned to mat-selected
someFunction(index: number) {
  this.selectedItem = this.table-lookups[index]
}

I also see you are running the *ngFor on filteredResults$ .我还看到您正在 filterResults filteredResults$上运行*ngFor If filteredResults$ is not same as table-lookups , then you can bind value to codeFileType and use findIndex or filter .如果 filtersResults filteredResults$table-lookups不同,那么您可以将value绑定到codeFileType并使用findIndexfilter

[value]="codeFileType"

component.ts:组件.ts:

// function that is assigned to mat-selected
someFunction(value: string) {

// filter
  this.selectedItem = this.table-lookups.filter((item) => item.codeFileType === value)[0] // since filter returns an array
}

// or findIndex
  const itemIndex = this.table-lookups.findIndex((item) => item.codeFileType === value) // since filter returns an array
  this.selectedItem = this.table-lookups[itemIndex];
}

Then you can use this.selectedItem.myAutoCompleteHook and this.selectedItem.myAutoCompleteParams to access those props.然后你可以使用this.selectedItem.myAutoCompleteHookthis.selectedItem.myAutoCompleteParams来访问这些道具。

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

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