[英]Is there a way to switch between angular material tabs based on a form submission from contents of those tabs?
我正在開發一個有角度的Web應用程序。 我需要根據在這些標簽的內容中執行的操作來控制我的角度標簽。 例如,我有以下選項卡:項目信息,變化,定價,庫存等,這些選項卡包含由角度組件加載的表格。 當項目信息表單成功提交后,我想自動切換到“變體”選項卡。 另外,用戶在保存項目信息之前應該不能切換到版本選項卡。
<mat-tab-group class=" navbar-default panel-piluku nav nav-justified nav-wizard nav-progression" >
<mat-tab class="outbound_link" role="button" title="Item Info" label="Item Info">
<add-product-information></add-product-information>
</mat-tab>
<mat-tab class="outbound_link" role="button" title="Variations" label="Variations">
<add-product-variation></add-product-variation>
</mat-tab>
<mat-tab class="outbound_link" role="button" title="Pricing" label="Pricing">
<add-product-pricing></add-product-pricing>
</mat-tab>
<mat-tab class="outbound_link" role="button" title="Inventory" label="Inventory">
<add-product-stock></add-product-stock>
</mat-tab>
<mat-tab class="outbound_link" role="button" title="Images" label="Images">
<add-product-images></add-product-images>
</mat-tab>
<mat-tab class="outbound_link" role="button" title="Locations" label="Locations">
<add-product-locations></add-product-locations>
</mat-tab>
</mat-tab-group>
constructor(private route: ActivatedRoute, private productsService: ProductsService, private router: Router, public dialog: MatDialog, private suppliersService: SuppliersService) { }
submitProduct = function(){
this.product["sku"] = this.sku;
this.product["alternate_sku"] = this.alternate_sku;
this.product["product_name"] = this.product_name;
this.product["product_description"] = this.product_description;
this.product["category"] = this.category_id;
this.product["price_per_unit"] = this.price_per_unit;
this.product["reorder_level"] = this.reorder_level;
this.product["discontinued"] = this.discontinued;
this.product["cost"] = this.cost;
this.product["default_price"] = this.default_price;
this.product["best_price"] = this.best_price;
this.product["medium_price"] = this.medium_price;
this.product["brand"] = this.brand;
this.product["upc"] = this.upc;
this.productsService.addProduct(this.product).subscribe((data)=>{
````Here the tab should switch```````
});
<form action="submitProduct()" id="item_form" class="form-horizontal" method="post" accept-charset="utf-8">
<input type="hidden" name="ecommerce_product_id" value="" />
...
...
...
<div class="form-actions">
<input type="submit" name="submitf" value="Save" id="submitf"
(click)="submitProduct()" class="submit_button floating-button btn btn-lg btn-primary" />
</div>
</div>
</div>
</form>
可以使用輸入到mat-tab-group
的selectIndex
來控制選擇mat-tab-group
。 要更改tabSelected,將其設置為所需的標簽索引,然后將標簽更改為設置的索引。
為此,我只需定義一個服務並使用行為主體即可。 我的服務定義:
my-tab.service.ts
:
@Injectable()
export class MyTabService{
tabSubject : BehaviorSubject<number> = new BehaviorSubject<number>(0);
constructor(){}
setTabSelected(tabIndex : number){
this.tabSubject.next(tabIndex);
}
getTabSelected() {
this.tabSubject.asObservable();
}
}
現在,在tabs組件中訂閱getTabSelected()
,然后在訂閱響應中獲得tabIndex
。
考慮您的標簽組件是my-tab.component.ts
,
@Component({...})
export class MyTabComponent {
selectdIndex : number = 0;
constructor(private _myTabService : MyTabService){}
ngOnInit(){
this.-myTabService.getTabSelected().subscribe((tabIndex : number) =>{
this.selectedIndex = tabIndex;
})
...
}
...
}
在模板中使用selectedIndex:
<mat-tab-group class=" navbar-default panel-piluku nav nav-justified nav-wizard nav-progression" [selectedIndex]='selectedIndex'>
<mat-tab class="outbound_link" role="button" title="Item Info" label="Item Info">
<add-product-information></add-product-information>
</mat-tab>
<mat-tab class="outbound_link" role="button" title="Variations" label="Variations">
<add-product-variation></add-product-variation>
</mat-tab>
<mat-tab class="outbound_link" role="button" title="Pricing" label="Pricing">
<add-product-pricing></add-product-pricing>
</mat-tab>
<mat-tab class="outbound_link" role="button" title="Inventory" label="Inventory">
<add-product-stock></add-product-stock>
</mat-tab>
<mat-tab class="outbound_link" role="button" title="Images" label="Images">
<add-product-images></add-product-images>
</mat-tab>
<mat-tab class="outbound_link" role="button" title="Locations" label="Locations">
<add-product-locations></add-product-locations>
</mat-tab>
</mat-tab-group>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.