2017-07-02 11 views
0

由於缺少「root」元素,我無法填充我的dataTables。DataTables的JSON數據中缺少根元素

請參閱下面的代碼,並幫助我如何解決它。

件1:

$('#example').DataTable({ 
     //"ajax": "density.txt", 
     "ajax" : "getProductPropData", 
     "dataType": 'json', 
     "contentType": "application/json; charset=utf-8", 
     "columns": [{ 
      "data": "densityId" 
     }, { 
      "data": "densityDescription" 
     }, { 
      data: null, 
      className: "center", 
      defaultContent: '<a href="#" id="edit" class="edit" data-toggle="modal" data-target="#myModal">Edit</a>/<a href="#" id="delete">Delete</a>' 
       //defaultContent: '<button type="button" class="btn-default" data-toggle="modal" data-target="#myModal">Open Modal</button>' 
     }] 
    }); 

片2:我所doingin我的Struts 2 action類。

public String execute() throws Exception { 

    SessionFactory sf = (SessionFactory) ctx.getAttribute("SessionFactory"); 
    ProductPropertyDAO pdao = new ProductPropertyDAOImpl(sf); 
    List<DensityGroup> dg = pdao.getProductPropListData("Density"); 

    List<DensityGroup> list = new ArrayList<DensityGroup>(); 
    System.out.println("SIZE is: " + dg.size()); 

    for (int i = 0; i < dg.size(); i++) { 
     DensityGroup denpojo = new DensityGroup(); 
     denpojo.setDensityId(dg.get(i).getDensityId()); 
     denpojo.setDensityDescription(dg.get(i).getDensityDescription()); 
     list.add(denpojo); 
    } 

    data = list; 

    System.out.println("JSON is :\n" + data); 
    return "success"; 
    } 

我的JSON數據從Chrome的網絡標籤是:

[{"densityDescription":"16 KG","densityId":"21"}, 
{"densityDescription":"Chitti","densityId":"22"}] 

正確的數據應該是:

{"data" : [{"densityDescription":"16 KG","densityId":"21"}, 
{"densityDescription":"Chitti","densityId":"22"}]} 

編輯在數據變量:

private List<DensityGroup> data; 

    public List<DensityGroup> getData() { 
     return data; 
    } 

    public void setData(List<DensityGroup> data) { 
     this.data = data; 
    } 

DensityGroup.java

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.SequenceGenerator; 
import javax.persistence.Table; 

@Entity 
@Table(name="DENGROUP") 
public class DensityGroup{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    @Id 
    @Column (name = "DEN_ID") 
    @SequenceGenerator(name="dengroupSeq",sequenceName="density_group_seq") 
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="dengroupSeq") 


    private String densityId; 
    public String getDensityId() { 
     return densityId; 
    } 
    public void setDensityId(String densityId) { 
     this.densityId = densityId; 
    } 
    public String getDensityDescription() { 
     return densityDescription; 
    } 
    public void setDensityDescription(String densityDescription) { 
     this.densityDescription = densityDescription; 
    } 
    @Column (name = "DEN_DESC") 
    private String densityDescription; 
} 
+0

你的java代碼中'data'的類型是什麼?你在這裏指的是'data = list;' – Ravi

+0

Hi Ravi,數據是我在我的action類中創建的變量,它有getter和setter。我編輯了這個問題。 –

+0

你可以發佈'DensityGroup'類嗎? – Ravi

回答

1

使用ajax.dataSrc選項設置爲空字符串。

例如:

"ajax" : { 
    "url": "getProductPropData", 
    "dataSrc": "", 
    "dataType": 'json', 
    "contentType": "application/json; charset=utf-8" 
} 

參見this example代碼和示範。

+0

是的,我試過,但它仍然沒有填充數據 –

+0

@MittintiRamanaMurthy,增加了一個例子來證明我的解決方案有效。 –

+0

我發現我犯的錯誤。你是對的。 dataSrc應該在ajax中。 +1 –