When rendering a table from a json, only the first column takes the value " always, rest are null

When rendering a table from a json, only the first column takes the value " always, rest are null

Alejandro_123Alejandro_123 Posts: 1Questions: 1Answers: 0
edited May 2017 in Free community support

Hello,

When I'm loading a table from a JSON (server side generated) it will only fill the first column with the same value " and the rest of columns are null. I suspect this is because of how the Json is generated, but I can't change that, so I'd like to know if I can work around this with datatables.

HTML Table:

<table id="tablaDetalle2" class="display" role="grid" aria-describedby="example2_info">
<thead>
tr>
<th>id</th>
<th>idFlight</th>
<th>idSample</th>
<th>import</th>
</tr>
</thead>
<tbody>
</table>

JS DataTable:

$('#tablaDetalle2').DataTable({
      "bProcessing": true,
      "sAjaxSource": "@Url.Action("Detalle","Muestra", new {idExp = 1, descrExp="abc"})"
});

Json returned:

[{"id":"1","idFlight":1,"idSample":3217.5,"import":0}]

No matter how I change the column order, or whatsoever I can't manage to get it.

Can anybody help?Thanks.

EDIT
Just playing I used the propertiy bServerSide:True, now it takes all the json and for each character it puts it in the first colum. If the json was "abc":1, it would return the first column: " a b c " : 1 and the rest as null.

EDIT2

Went super simple, I now only render the column id (modified html, json, and all the code needed), now no error is returned, btu the value for the column id for all the rows is ". Apparently isn't accessing to the json data properly.

HTML Table:

<table id="tablaDetalle2" class="display" role="grid" aria-describedby="example2_info">
<thead>
<tr>
<th>id</th>
</tr>
</thead>
<tbody>
</table>

JS DataTable:

$('#tablaDetalle2').DataTable({
      "bProcessing": true,
      "sAjaxSource": "@Url.Action("Detalle","Muestra", new {idExp = 1, descrExp="abc"})",
      "columns" : [
            {"aaData":"id"}]
});

JSON Example:

[{"id":1},{"id":2}]

The value is always ".

EDIT 3
Just foudn out, that for each value of the Json, it will asign a column. If my Json has 500 characetrs, it will create 500 columns with only value on the first column.

EDIT 4

made some changes in my coed, using directly ajax property now, apparently not being able t oaccess the field id even though its clearly declared on json and html table:

** HTM TABLE **

                            <table id="tablaDetalle2" class="display" role="grid"
                                aria-describedby="example2_info">
                                <thead>
                                    <tr>
                                   <th>id</th>
                                    <!--<th>idGastso</th>
                                    <th>idMuestra</th>
                                    <th>ImpGasto</th>-->
                                </tr>
                                </thead>
                                <tbody>

JS CODE TABLE



$('#tablaDetalle2').DataTable({ "processing" :true, "ajax": { "url": "@Url.Action("Detalle","Muestra", new {idExp = 1, descrExp="abc"})" }, "columns": [ {"data":"id"} ] });

JSON received (logged in console)

[object Object]{data: "[{"id":1},{..."}

"[{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1},{"id":1}]"

If i dont add the option "columns" it will just take each char of the json and put it in one column, otherwie the value not found error pops up.

This discussion has been closed.