为在 angular 2 中的 mat-table 中实现的 mat-select 分配所需的值 [英] Assigning the required value for mat-select implemented in mat-table in angular 2

查看:27
本文介绍了为在 angular 2 中的 mat-table 中实现的 mat-select 分配所需的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在角度 2 中创建了一个简单的 mat-table,有两列 acc_idacc_desc.

我正在使用 accountdetails.service.ts 从放置在 assets 文件夹中的 accountdetails.json 文件访问这些列的值.

我在帐户描述列中做了一个下拉列表,列出了 acc_desc 的值.

下面是我的输出

但在我的表中,我想根据 json 文件中的 acc_id 初始化 acc_desc 值,并在下拉列表中列出,以便在需要时我可以将其更改为其他选项.

下面是我的代码

account.component.html

欢迎</mat-toolbar><br/><!-- 表格从这里开始--><垫卡><div class="example-container mat-elevation-z8"><mat-table #table [dataSource]="dataSource1"><!-- 账号栏--><ng-container matColumnDef="acc_id"><mat-h​​eader-cell *matHeaderCellDef>帐户ID.</mat-h​​eader-cell><mat-cell *matCellDef="let element">{{element.acc_id}}</mat-cell></ng-容器><!-- 账户描述栏--><ng-container matColumnDef="acc_desc"><mat-h​​eader-cell *matHeaderCellDef>账户描述 </mat-h​​eader-cell><mat-cell *matCellDef="let element"><mat-form-field ><mat-select style="min-width: 200px;"占位符="" ><mat-option *ngFor="let dep of acc_desc" [value]="acc_desc" >{{ dep.acc_desc }}</mat-option></mat-select></mat-form-field></mat-cell></ng-容器><mat-h​​eader-row *matHeaderRowDef="displayedColumns1" ></mat-h​​eader-row><mat-row *matRowDef="让行;列:displayColumns1;"></mat-row></mat-table><mat-paginator #paginator[页面大小]="10"[pageSizeOptions]="[5, 10, 20]"></mat-paginator>

</mat-card>

account.component.ts

import {Component, ViewChild, Inject, OnInit} from '@angular/core';从@angular/forms"导入 { FormControl, FormGroup, ReactiveFormsModule };从 'rxjs/Observable' 导入 { Observable };导入 'rxjs/add/operator/map';从@angular/material"导入 {MatPaginator, MatTableDataSource};从 '../accountdetail.service' 导入 { AccountdetailService };@成分({选择器:'应用程序帐户',templateUrl: './account.component.html',styleUrls: ['./account.component.scss']})导出类 AccountComponent 实现 OnInit {acc_desc:任何;构造函数(私人accdetailservice:AccountdetailService){}/* 表格从这里开始--------------- */displayColumns1 = ['acc_id', 'acc_desc'];dataSource1= new MatTableDataSource(ELEMENT_DATA);ngOnInit(){this.accdetailservice.accountdetails().订阅(数据=> {this.acc_desc = 数据;this.dataSource1.data = 数据;});}@ViewChild(MatPaginator) 分页器:MatPaginator;ngAfterViewInit() {this.dataSource1.paginator = this.paginator;} }导出接口元素{acc_id:任何;acc_desc:任何;}const ELEMENT_DATA: 元素[] = [];

accountdetails.service.ts

import { Injectable } from '@angular/core';从 '@angular/http' 导入 { Http };导入 'rxjs/add/operator/map';@Injectable()导出类 AccountdetailService {构造函数(私有 http:Http){}帐户详细资料(){return this.http.get('/assets/accountdetails.json').map(result => result.json());}}

accountdetails.json

[{acc_id":1001,"acc_desc": "管理"},{acc_id":1002,"acc_desc": "实验室"},{acc_id":1003,"acc_desc": "工作人员"},{acc_id":1004,"acc_desc": "Office-1"},{acc_id":1005,"acc_desc": "Office-2"}]

app.module.ts

import { NgModule } from '@angular/core';从'@angular/platform-b​​rowser' 导入 { BrowserModule };从@angular/platform-b​​rowser/animations"导入 { BrowserAnimationsModule };从'@angular/forms' 导入 { ReactiveFormsModule, FormsModule};从'@angular/http'导入{HttpModule};import { AppMaterialModule } from './app-material.module';import { AppRoutingModule } from './app-routing.module';从 './app.component' 导入 { AppComponent };从 './account/account.component' 导入 { AccountComponent };从 './accountdetail.service' 导入 { AccountdetailService };@NgModule({声明: [应用组件,帐户组件],进口:[浏览器模块,应用路由模块,反应形式模块,浏览器动画模块,应用材料模块,表单模块,Http模块],入口组件:[ ],提供者:[AccountdetailService],引导程序:[AppComponent]})导出类 AppModule { }

谁能告诉我如何使用我的 json 文件中列出的 acc_desc 初始化相应 acc_id 的值,也可以使用下拉列表来选择所需的值.

提前致谢...

解决方案

您必须将变量绑定到 mat-select 组件并修复 value>mat-option.

示例:https://stackblitz.com/edit/angular-isif36?file=app%2Fselect-value-binding-example.ts

I have created a simple mat-table in angular 2 with two columns acc_id and acc_desc.

I am accessing the value for these columns from my accountdetails.json file placed in assets folder using the accountdetails.service.ts.

I have taken a drop-down in the Account Description column listing the values for the acc_desc .

Below shown is my output

But here in my table i want to initialise the acc_desc value as per its acc_id from the json file with the listing in dropdown ,so that if required i can change it to other option.

Below shown is my code

account.component.html

<mat-toolbar color="primary" style="width:100%"> WELCOME </mat-toolbar><br/>

<!-- Table starts here -->

<mat-card>
<div class="example-container mat-elevation-z8">

  <mat-table #table [dataSource]="dataSource1">

    <!-- Account No. Column -->
    <ng-container matColumnDef="acc_id">
      <mat-header-cell *matHeaderCellDef> Account ID. </mat-header-cell>
      <mat-cell *matCellDef="let element">{{element.acc_id}}</mat-cell>
    </ng-container>

      <!-- Account Description Column -->
    <ng-container matColumnDef="acc_desc">
      <mat-header-cell *matHeaderCellDef> Account Description </mat-header-cell>
      <mat-cell *matCellDef="let element">
          <mat-form-field >
            <mat-select style="min-width: 200px;" placeholder="" >
              <mat-option *ngFor="let dep of acc_desc " [value]="acc_desc" >
                {{ dep.acc_desc }}
              </mat-option>
            </mat-select>
          </mat-form-field>
      </mat-cell>
       </ng-container>


    <mat-header-row *matHeaderRowDef="displayedColumns1" ></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns1;"> </mat-row>
  </mat-table>

  <mat-paginator #paginator
                 [pageSize]="10"
                 [pageSizeOptions]="[5, 10, 20]">
  </mat-paginator>
</div>
</mat-card>

account.component.ts

import {Component, ViewChild, Inject, OnInit} from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import {MatPaginator, MatTableDataSource} from '@angular/material';
import { AccountdetailService } from '../accountdetail.service';

@Component({
  selector: 'app-account',
  templateUrl: './account.component.html',
  styleUrls: ['./account.component.scss']
   })


export class AccountComponent implements OnInit {

acc_desc: any;

constructor(private accdetailservice: AccountdetailService ) { }


  /* Table Starts here
  ---------------------- */

 displayedColumns1 = ['acc_id', 'acc_desc'];
 dataSource1= new MatTableDataSource<Element>(ELEMENT_DATA);

ngOnInit(){
  this.accdetailservice.accountdetails()
  .subscribe(data => {
     this.acc_desc = data;
     this.dataSource1.data = data;
  }); }

  @ViewChild(MatPaginator) paginator: MatPaginator;

   ngAfterViewInit() {
    this.dataSource1.paginator = this.paginator;
  } }

  export interface Element {
   acc_id: any;
   acc_desc: any; 
  }

const ELEMENT_DATA: Element[] = [];

accountdetails.service.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';


@Injectable()
export class AccountdetailService {

  constructor(private http:Http ) { }

  accountdetails()
  {
    return this.http.get('/assets/accountdetails.json')
    .map(result => result.json());
  }}

accountdetails.json

[
    {
        "acc_id": 1001,
        "acc_desc": "Administration"
    },

    {
        "acc_id": 1002,
        "acc_desc": "Laboratory"
    },

    {
        "acc_id": 1003,
        "acc_desc": "Staff"
    },

    {
        "acc_id": 1004,
        "acc_desc": "Office-1"
    },

    {
        "acc_id": 1005,
        "acc_desc": "Office-2"
    }
]

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ReactiveFormsModule, FormsModule} from '@angular/forms';
import { HttpModule} from '@angular/http';

import { AppMaterialModule } from './app-material.module';


import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AccountComponent } from './account/account.component';

import { AccountdetailService } from './accountdetail.service';


@NgModule({
  declarations: [
    AppComponent,
    AccountComponent      
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    ReactiveFormsModule,
    BrowserAnimationsModule,
    AppMaterialModule,
    FormsModule ,
    HttpModule   
  ],
  entryComponents:[ ],

  providers: [ AccountdetailService],
  bootstrap: [AppComponent]
})
export class AppModule { }

Can anybody please tell me how can i initialise the values for the corresponding acc_id with its acc_desc as listed in my json file ,also with the dropdown to select the required.

Thanks in Advance...

解决方案

You have to bind a variable to the mat-select component and fix the value of mat-option.

<mat-select style="min-width: 200px;" placeholder="" [(value)]="element.acc_desc" >
   <mat-option *ngFor="let dep of acc_desc " [value]="dep.acc_desc" >
     {{ dep.acc_desc }}
   </mat-option>
</mat-select>

Sample : https://stackblitz.com/edit/angular-isif36?file=app%2Fselect-value-binding-example.ts

这篇关于为在 angular 2 中的 mat-table 中实现的 mat-select 分配所需的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆