为角度为2

时间:2018-03-02 12:47:25

标签: angular angular-material

我在角度2中创建了一个简单的mat-table,其中包含两列 acc_id acc_desc

我使用 accountdetails.service.ts 从位于assets文件夹中的 accountdetails.json 文件中访问这些列的值。

我在帐户说明列中列出了 acc_desc 的值。

下面显示的是我的输出

enter image description here

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

下面显示的是我的代码

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 { }

有人可以告诉我如何使用我的json文件中列出的acc_desc初始化相应acc_id的值,同时使用下拉列表选择所需的值。

提前致谢...

1 个答案:

答案 0 :(得分:3)

您必须将变量绑定到mat-select组件并修复value的{​​{1}}。

mat-option

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

相关问题