json数据
[{"masterbatch":"29",
"batch": [{"subgroupname":"Batch 1"},{"subgroupname":"Batch 10"},{"subgroupname":"Batch 11"},{"subgroupname":"Batch 12"},{"subgroupname":"Batch 13"},{"subgroupname":"Batch 14"},{"subgroupname":"Batch 15"},{"subgroupname":"Batch 16"},{"subgroupname":"Batch 17"},{"subgroupname":"Batch 2"},{"subgroupname":"Batch 3"},{"subgroupname":"Batch 4"},{"subgroupname":"Batch 5"},{"subgroupname":"Batch 6"},{"subgroupname":"Batch 7"},{"subgroupname":"Batch 8"},{"subgroupname":"Batch 9"}],
"group":[{"groupname":"Group I"},{"groupname":"Group II"},{"groupname":"Group III"},{"groupname":"Group IV"},{"groupname":"Group IX"},{"groupname":"Group V"},{"groupname":"Group VI"},{"groupname":"Group VII"},{"groupname":"Group VIII"}],
"section":[{"Sections":"Section I"},{"Sections":"Section II"},{"Sections":"Section III"},{"Sections":"Section IV"}]
}];
html
Select Type :
<select [(ngModel)]="sel_type" style="width:100px;">
<option value='Select Type'>Select Type</option>
<option>section</option>
<option>group</option>
<option>batch</option>
</select>
<select [(ngModel)]="sel_subtype" style="width:100px;"></select>
如果我选择sel_type
等于batch
,则将数据传递到下一个选择框sel_subtype
,只批量来自json object
group
和section
如何使用angular2
?
答案 0 :(得分:1)
You can easily add a pipe for such use case and use it may be some like this can be useful for your case . You can try it with with selct dropdown as well it works with ul and li .
import {Injectable, Pipe} from 'angular2/core';
@Pipe({
name: 'myfilter'
})
@Injectable()
export class MyFilterPipe implements PipeTransform {
transform(items: any[], args: any[]): any {
return items.filter(item => item.id.indexOf(args[0]) !== -1);
}
}
And use it:
import { MyFilterPipe } from './filter-pipe';
(...)
@Component({
selector: 'my-component',
pipes: [ MyFilterPipe ],
template: ` <ul>
<li *ngFor="#element of (elements | myfilter:'123')">(...)</li>
</ul> `
})
另一种方法可能是在选择列表上添加事件并过滤typescript组件中其他组件的数据,在typescript过滤器函数中使用它。
答案 1 :(得分:1)
您可以在组件中创建一个可以设置子类型的函数:
//组件
public batches : any = [
{
"masterbatch":"29",
"batch": [
{"subgroupname":"Batch 1"},{"subgroupname":"Batch 10"},{"subgroupname":"Batch 11"},{"subgroupname":"Batch 12"},{"subgroupname":"Batch 13"},{"subgroupname":"Batch 14"},{"subgroupname":"Batch 15"},{"subgroupname":"Batch 16"},{"subgroupname":"Batch 17"},{"subgroupname":"Batch 2"},{"subgroupname":"Batch 3"},{"subgroupname":"Batch 4"},{"subgroupname":"Batch 5"},{"subgroupname":"Batch 6"},{"subgroupname":"Batch 7"},{"subgroupname":"Batch 8"},{"subgroupname":"Batch 9"}
],
"group":[
{"groupname":"Group I"},{"groupname":"Group II"},{"groupname":"Group III"},{"groupname":"Group IV"},{"groupname":"Group IX"},{"groupname":"Group V"},{"groupname":"Group VI"},{"groupname":"Group VII"},{"groupname":"Group VIII"}
],
"section":[
{"Sections":"Section I"},{"Sections":"Section II"},{"Sections":"Section III"},{"Sections":"Section IV"}
]
}
];
public subTypes: any[] = [];
public function onChange(type:string){
this.subTypes = this.batches[0][type].length ? this.batches[0][type] : [];
}
// HTML
Select Type :
<select [(ngModel)]="sel_type" style="width:100px;" (change)="onChange(sel_type);">
<option value='Select Type'>Select Type</option>
<option value="section">section</option>
<option value="group">group</option>
<option value="batch">batch</option>
</select>
<select [(ngModel)]="sel_subtype" style="width:100px;">
<option value='Select Type'>Select Sub Type</option>
<option [value]="type.groupname" *ngFor="let type of subTypes">{{type.groupname}}</option>
</select>