簡體   English   中英

jQuery數據表插件與字符串的數據綁定

[英]jQuery datatables plugin databinding with string

我從服務中收到JSON響應。 在學習完本教程之后,我創建了將數據綁定到datatables jquery插件中的響應。

客戶端代碼:

var test_reports = jsonResp.reports;
var aDataSet = [test_reports];

$('#example').dataTable( {
    "aaData": aDataSet,
    "aoColumns": [{ "sTitle": "Tests" },
          { "sTitle": "Reports"}]
});

在控制台中,我的“ test_reports”顯示:

['TEST_1','1'] ['TEST_2','1']

但是在將此數據綁定到表時,會引發錯誤。 如果我將此Cosole輸出復制到aaData中,它將創建表。 我知道我的“ test_reports”是一個字符串,並且此插件需要一個值數組。 任何使這項工作的想法!

給出此json響應的服務器端代碼

testcasesCountRS = statement.executeQuery(testcasesQuery);

            while(testcasesCountRS.next()){
                String test_name = testcasesCountRS.getString("test_name");
                String test_count = testcasesCountRS.getString("test_count");
                testResults.put(test_name, test_count);
                resBuffer.append("[\'" + test_name + "\',\'" + test_count + "\'],");
            }

resBuffer = resBuffer.deleteCharAt(resBuffer.lastIndexOf(","));

reports.put("reports", resBuffer);

我的服務器端代碼中是否還有其他選擇可以將響應作為數組對象發送到datatables插件。

您的服務器響應應該是一個數組數組。

[['TEST_1','1'],['TEST_2','1']]

將您的代碼更改為

resBuffer.append("[");
while(testcasesCountRS.next()){
    String test_name = testcasesCountRS.getString("test_name");
    String test_count = testcasesCountRS.getString("test_count");
    testResults.put(test_name, test_count);
    resBuffer.append("[\'" + test_name + "\',\'" + test_count + "\'],");
}
resBuffer = resBuffer.deleteCharAt(resBuffer.lastIndexOf(","));
resBuffer.append("]");

上面的代碼未經測試。 但是希望您能明白。

文檔鏈接

我通過以下方式使其工作:

JSONArray testCaseArray = new JSONArray();
while(testcasesCountRS.next()){
                JSONArray testCase = new JSONArray();
                String test_name = testcasesCountRS.getString("test_name");
                String test_count = testcasesCountRS.getString("test_count");

                testCase.add(test_name);
                testCase.add(test_count);
                testCaseArray.add(testCase);
}

reports.put("reports", testCaseArray);

唯一困擾我的是,它是如此愚蠢的代碼,以至於我每次循環時都需要創建一個新的Array。 並將這些數組添加到我的主Array對象中。 應該使它變得簡單一些。 請提出一些有效的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM