Angular 2 Jquery Datatables.net服务器端无法正常工作

时间:2019-03-30 07:47:50

标签: java jquery angular datatables

当数据从服务器返回时,它永远不会填充到表中。

“我可以从'success'回调中看到结果,但是该表未填充,并且'Processing ...'也继续显示”

使用Angular 6,Jquery Datatables和Spring Rest Controller

npm install jquery --save
npm install datatables.net --save
npm install datatables.net-dt --save
npm install angular-datatables@6.0.0 --save
npm install @types/jquery --save-dev
npm install @types/datatables.net --save-dev

HTML代码

<table id=paymentHistoryTable datatable [dtOptions]="dtOptions class="table table-striped table-bordered" >
            <thead>
              <tr>
  <th width="10%">Account ID</th>
                <th width="12%">Name</th>
   </tr>
            </thead>
            <tbody>
            </tbody>
          </table>

角度代码如下

  getPaymentHistoryPagination() {
    this.dtOptions = {
      pagingType: 'full_numbers',
      serverSide: true,
      processing: true,
       ajax: {
    url: this.baseUrl + '/getPaymentHistoryPagination',
    type: 'POST',
    contentType: 'application/json; charset=UTF-8',
    error: function(xhr, error, code) { console.log("error::" +error); },
    success: function(result) {console.log(JSON.stringify(result))},
    data: function(data) {
        return JSON.stringify(data);
    }
}, columns: [
      { data: "customerId", title: 'Departure Time'},
      { data: "customerName", title: 'Customer Name'}
    ]
  };

  }

Java控制器如下

@RequestMapping(value = "/getPaymentHistoryPagination", method = RequestMethod.POST)
@ResponseBody
public DataTableResponse getPaymentHistoryPagination1(@RequestBody @Valid SearchPropertyParamDTO dto) {
    DataTableResponse dataTableResponse = new DataTableResponse();
try {
dataTableResponse.setDraw(1);
dataTableResponse.setRecordsFiltered(1L); //One record for testing
dataTableResponse.setRecordsTotal(1L);
  Invoice invoice = new Invoice();
        invoice.setCustomerId(111L);
        invoice.setCustomerName("Abc");

        List invoices = new ArrayList();
        invoices.add(invoice);
        dataTableResponse.setData(invoices);
    } catch (Exception ex) {
        //LOG.error("Error in getAllProperty", ex);
    }
    return dataTableResponse;
}

DataTableResponse如下:

public class DataTableResponse {
private int draw =1;
private long recordsTotal ;
private long recordsFiltered ;
   private List data;

}

预期结果:应呈现表 实际结果:获取消息处理 获得成功回应 {“绘制”:1,“ recordsTotal”:1,“ recordsFiltered”:1,“数据”:[{“ id”:null,“ customerId”:111,“ customerName”:“ Abc”}]} 但没有数据填充

1 个答案:

答案 0 :(得分:0)

在Angular项目中安装jQuery和jQuery插件对您的项目非常不利。

看看我的Angular表,很抱歉,我还没有太多机会对其进行记录,但是这里有一篇文章和一个StackBlitz。

https://www.reddit.com/r/Angular2/comments/b76924/easy_angular_table/

https://stackblitz.com/edit/angular-npn1p1?file=src%2Fapp%2Fapp.component.html