问题描述
哎呀,看来我错了。由于@MDeSchaepmeester&安培; @charlietfl指出它应该工作,我现在看到它。问题是现在没有渲染数据表。
任何建议,为什么它不加载到数据表,并没有错误,欢迎!
下面是为要求所有code:
函数PartnerListSupportTickets(){ jQuery的(#装载机)显示()。 VAR idOpen ='门票-TBL-开放;
VAR idOnHold ='门票-TBL-上保持';
VAR idDeferred ='门票-TBL-递延';
VAR idClosed ='门票-TBL-封闭';
变种tblDataOpen = [];
变种tblDataOnHold = [];
变种tblDataDeferred = [];
变种tblDataClosed = [];
变种tblEmptyMsg = []; tblEmptyMsg.push({
门票:此类别的票号,
后code:' - ',
状态:' - ',
客户:' - ',
状态: '-',
优先: '-',
故障:' - ',
类别:' - ',
作者:' - ',
老板:' - ',
年龄:' - ',
最后更新: '-'
}); VAR get_data_url ='/sma-php/gettickets.php?var=PARTCK&pid='+ sessionStorage.party_id; jQuery.getJSON(get_data_url,功能(数据){ OpenNum = 0;
OnHoldNum = 0;
DeferredNum = 0;
ClosedNum = 0; jQuery.each(数据,功能(I,D){ VAR状态= d.status;
状态= status.toLowerCase(); 如果(状态=='关闭'){
VAR现在时刻=();
VAR然后=时刻(d.create_date);
d.days_age = now.diff(当时,'天');
}; 开关(状态){
案(开放):
OpenNum = OpenNum + 1;
tblDataOpen.push({
门票:'<跨度ID =+ d.ticket_id +'级=票ID级>' + d.short_title +'< / SPAN>,
后code:d.post code,
状态:d.state,
顾客:d.customer_name,
状态:d.status,
优先级:'<字体颜色=+ d.color +'>' + d.priority +'< / FONT>,
故障:d.fault_type,
类别:d.category,
作者:d.author_name,
老板:d.owner_name,
年龄:d.days_age,
LASTUPDATE:d.update_date
});
打破; 案例('搁置'):
OnHoldNum = OnHoldNum + 1;
tblDataOnHold.push({
门票:'<跨度ID =+ d.ticket_id +'级=票ID级>' + d.short_title +'< / SPAN>,
后code:d.post code,
状态:d.state,
顾客:d.customer_name,
状态:d.status,
优先级:'<字体颜色=+ d.color +'>' + d.priority +'< / FONT>,
故障:d.fault_type,
类别:d.category,
作者:d.author_name,
老板:d.owner_name,
年龄:d.days_age,
LASTUPDATE:d.update_date
});
打破; 案例('递延'):
DeferredNum = DeferredNum + 1;
tblDataDeferred.push({
门票:'<跨度ID =+ d.ticket_id +'级=票ID级>' + d.short_title +'< / SPAN>,
后code:d.post code,
状态:d.state,
顾客:d.customer_name,
状态:d.status,
优先级:'<字体颜色=+ d.color +'>' + d.priority +'< / FONT>,
故障:d.fault_type,
类别:d.category,
作者:d.author_name,
老板:d.owner_name,
年龄:d.days_age,
LASTUPDATE:d.update_date
});
打破; 案(关闭):
ClosedNum = ClosedNum + 1;
tblDataClosed.push({
门票:'<跨度ID =+ d.ticket_id +'级=票ID级>' + d.short_title +'< / SPAN>,
后code:d.post code,
状态:d.state,
顾客:d.customer_name,
状态:d.status,
优先级:'<字体颜色=+ d.color +'>' + d.priority +'< / FONT>,
故障:d.fault_type,
类别:d.category,
作者:d.author_name,
老板:d.owner_name,
年龄:d.days_age,
LASTUPDATE:d.update_date
});
打破; 默认:
}; });
如果(OpenNum大于0){
renderPartnerTicketsList(tblDataOpen,idOpen,真正的);
}其他{
renderPartnerTicketsList(tblEmptyMsg,idOpen,真正的);
};
如果(OnHoldNum大于0){
renderPartnerTicketsList(tblDataOnHold,idOnHold,真正的);
}其他{
renderPartnerTicketsList(tblEmptyMsg,idOnHold,真正的);
};
如果(DeferredNum大于0){
renderPartnerTicketsList(tblDataDeferred,idDeferred,真正的);
}其他{
renderPartnerTicketsList(tblEmptyMsg,idDeferred,真正的);
};
如果(ClosedNum大于0){
renderPartnerTicketsList(tblDataClosed,idClosed,真正的);
}其他{
renderPartnerTicketsList(tblEmptyMsg,idClosed,真正的);
}; jQuery的('一[HREF =#tab1c47048a])HTML(开放门票<跨度类=票数>'+ OpenNum +'< / SPAN>');
。jQuery的('一[HREF =#tab281c055c])HTML(待定门票<跨度类=票数>'+ OnHoldNum +'< / SPAN>');
jQuery的('一[HREF =#tab328b0619])HTML。('递延门票<跨度类=票数>'+ DeferredNum +'< / SPAN>');
jQuery的('一[HREF =#tab26b60552])HTML(封闭门票<跨度类=票数>'+ ClosedNum +'< / SPAN>');
})
.done(函数(){
jQuery的(#装载机)淡出(慢)。
})
.fail(功能(jqxhr,textStatus,错误){
变种sysError = textStatus +,+误差;
showPopupMsg(errorClass,logoutFlag,有一个错误检索您的支持票名单,LT; BR />页面无线本地环路不从这点加载,你将返回到我们的主页...< BR />错误: + sysError);
});
};功能renderPartnerTicketsList(tblData,ID,initLoad){ //为DataTable的插件设置数据 如果(!initLoad){
VAR TBL =的jQuery('#'+ id)的.DataTable();
tbl.clear();
tbl.rows.add(tblData);
tbl.draw();
返回;
}; //绑定DataTable的插件
VAR tblHtml ='<表格的cellpadding =0CELLSPACING =0BORDER =0级=显示ID =+身份证+'>< THEAD>< TR>';
tblHtml + ='<第i门票和LT; /第i';
tblHtml + ='<第i个帖子code< /第i';
tblHtml + ='<第i个国家和LT; /第i';
tblHtml + ='<第i个客户和LT; /第i';
tblHtml + ='<第i状态和LT; /第i';
tblHtml + ='<第i个优先级和LT; /第i';
tblHtml + ='<第i个故障与LT; /第i';
tblHtml + ='<第i类和LT; /第i';
tblHtml + ='<第i个作者< /第i';
tblHtml + ='<第i个业主和LT; /第i';
tblHtml + ='<第i年龄和LT; /第i';
tblHtml + ='<第i最后更新< /第i';
tblHtml + ='< / TR>< / THEAD><&TBODY GT;< / TBODY>< /表>'; 。jQuery的('#'+ ID +'-container')HTML(tblHtml); jQuery的('#'+ id)的.dataTable({
数据:tblData,
列: [
{数据:准考证},
{数据:后code},
{数据:国家},
{数据:客户},
{数据:身份},
{数据:优先级},
{数据:过失},
{数据:类别},
{数据:作者},
{数据:所有者},
{数据年龄},
{数据:LASTUPDATE}
]
pageLength:25,
命令:[12,'递减'],
scrollCollapse:假的,
jQueryUI的:真实,
回应:真
});
};
我失去下一次,我把它传递给函数的code数组内的值。我用我的网页jQuery的数据表。我没有语法错误,虽然,只是code失败,因为空数组。
主要code:
jQuery.each(数据,功能(I,D){
tblDataOpen.push({
门票:'<跨度ID =+ d.ticket_id +'级=票ID级>' + d.short_title +'< / SPAN>,
后code:d.post code,
状态:d.state,
顾客:d.customer_name
});的console.log(tblDataOpen); //< ===显示阵列预期 renderPartnerTicketsList(tblDataOpen,'开放');
功能:
函数renderPartnerTicketsList(tblData,ID){的console.log(tblData); //< ===显示了一个空对象 VAR tblHtml ='<表格的cellpadding =0CELLSPACING =0BORDER =0级=显示ID =+身份证+'>< THEAD>< TR>';
tblHtml + ='<第i门票和LT; /第i';
tblHtml + ='<第i个帖子code< /第i';
tblHtml + ='<第i个国家和LT; /第i';
tblHtml + ='<第i个客户和LT; /第i';
tblHtml + ='< / TR>< / THEAD><&TBODY GT;< / TBODY>< /表>'; 。jQuery的('#'+ ID +'-container')HTML(tblHtml); 的console.log(tblData); jQuery的('#'+ id)的.dataTable({
数据:tblData,
列: [
{数据:准考证},
{数据:后code},
{数据:国家},
{数据:客户}
]
pageLength:25,
命令:[3,'递减'],
scrollCollapse:假的,
jQueryUI的:真实,
回应:真
});
};
code是这样的:
的console.log(tblDataOpen); //< ===显示阵列预期 renderPartnerTicketsList(tblDataOpen,'开放');
工作。真。数以百万计的人使用它,每天数十亿次。
因此,这意味着问题必须在其他地方。要调试的问题,您需要设置一个断点在函数 renderPartnerTicketsList()
,然后浏览器将停止,看看调用堆栈。我的猜测是, renderPartnerTicketsList()
是从其他地方调用也是如此。
Oops, it seems I was mistaken. As @MDeSchaepmeester & @charlietfl point out it should work, and I see now it does. The problem is now it is not rendering the DataTable.
Any suggestions as to why it does not load into DataTables with no error are welcome!!
Here's all the code as requested:
function PartnerListSupportTickets() {
jQuery("#loader").show();
var idOpen = 'tickets-tbl-open';
var idOnHold = 'tickets-tbl-on-hold';
var idDeferred = 'tickets-tbl-deferred';
var idClosed = 'tickets-tbl-closed';
var tblDataOpen = [];
var tblDataOnHold = [];
var tblDataDeferred = [];
var tblDataClosed = [];
var tblEmptyMsg = [];
tblEmptyMsg.push({
ticket: 'No tickets available for this category',
postcode: '-',
state: '-',
customer: '-',
status: '-',
priority: '-',
fault: '-',
category: '-',
author: '-',
owner: '-',
age: '-',
lastupdate: '-'
});
var get_data_url = '/sma-php/gettickets.php?var=PARTCK&pid=' + sessionStorage.party_id;
jQuery.getJSON( get_data_url, function( data ) {
OpenNum = 0;
OnHoldNum = 0;
DeferredNum = 0;
ClosedNum = 0;
jQuery.each( data, function( i, d ) {
var status = d.status;
status = status.toLowerCase();
if(status == 'closed') {
var now = moment();
var then = moment(d.create_date);
d.days_age = now.diff(then, 'days');
};
switch(status) {
case('open'):
OpenNum = OpenNum + 1;
tblDataOpen.push({
ticket: '<span id="' + d.ticket_id + '" class="ticket-id-class">' + d.short_title + '</span>',
postcode: d.postcode,
state: d.state,
customer: d.customer_name,
status: d.status,
priority: '<font color="' + d.color + '">' + d.priority + '</font>',
fault: d.fault_type,
category: d.category,
author: d.author_name,
owner: d.owner_name,
age: d.days_age,
lastupdate: d.update_date
});
break;
case('on hold'):
OnHoldNum = OnHoldNum + 1;
tblDataOnHold.push({
ticket: '<span id="' + d.ticket_id + '" class="ticket-id-class">' + d.short_title + '</span>',
postcode: d.postcode,
state: d.state,
customer: d.customer_name,
status: d.status,
priority: '<font color="' + d.color + '">' + d.priority + '</font>',
fault: d.fault_type,
category: d.category,
author: d.author_name,
owner: d.owner_name,
age: d.days_age,
lastupdate: d.update_date
});
break;
case('deferred'):
DeferredNum = DeferredNum + 1;
tblDataDeferred.push({
ticket: '<span id="' + d.ticket_id + '" class="ticket-id-class">' + d.short_title + '</span>',
postcode: d.postcode,
state: d.state,
customer: d.customer_name,
status: d.status,
priority: '<font color="' + d.color + '">' + d.priority + '</font>',
fault: d.fault_type,
category: d.category,
author: d.author_name,
owner: d.owner_name,
age: d.days_age,
lastupdate: d.update_date
});
break;
case('closed'):
ClosedNum = ClosedNum + 1;
tblDataClosed.push({
ticket: '<span id="' + d.ticket_id + '" class="ticket-id-class">' + d.short_title + '</span>',
postcode: d.postcode,
state: d.state,
customer: d.customer_name,
status: d.status,
priority: '<font color="' + d.color + '">' + d.priority + '</font>',
fault: d.fault_type,
category: d.category,
author: d.author_name,
owner: d.owner_name,
age: d.days_age,
lastupdate: d.update_date
});
break;
default:
};
});
if(OpenNum > 0) {
renderPartnerTicketsList(tblDataOpen, idOpen, true);
} else {
renderPartnerTicketsList(tblEmptyMsg, idOpen, true);
};
if(OnHoldNum > 0) {
renderPartnerTicketsList(tblDataOnHold, idOnHold, true);
} else {
renderPartnerTicketsList(tblEmptyMsg, idOnHold, true);
};
if(DeferredNum > 0) {
renderPartnerTicketsList(tblDataDeferred, idDeferred, true);
} else {
renderPartnerTicketsList(tblEmptyMsg, idDeferred, true);
};
if(ClosedNum > 0) {
renderPartnerTicketsList(tblDataClosed, idClosed, true);
} else {
renderPartnerTicketsList(tblEmptyMsg, idClosed, true);
};
jQuery('a[href="#tab1c47048a"]').html('Open Tickets<span class="ticket-count">' + OpenNum + '</span>');
jQuery('a[href="#tab281c055c"]').html('On Hold Tickets<span class="ticket-count">' + OnHoldNum + '</span>');
jQuery('a[href="#tab328b0619"]').html('Deferred Tickets<span class="ticket-count">' + DeferredNum + '</span>');
jQuery('a[href="#tab26b60552"]').html('Closed Tickets<span class="ticket-count">' + ClosedNum + '</span>');
})
.done(function() {
jQuery("#loader").fadeOut("slow");
})
.fail(function(jqxhr, textStatus, error ) {
var sysError = textStatus + ", " + error;
showPopupMsg(errorClass, logoutFlag, "There was an error retrieving your Support Tickets List.<br/>Page wll not load from this point and you will be returned to our home page...<br/>Error: " + sysError);
});
};
function renderPartnerTicketsList(tblData, id, initLoad) {
// Set up data for DataTable plugin
if (!initLoad) {
var tbl = jQuery('#' + id).DataTable();
tbl.clear();
tbl.rows.add(tblData);
tbl.draw();
return;
};
// Bind DataTable plugin
var tblHtml = '<table cellpadding="0" cellspacing="0" border="0" class="display" id="' + id + '"><thead><tr>';
tblHtml += '<th>Ticket</th>';
tblHtml += '<th>Postcode</th>';
tblHtml += '<th>State</th>';
tblHtml += '<th>Customer</th>';
tblHtml += '<th>Status</th>';
tblHtml += '<th>Priority</th>';
tblHtml += '<th>Fault</th>';
tblHtml += '<th>Category</th>';
tblHtml += '<th>Author</th>';
tblHtml += '<th>Owner</th>';
tblHtml += '<th>Age</th>';
tblHtml += '<th>Last Update</th>';
tblHtml += '</tr></thead><tbody></tbody></table>';
jQuery('#' + id + '-container').html(tblHtml);
jQuery('#' + id).dataTable({
data: tblData,
columns: [
{ data: "ticket" },
{ data: "postcode" },
{ data: "state" },
{ data: "customer" },
{ data: "status" },
{ data: "priority" },
{ data: "fault" },
{ data: "category" },
{ data: "author" },
{ data: "owner" },
{ data: "age" },
{ data: "lastupdate" }
],
pageLength: 25,
order: [12, 'desc'],
"scrollCollapse": false,
"jQueryUI": true,
responsive: true
});
};
I am losing the values inside of the array in the code below once I pass it to a function. I am using jQuery DataTables in my page. I have no syntax error though, just the code fails due to empty array.
MAIN CODE:
jQuery.each( data, function( i, d ) {
tblDataOpen.push({
ticket: '<span id="' + d.ticket_id + '" class="ticket-id-class">' + d.short_title + '</span>',
postcode: d.postcode,
state: d.state,
customer: d.customer_name
});
console.log(tblDataOpen); // <=== SHOWS THE ARRAY AS EXPECTED
renderPartnerTicketsList(tblDataOpen, 'open');
FUNCTION:
function renderPartnerTicketsList(tblData, id) {
console.log(tblData); // <=== SHOWS AN EMPTY OBJECT
var tblHtml = '<table cellpadding="0" cellspacing="0" border="0" class="display" id="' + id + '"><thead><tr>';
tblHtml += '<th>Ticket</th>';
tblHtml += '<th>Postcode</th>';
tblHtml += '<th>State</th>';
tblHtml += '<th>Customer</th>';
tblHtml += '</tr></thead><tbody></tbody></table>';
jQuery('#' + id + '-container').html(tblHtml);
console.log(tblData);
jQuery('#' + id).dataTable({
data: tblData,
columns: [
{ data: "ticket" },
{ data: "postcode" },
{ data: "state" },
{ data: "customer" }
],
pageLength: 25,
order: [3, 'desc'],
"scrollCollapse": false,
"jQueryUI": true,
responsive: true
});
};
Code like this:
console.log(tblDataOpen); // <=== SHOWS THE ARRAY AS EXPECTED
renderPartnerTicketsList(tblDataOpen, 'open');
works. Really. Millions of people use it every day billions of times.
So that means the problem must be elsewhere. To debug the issue, you need to set a breakpoint in the function renderPartnerTicketsList()
and then the browser stops, look at the call stack. My guess is that renderPartnerTicketsList()
is called from other places as well.
这篇关于传递给函数时,JavaScript数组内容丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!