0

我正在尝试使用 ng2-smart-table 插件显示产品数据。

我能够在浏览器日志中获取产品,但我需要显示相同的 onChange 下拉值。

\app\components\layout\blank-page\blank-page.component.html

<form role="form">
        <fieldset class="form-group">
            <label>Select Category</label><br/><br/>
                <select [(ngModel)]="selectedObject" (ngModelChange)="onChange(selectedObject)" name="selectedObject" class="form-control">
                        <option>--Select Category--</option>
                        <option *ngFor="let category of categories" [value]="category._id">{{category.category_name}}</option>
                </select>
        </fieldset>
    </form>


//This is where table data is displaying, need to show my data here

<ng2-smart-table [settings]="view_update_items_settings" [source]="view_update_items_smart_data" (userRowSelect)="onUserRowSelect($event)"  class="table table-hover table-striped"></ng2-smart-table>

应用程序\组件\布局\空白页\空白页.component.ts

onChange(categoryid) {

this.productService.getProductsOncategory(categoryid).subscribe(data => {
  if (data.success) {
    //this.selectedObject = data.mainProducts;
    console.log(data)
    console.log('Products obtained');
  } else {
    console.log('Not obtained!');
  }
});

}

应用\服务\product.service.ts

getProductsOncategory(category_id){
  console.log(category_id)
  let catUrl="http://10.*.*.*:5000/products/getProductsOncategory"
  let headers = new Headers();
  headers.append('Content-Type','application/json');
  let catIdObj = JSON.stringify({category_id:category_id})
  console.log(catIdObj)
  return this.http.post(catUrl,catIdObj,{headers:headers})
  .map((response:Response)=>response.json())
  .do(data=>console.log(JSON.stringify(data)))
  .catch(this.handleError);
}

从下拉列表中选择一个值时,我会在浏览器中发布数据,我需要在我的 ng2-smart-table 中显示相同的数据

在此处输入图像描述

4

1 回答 1

0

将“view_update_items_smart_data”与您从服务器获取的数据绑定。

    onChange(categoryid) {

this.productService.getProductsOncategory(categoryid).subscribe(data => {
  if (data.success) {
    //this.selectedObject = data.mainProducts;
    console.log(data)
    console.log('Products obtained');
    this.view_update_items_smart_data.load(data);  
  } else {
    console.log('Not obtained!');
  }
});

}

有关更多信息,您可以参考 https://akveo.github.io/ng2-smart-table/#/examples/populate-from-server

于 2017-11-13T15:05:40.710 回答