无法解析来自服务器的 Cakephp Jquery Datatable JSON 数据
Cakephp Jquery Datatable JSON data from server could not be parsed
我在使用 cakephp 和 jquery (v 1.2.0) 时收到以下警告:
DataTables 警告(table id = 'tblinv'):DataTables 警告:JSON 来自服务器的数据无法解析。这是由 JSON 格式错误引起的。
这是我的脚本代码
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 输入").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
有什么想法吗?我看不到格式错误...
我有一个简单但有效的解决方案,当我们进行多次搜索(我们输入并删除并再次输入几个单词)时,json 会损坏并且代码不会发送任何 json , 所以我们需要在过滤后重新绘制数据表
$("thead input").keyup(function () {
dt.fnFilter(HtmlEncode(this.value), $("thead input").index(this));
dt.fnDraw();
});
就这些了,因为我正在使用服务器端,所以我不需要重新加载 ajax,只需重新绘制,我希望这对某些人有所帮助。
我在使用 cakephp 和 jquery (v 1.2.0) 时收到以下警告: DataTables 警告(table id = 'tblinv'):DataTables 警告:JSON 来自服务器的数据无法解析。这是由 JSON 格式错误引起的。
这是我的脚本代码
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 输入").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
有什么想法吗?我看不到格式错误...
我有一个简单但有效的解决方案,当我们进行多次搜索(我们输入并删除并再次输入几个单词)时,json 会损坏并且代码不会发送任何 json , 所以我们需要在过滤后重新绘制数据表
$("thead input").keyup(function () {
dt.fnFilter(HtmlEncode(this.value), $("thead input").index(this));
dt.fnDraw();
});
就这些了,因为我正在使用服务器端,所以我不需要重新加载 ajax,只需重新绘制,我希望这对某些人有所帮助。