Search code examples
javajquerydatatableserver-side

java datatable server side issue


I am working on datable that populate the data from server side, issue i am facing is that it always give a alert box that shows

DataTables warning: table id=firmtable - Requested unknown parameter '1' for row 0. For more information about this error, please see http://datatables.net/tn/4 i am unable to trace where this issue might be

my json

{
    "sEcho":"3",
    "iTotalRecords":10,
   "iTotalDisplayRecords":10,
    "aaData":
        "[
             {\"LogId\":\"108\",
             \"tableName \":\"game\",
             \"columnName\":\"Status\",
             \"oldValue\":\"0\",
             \"newValue\":\"1\",
             \"changeTypeText\":\"Update \",
             \"changedByName\":\"abc\"}
         ]"
}

this is how i have worked it on server side

Iterator<LogInfo> i = logList.iterator();
int row = 0;
JsonObject returnObj = new JsonObject();
JsonArray dataArray = new JsonArray();
while (i.hasNext()) {
    LogInfo logInfo = (LogInfo) i.next();
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("logId", logInfo.getLogId());
    jsonObject.addProperty("tableName", logInfo.getTableName());
    jsonObject.addProperty("columnName", logInfo.getColumnName());
    jsonObject.addProperty("oldValue", logInfo.getOldValue());
    jsonObject.addProperty("newValue", logInfo.getNewValue());
    jsonObject.addProperty("changeTypeText", logInfo.getChangeTypeText());
    jsonObject.addProperty("changedByName", logInfo.getChangedByName());
    row++;
    dataArray.add(jsonObject.getAsJsonObject());
    }
returnObj.addProperty("sEcho", "3");
returnObj.addProperty("iTotalRecords", row);
returnObj.addProperty("iTotalDisplayRecords", row);
returnObj.addProperty("aaData", dataArray.toString());
PrintWriter out = response.getWriter();
Gson gson = null;
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat(JarolConstants.AJAX_DATE_FORMAT);
gson = builder.create();
String resultStr = gson.toJson(returnObj);
out.print(resultStr);
out.close();

whats happening on the client side it that mytable is not getting populated html code

script

     $(document).ready(function() {
         $('#firmtable').dataTable({
             "bProcessing" : true,
             bServerSide : true,
             sAjaxSource : "./log!list.action",
             sServerMethod : "POST"
         });
     }); </script>


<table id="firmtable">
                <thead>
                    <tr class="detail">
                         <th><s:property value="getText('auditLog.jsp.transactionId')" /></th>
                        <th><s:property value="getText('auditLog.jsp.tableName')" /></th>
                        <th><s:property value="getText('auditLog.jsp.columnName')" /></th>
                        <th><s:property value="getText('auditLog.jsp.oldValue')" /></th>
                        <th><s:property value="getText('auditLog.jsp.newValue')" /></th>
                        <th><s:property value="getText('auditLog.jsp.changeTypeId')" /></th>
                        <th><s:property value="getText('auditLog.jsp.changeBy')" /></th>
                        <th><s:property value="getText('auditLog.jsp.changeOn')" /></th>
                        <th class="edit"><s:property value="getText('global.action')" /></th> 
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>

EDIT i have also tried sEcho value setting it to

returnObj.addProperty("sEcho", Integer.parseInt(request.getParameter("sEcho")));

but no result, same issue i am getting

what result i get is the table first column gets populated with the aaData content like this

[ { L o g I d 1 0 8 i.e. single letter in first column


Solution

  • my problem is solved few things that i have changed i was using com.google.gson.Gson for json now i am using org.json which solved the issue of \ in my aaData then I used newer version of datatable and added jsonObject.put("DT_RowId", row); to my aaData now my json is as follows

    {
       "recordTotal":10,
       "draw":"3",
       "recordsFiltered":10,
       "data":[
          {
             "LogId":"112",
             "changeTypeText":"Update",
             "changedOn":"2015-05-27 18:05:43.113",
             "newValue":"1000.00",
             "changedByName":"Lalit Singh1",
             "tableName":"Game",
             "DT_RowId":0,
             "columnName":"jackpot_Amount",
             "oldValue":"1500.00"
          }]
    

    and in javascript code I have added

    $('#LogTable').dataTable({
       "bProcessing" : true,
       bServerSide : true,
       sAjaxSource : "./log!lList.action",
       sServerMethod : "POST",
       "columns": [
           { "data": "LogId" },
           { "data": "tableName" },
           { "data": "columnName" },
           { "data": "oldValue" },
           { "data": "newValue" },
           { "data": "changeTypeText" },
           { "data": "changedByName" },
           { "data": "changedOn" },
        ]
    });