JSON from a ASHX C#

JSON from a ASHX C#

nesrallanesralla Posts: 1Questions: 0Answers: 0
edited September 2012 in DataTables 1.9
Hi,
I new with a jquery datatable, i have some trouble with my json return from a ashx handle...how to construct a datatable with only 4 columns , from a json fields....o look on a site but i'm not understand it...can anyone help me !? with a pratice exemple....

first one is my json return

[code]
[{"US_EQU_ID":9,"PRE_ID":119,"PRE_ETA_ID":9,"PRE_PRG_ID":1,"US_ID":0,"US_NOME":"teste 6","US_EQU_DATACADASTRO":"\/Date(1347559510173)\/","US_EQU_FUNCAO":"teste 6","US_EQU_ATRIBUICAO":" teste 6","US_EQU_RESPONSAVEL":"N","US_EQU_RESPONSAVELPROJETO":"S","US_EQU_RESPONSAVELTECNICO":"S","DBInfo":{"Operacao":0,"Codigo":null,"Message":null,"HasError":true,"Translatable":false},"WSMessage":null,"WSCode":null,"SizeList":0,"Index":0,"AllFiles":null,"GroupCount":0,"RowsCount":0},{"US_EQU_ID":8,"PRE_ID":119,"PRE_ETA_ID":9,"PRE_PRG_ID":1,"US_ID":0,"US_NOME":"teste 5","US_EQU_DATACADASTRO":"\/Date(1347559348203)\/","US_EQU_FUNCAO":"teste 5","US_EQU_ATRIBUICAO":"teste 5","US_EQU_RESPONSAVEL":"N","US_EQU_RESPONSAVELPROJETO":"S","US_EQU_RESPONSAVELTECNICO":"S","DBInfo":{"Operacao":0,"Codigo":null,"Message":null,"HasError":true,"Translatable":false},"WSMessage":null,"WSCode":null,"SizeList":0,"Index":0,"AllFiles":null,"GroupCount":0,"RowsCount":0},{"US_EQU_ID":7,"PRE_ID":119,"PRE_ETA_ID":9,"PRE_PRG_ID":1,"US_ID":0,"US_NOME":"teste 3","US_EQU_DATACADASTRO":"\/Date(1347559000810)\/","US_EQU_FUNCAO":"teste 3","US_EQU_ATRIBUICAO":"teste 4","US_EQU_RESPONSAVEL":"N","US_EQU_RESPONSAVELPROJETO":"S","US_EQU_RESPONSAVELTECNICO":"S","DBInfo":{"Operacao":0,"Codigo":null,"Message":null,"HasError":true,"Translatable":false},"WSMessage":null,"WSCode":null,"SizeList":0,"Index":0,"AllFiles":null,"GroupCount":0,"RowsCount":0},{"US_EQU_ID":6,"PRE_ID":119,"PRE_ETA_ID":9,"PRE_PRG_ID":1,"US_ID":0,"US_NOME":"teste 2","US_EQU_DATACADASTRO":"\/Date(1347558982907)\/","US_EQU_FUNCAO":"teste 2","US_EQU_ATRIBUICAO":"teste 3","US_EQU_RESPONSAVEL":"N","US_EQU_RESPONSAVELPROJETO":"S","US_EQU_RESPONSAVELTECNICO":"S","DBInfo":{"Operacao":0,"Codigo":null,"Message":null,"HasError":true,"Translatable":false},"WSMessage":null,"WSCode":null,"SizeList":0,"Index":0,"AllFiles":null,"GroupCount":0,"RowsCount":0},{"US_EQU_ID":5,"PRE_ID":119,"PRE_ETA_ID":9,"PRE_PRG_ID":1,"US_ID":0,"US_NOME":"teste","US_EQU_DATACADASTRO":"\/Date(1347558963340)\/","US_EQU_FUNCAO":"teste","US_EQU_ATRIBUICAO":"teste","US_EQU_RESPONSAVEL":"N","US_EQU_RESPONSAVELPROJETO":"S","US_EQU_RESPONSAVELTECNICO":"S","DBInfo":{"Operacao":0,"Codigo":null,"Message":null,"HasError":true,"Translatable":false},"WSMessage":null,"WSCode":null,"SizeList":0,"Index":0,"AllFiles":null,"GroupCount":0,"RowsCount":0}]
[/code]


second is my datatable jquery
[code]
$('#tbUsuariosEquipes').dataTable();
[/code]

here is my html tags
[code]










US_EQU_id
PRE_id
PRE_ETA_id
US_nome
US_id




US_EQU_id
PRE_id
PRE_ETA_id
US_nome
US_id






[/code]

how i put only collunms of my choice !? my json notation is in an correct format ?!
sorry for my english !!

thanks in advance !!
Igor

Replies

  • robertmazzorobertmazzo Posts: 22Questions: 0Answers: 0
    Hi Igor,
    I've recently solved a similar problem, however in my case the source data was in XML format. However, I converted the XML elements into the appropriate Json format as input to the Datatable.
    Here's an example. I hope it helps:

    [code]
    function parsePortfolios(xml) {
    var PfJsonData = [];

    $(this).find("exposureProfile node").each(function () {
    var term = $(this).find('tag').text();
    var exposure = parseFloat($(this).find('exposure').text()).toLocaleString();
    var dateArry = $(this).attr('date').split("-");
    var nodeDate = dateArry[1] + "/" + dateArry[2] + "/" + dateArry[0];

    // CREATE JSON DATA OF PORTFOLIOS !!
    PfJsonData.push({
    "PfId": pfId,
    "Name": name,
    "ExpType": expType,
    "Date": nodeDate,
    "Term": term,
    "Exposure": exposure
    });
    });

    oTablePf = $('#pftable').dataTable({
    "aaData": PfJsonData, // HERE'S YOUR JSON DATA INPUT !!!
    "sScrollY": "450px",
    "sPaginationType": "full_numbers",
    "aoColumns":[
    { "mData": "PfId" },
    { "mData": "Name", },
    { "mData": "ExpType" },
    { "mData": "Date" },
    { "mData": "Term" },
    { "mData": "Exposure" }
    ],
    'aoColumnDefs': [ // NB: col index starts from zero

    { "sTitle": "Pf Id", "aTargets": [0] }, // also defined in Html table below
    { "sTitle": "Name", "aTargets": [1] },
    { "sTitle": "Exp Type", "aTargets": [2] },
    { "sTitle": "Date", "aTargets": [3] },
    { "sTitle": "Term", "aTargets": [4] },
    { "sTitle": "Exposure", "aTargets": [5] },
    { "sClass": "td_pfid", "aTargets": [0] }, // add a class attrib to cell data, column 0 "Pf Id"
    { "sClass": "td_nodedate", "aTargets": [3] },
    { "aDataSort": [1, 3], "aTargets": [1] }, // sort by Name + Date
    { "sType": "numeric", "aTargets": [5] }, // col 5 is exposure
    { "bVisible": true, "aTargets": [0] } // TO HIDE A COLUMN !!
    ],
    "iDisplayLength": 50,
    "bLengthChange": true, // enable Show menu
    "oLanguage": {
    "sSearch": "Search all:",
    "oPaginate": {
    "sFirst": "First ",
    "sPrevious": "Prev ",
    "sNext": " Next ",
    "sLast": "Last"
    }
    }
    });
    } // end parsePortfolios()
    [/code]
  • robertmazzorobertmazzo Posts: 22Questions: 0Answers: 0
    for testing purposes, try to put your Json data inline to the "aaData" property:

    oTablePf = $('#pftable').dataTable({
    "aaData": [{"US_EQU_ID":9,"PRE_ID":119,"PRE_ETA_ID":9,"PRE_PRG_ID":1,"US_ID":0,"US_NOME":"teste 6"}],
    ...
    });

    also, good example here: http://www.datatables.net/release-datatables/examples/ajax/objects.html
This discussion has been closed.