2

我正在尝试使用 Angular2 ng Smart table 插件将数据从我的 API 加载到自定义组件。

根据他们的文档(https://github.com/akveo/ng2-smart-table/blob/master/src/app/pages/examples/server/basic-example-load.component.ts

我有我的组件,例如:

import { LocalDataSource } from 'ng2-smart-table';
import { ProductService } from '../../../services/product.service';

export class CategoryItemsComponent implements OnInit {

...
 source: LocalDataSource;

 constructor(private productService: ProductService,
             private flashMessage: FlashMessagesService,
             private router: Router,
             http: Http) {
       this.source = new LocalDataSource();

      this.productService.getProductsOncategory(this.categoryid).subscribe((data) => {
      this.source.load(data);
   });

}

产品服务.ts

getProductsOncategory(category_id) {

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

服务功能中使用的上述 API 在我的邮递员中完美运行。

现在我需要将该 API 中的 dame 数据加载到我的自定义组件中。

我收到此错误:

错误类型错误:this.data.slice 不是函数

在 LocalDataSource.webpackJsonp.../../../../ng2-smart-table/lib/data-source/local/local.data-source.js.LocalDataSource.getElements ( http://localhost:4200 /1.chunk.js:22280:30 ) 在 LocalDataSource.webpackJsonp.../../../../ng2-smart-table/lib/data-source/data-source.js.DataSource.emitOnChanged ( http://localhost:4200/1.chunk.js:22185:14 ) 在 LocalDataSource.webpackJsonp.../../../../ng2-smart-table/lib/data-source/data-source .js.DataSource.load ( http://localhost:4200/1.chunk.js:22105:14 ) 在 LocalDataSource.webpackJsonp.../../../../ng2-smart-table/lib/数据源/local/local.data-source.js.LocalDataSource.load ( http://localhost:4200/1.chunk.js:22243:38 )

4

2 回答 2

1

好的,我通过以下方式得到了它:

source: LocalDataSource;

constructor(private productService: ProductService,
            private flashMessage: FlashMessagesService,
            private router: Router,
            http: Http) 
{
   this.source = new LocalDataSource();
}

onChange(categoryid) {
this.productService.getProductsOncategory(categoryid).subscribe(data => {
  if (data.success) {
    this.source.load(data.products);
    console.log('Products obtained');
  } else {
    console.log('Not obtained!');
  }
});

}
于 2017-07-20T09:44:24.663 回答
0

有同样的问题。当我检查所有匹配列并在表数据中添加缺失的内容时,它解决了。例如我声明了一个变量

Settings = { 
....
columns: {
    id: {
    title: 'id',
    show: false,
    type: 'string',
  },
  name: {
    title: 'name',
    type: 'string',
  },
  //compare columns in json response in same variable declaration
  //for your data table [source]
 }
}

在第二种情况下,您的表尝试在完全加载数据之前从 dataTableSource 获取数据,以避免这种使用setTimeout();方法并设置更多时间。例如:

getChildData(): Promise<any> {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(this.childsList);
        }, 4000);//<= increase this value
    });
}

请原谅我的英语))

于 2017-07-29T09:41:04.710 回答