Reputation: 137
I'm having the following warning using cakephp and jquery (v 1.2.0) : DataTables warning (table id = 'tblinv'): DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.
Here is my script code
var dt;
var tipoStock = "lkdfjdklfjdklfjd"; /* pm 3.2.2015 */
$(document).ready(function () {
$("#filtro-semaforos").chosen({
allow_single_deselect: true,
disable_search: true,
width: '40px'
});
dt = $("#tblinv").DataTable({
"bProcessing": true,
"bServerSide": true,
'sAjaxSource': "<?php echo $this->base; ?>/Inventarios/stock<?php if(!empty($tipoInventario)){ echo "/".$tipoInventario; } ?>",
'bAutoWidth': false,
'bSort': true,
"oSearch": {"bRegex": false, "bSmart": true},
"decimal": ",",
"thousands": ".",
'iDisplayLength': 20,
"sPaginationType": "full_numbers",
"sDom": 'T<"clear">lfrtip',
//"aoColumns": [null, null, null, null, null, {"sType": 'numeric-comma'}, {"sType": 'numeric-comma'}, {"sType": 'numeric-comma'}, null, null, null,null],
// "oTableTools": {
// },
"oTableTools": {
"sSwfPath": "<?php echo $this->webroot; ?>js/plugins/datatables/swf/copy_csv_xls_pdf.swf",
"aButtons": [
]
},
"aaSorting": [],
"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
$(nRow).children().each(function (index, td) {
switch (index)
{
case 5:
{
if ($(td).html() !== '')
{
$(td).addClass("pesos");
$(td).addClass("derecha_p");
}
}
break;
case 6:
{
if ($(td).html() !== '')
{
$(td).prepend('<small class="small">US$ </small>');
$(td).addClass("derecha_p");
//$(td).addClass("usd");
}
}
break;
case 7:
{
if (parseInt($(td).html()) <= 0)
{
$(td).css("color", "red");
}
$(td).addClass("tdr");
$(td).addClass("gran");
}
break;
case 8:
{
$(td).html($('<small>').append(' ' + $(td).html()));
}
break;
case 11:
$(td).css("display", "none");
break;
}
});
return nRow;
},
"fnInitComplete": function () {
$("#cargando").hide();
$("#divtbl").fadeIn('slow');
}
});
// dt.fnFilter( 'Buscar....'); // Codigo Original. pm para que e traiga la busqueda vacia de entrada
dt.fnFilter(''); // pm para que e traiga la busqueda vacia de entrada
// workaround por Diego:
// vaciar el contenido para que quede sin el "Buscar.."
// $("#tblinv_filter input").val("");
String.prototype.replaceAll = function (str, str2) {
return this.split(str).join(str2);
};
// nota Diego: esto pareciera que no funciona, está de más
$("thead input").keyup(function () {
dt.fnFilter(HtmlEncode(this.value), $("thead input").index(this));
});
var p = false;
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"currency-pre": function (a) {
return parseFloat(a.replaceAll("$", "").replaceAll('usd', '').replaceAll(".", "").replaceAll(",", "."));
},
"currency-asc": function (a, b) {
return a - b;
},
"currency-desc": function (a, b) {
return b - a;
}
});
$('#tblinv').dataTable().fnClearTable();
$('#tblinv_filter label input:text').focus(); // pm 18.2.2015 a ver si hace focus
});
// document.getElementById('tblinv').style.display = 'none'; // pm agrega 18.2
Any ideas? i can't see the formatting error...
Upvotes: 2
Views: 338
Reputation: 137
i have an easy but effective solution, when we make several searchs (we type and delete and type again several words) the json will get damaged and the code will not send any json, so we need to redraw the datatable after doing the filter
$("thead input").keyup(function () {
dt.fnFilter(HtmlEncode(this.value), $("thead input").index(this));
dt.fnDraw();
});
and that's all, as i'm using serverside i don't need to reload ajax, just redraw, i hope this help somebody.
Upvotes: 1