Error on creating Dynamically Datatable columns and data

Error on creating Dynamically Datatable columns and data

devenchhillardevenchhillar Posts: 3Questions: 2Answers: 0
edited October 2022 in Free community support

Referred this link - http://live.datatables.net/qimukefe/1/edit

Error - invalid JSON primitive: draw at Deserialize Primitive Object()

Description - We have different reports - selecting a report name and submitting it - I am getting Data -column names and data

Please advice
Code

 var columnsss =[];
  ObjData["Facility_ID"] = $('#Facility_ID').val();
        ObjData["Report_Type_Name"] = $("#Report_Type option:selected").text();
        ObjData["IsGetReport"] = true;

        var dataProp = { model: ObjData };
        $.ajax({
            type: "POST",
            url: '../GenericReport/_AjaxGenericReport',
            data: JSON.stringify(dataProp),
            contentType: "application/json; charset=utf-8",
            dataType: "json",           
            success: function (result) {
                if (condition) {
                    $('#DivtableData').show();    
            var data = eval(result.data);  
               for (var i in data[0][0]) {
             columnsss.push({
            data: i,
            title: capitalizeFirstLetter(i)
        });
    }
    $('#btnSubmit').prop('disabled', false);
    $('.table').DataTable({        
        processing: true,
        serverSide: true,         
        ajax: {
            url: '../GenericReport/_AjaxGenericReport',
            type: 'POST',           
            contentType: "application/json; charset=utf-8"            
        }, 
        columnDefs: [],
        columns: columnsss,
        'rowCallback': function (row, data, dataIndex) {
          
        }
    });
  };
});
function capitalizeFirstLetter(string) {
    return string.toString().charAt(0).toUpperCase() + string.slice(1) ;
}

this is controller side

[HttpPost]
public JsonResult _AjaxGenericReport(GenericReportViewModel model)
{
    try
    {
        model.USER_NAME = UserData.username.Trim();
        model = _genericReportService.GenericReportSVC(model);          
    string JSONString = string.Empty;
        JSONString = JsonConvert.SerializeObject(model.Data); 
        return Json(
                new
                {                         
                    data  = JSONString
                }, JsonRequestBehavior.AllowGet);               
    }
    catch (Exception ex)
    {
        //return Json(new { Success = false, Message = ex.Message },JsonRequestBehavior.AllowGet);
    }
}

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 63,451Questions: 1Answers: 10,465 Site admin

    How many rows do you have in the table? To you need server-side processing enabled? If so, you would need to fully implement the client / server communication that DataTables requires in your server-side code.

    Allan

  • devenchhillardevenchhillar Posts: 3Questions: 2Answers: 0

    Thank you for the response. In current implementation I am building the table on server side with the data.
    For few reports - data is not much thats not the issue but for few reports it's timing out even we use date filters

  • allanallan Posts: 63,451Questions: 1Answers: 10,465 Site admin

    Fair enough - if it is taking too long to response with all records, then server-side processing probably is the way to go (typically we say 10'000 or more records is when you should start thinking about server-side processing).

    Allan

Sign In or Register to comment.