分配角度2中在垫表中实施的垫选择所需的值 [英] Assigning the required value for mat-select implemented in mat-table in angular 2

查看:69
本文介绍了分配角度2中在垫表中实施的垫选择所需的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用两个列 acc_id acc_desc 创建了一个角度为2的简单席子表.

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

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

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

我已在帐户说明列中下拉列表,列出了 acc_desc 的值.

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

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

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.

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的值,以及如何选择所需的下拉列表.

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.

预先感谢...

推荐答案

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

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>

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

这篇关于分配角度2中在垫表中实施的垫选择所需的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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