2012-07-13 44 views
21

DataTables jqueryプラグインを初めて使用しました。 IE 8にJavascriptのパフォーマンスの問題があることを発見した後、私はDataTableを使ってサーバー側の処理を行う方法を変更することにしました。DataTablesエラー:「不明なパラメータが要求されました」

DataTables warning (table id = 'results_table'): Requested unknown parameter '0' from the data source for row 0 

私の周りGoogleで検索し、そのエラー・メッセージの多くの原因は、不正な形式のJSONに降りてくることがわかったので、私はへの道を見つけた:私のJSPのロード(私は春の3を使用しています)とき、私は、このエラーメッセージが出てい私のSpring 3コントローラ関数からJSONを出力してJSONを見て、DataTablesサイトのように見えるようにコードを変更しました。

まだエラーメッセージが表示されています

DataTablesで見つかったサーバー側の処理例には、クライアント側で使用されている列を指定するコードが含まれていなかったため、必要ないと仮定しました。私は?ここで

は私results.jspの関連する部分です。ここで

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 

<head> 
    <title>ACME: search results in a nice DataTables.net Plugin</title> 
</head> 
<body> 

<link rel="stylesheet" type="text/css" href="css/jquery.dataTables.css" /> 
<script language = "JavaScript" type = "text/javascript" src = "../nsd/js/jquery-1.7.js"></script> 
<script language = "JavaScript" type = "text/javascript" src = "../nsd/js/jquery.dataTables.js"></script> 

<script type="text/javascript"> 
$(document).ready(function() { 
    $('#results_table').dataTable({ 
     "bProcessing": true, 
     "bServerSide": true, 
     "sScrollX": "600px", 
     "sServerMethod": "POST", 
     "sAjaxSource": "/acme/resultstable", 
    }); 
}); 
</script> 


<form id="command" name="f" action="employee" method="post"> 

    <div id = "results"> 
     <table id = "results_table"> 
      <thead>   
       <tr> 
        <th>&nbsp;</th> 
        <th>ID</th> 
        <th>NO_PRINT</th> 
        <th>Full Name</th> 
        <th>Email Address</th> 
        <th>Phone Number</th> 
        <th>Organization</th> 
        <th>Organization Code</th> 
        <th>Position</th> 
        <th>Employee Type</th> 
       </tr> 
      </thead> 
      <tbody>   
      </tbody> 
     </table> 

    </body> 
</html> 

は、私はそれに送信されてきたJSON応答である:ここで

{ 
    "sEcho" : 1, 
    "iTotalRecords" : 1, 
    "iTotalDisplayRecords" : 1, 
    "aaData" : [ { 
    "person_id" : "888888", 
    "ID" : "999999", 
    "no_print" : "&nbsp;", 
    "fullname" : "Obama, Willard", 
    "email_address" : "<a href = \"mailto:[email protected]\">[email protected]</a>", 
    "current_phone_number" : "303-867-5309", 
    "title" : "&nbsp;", 
    "office" : "&nbsp;", 
    "position" : "Contractor", 
    "empl_code" : "CONT" 
    } ] 
} 

は私の春です私はジャクソン経由でJSON応答を送信するために使用しています。これにはJSONを出力するためのコードが含まれていますので、どのように見えるかわかります。 JSONをstdoutに出力し、DataTableに返送するものが違うのでしょうか?

@RequestMapping(value = "/resultstable", method = RequestMethod.POST) 
public @ResponseBody LinkedHashMap resultstable(ModelMap model,     
               HttpSession session, 
               @RequestParam (required=true) int sEcho, 
               @RequestParam (required=true) int iDisplayStart, 
               @RequestParam (required=true) int iDisplayLength,  
               @RequestParam (required=true) int iColumns, 
               @RequestParam (required=true) int iSortCol_0, 
               @RequestParam (required=false)String sSortDir_0, 
               @RequestParam (required=true) String sSearch) { 

    /* 
    ********************************************************************** 
    ** These come from the DataTables.net Jquery plugin on results.jsp 
    ********************************************************************** 
    ** sEcho,   - just send it back, used by DataTables for synching 
    ** iDisplayStart - index of the record to start with, ie 3 for the 3rd of 100 records 
    ** iDisplayLength - number of records to send back starting with iDisplayStart 
    ** iColumns  - number of columns to be displayed in the table 
    ** iSortCol_0  - the number of thee column to be sorted on 
    ** sSortDir_0  - direction of sorting: asc or desc 
    ** sSearch   - from the search box, filter results further on this term 
    ********************************************************************** 
    */ 

    String nextView     = "results"; 
    String usertype     = (String)session.getAttribute("usertype"); 
    Search search      = new Search(usertype); 
    List<LinkedHashMap> records  = null; 
    String results     = null; 
    int number_of_records    = (Integer)session.getAttribute("number_of_records_found"); 
    ResultsView rv     = new ResultsView(); 
    ResultsScreenTableHolder rstrh = null; 
    SearchScreenDataHolder ssdh2  = (SearchScreenDataHolder)session.getAttribute("search_screen_data_holder"); 
    ObjectMapper mapper    = new ObjectMapper(); 

    logger.debug("started"); 

    logger.debug("sEcho,   == " + sEcho  ); 
    logger.debug("iDisplayStart == " + iDisplayStart ); 
    logger.debug("iDisplayLength == " + iDisplayLength); 
    logger.debug("iColumns  == " + iColumns  ); 
    logger.debug("iSortCol_0  == " + iSortCol_0 ); 
    logger.debug("sSortDir_0  == " + sSortDir_0 ); 
    logger.debug("sSearch  == " + sSearch  ); 


    try { 
     records = search.searchForAnEmployee(ssdh2,usertype,sSearch,"asc", 
              iSortCol_0,iDisplayStart, 
              iDisplayLength);  


     LinkedHashMap lhm= new java.util.LinkedHashMap(); 
     lhm.put("sEcho", sEcho); 
     lhm.put("iTotalRecords",number_of_records); 
     lhm.put("iTotalDisplayRecords",9); 
     lhm.put("aaData",records); 

     // convert user object to json string, and save to a file 
     mapper.writeValue(new File("c:\\Downloads\\rstrh.json.txt"), lhm); 

     // display to console 
     logger.debug("My JSON: " + mapper.defaultPrettyPrintingWriter().writeValueAsString(lhm)); 

    } 
    catch (Exception e) { 
     logger.debug("\n",e); 
    } 

    return lhm;  

}// end function 
+0

情報は、あなたが... http://stackoverflow.com/a/12985883/661584 は、あなたがそれをうまくホープに役立つかもしれません。 Chrs – MemeDeveloper

答えて

13

私はこの同じ問題を今朝起していました。あなたはaoColumnsパラメータを持っており、これのようにmDataPropを使用する必要があります。

https://gist.github.com/1660712

少なくともそれは私の問題を解決しました。

+0

例をありがとう! – Steve

35

同じ警告が表示されましたが、原因は異なります。私はnullのデータを持っていました。 JSON形式は正しいですが、DataTablesはnullを表示するための既定の規則を持っているかどうかわかりません。解決策は、のsDefaultContentプロパティを使用することでした。

サンプルaaData:aoColumnsで、その後

aaData: [ 
    { "Field1": "Foo", "Field2":null }, 
    { "Field1": "Bar", "Field2":null }, 
] 

そして、次のようにプロパティを使用することができます。

aoColumns: [ 
    { "mData": "Field1", sDefaultContent: "n/a" }, 
    { "mData": "Field2", sDefaultContent: "" } 
] 

これはあなたの現在の問題ではありませんが、あなたがこの問題が発生する可能性があります未来。

希望しました。

+1

ありがとう!それが私を助けました。 –

+0

ありがとうございます。これは私の問題を解決しました。それはv1.9でのみ起こります。 1.8の場合、データテーブルは正常に実行されます –

+0

これは私の問題を解決しました。テーブルの最初の列はデータには関連付けられていませんが、現在のレコードを編集するためのボタンが表示されます。 –

2

sDefaultContentオプションを指定すると警告ボックスのみが表示されなくなります。データテーブルでは、null値を表示するためのルールが見つかりません。

テーブルのNULL値を変更すると、この警告は消えます。

1

それは私が私のmRender関数名にピリオドがあったので、私は同様のエラーを持っていた誰もが助け場合:
My.Namespace.MyFunction(data, row);

このライン:
var a = _fnSplitObjNotation(src);

スプリットこと明らかに生成し、別のオブジェクトに変換しますエラー。文字列関数名の代わりに、JavaScriptの関数オブジェクトを渡すときに

My_Namespace_MyFunction(data, row);

を使用して

は、さらに私は、このエラーに気づきました。

1

このエラーを回避するには、「th」列の表番号がデータ列(数値)を返すようにする必要があります。上記の問題ではaaDataです。

"aaData" : [ 
    [ 
    "person_id" : "888888", 
    "ID" : "999999", 
    ], 
    [ 
    "person_id" : "8888889", 
    "ID" : "9999990", 
    ] 
] 

これは、サーバー側の言語からデータを返すための正しい形式です。 私は同じように私の問題を解決しました。ここ

関連する問題