getJson后如何携带数据
how to carry data after getJson
我在 getJson 之后在变量数据中携带数据时遇到问题.. 下面是我的代码
function format ( d ) {
var id = d[1],$base = '<?php echo Routed::url('/api/course_fees_setup/getSponsorList?'); ?>';
var rows = '';
$.getJSON($base+'id=' + id,function(data){
var index =0;
var newOptions = data.data;
var SponsorbyFee = newOptions.Sponsorship[0].SponsorbyFee;
$.each(SponsorbyFee, function(value,key) {
// console.log(key);
switch(key['fee_type_id']){
case '1' :
// console.log('program');
break;
case '2' :
// console.log(key['OthersFee']);
rows[index] = '<tr><td>' + key['OthersFee']['payment_description'] + '</td><td>' + key['OthersFee']['price'] + '</td><td>' + key['OthersFee']['remarks'] + '</td><tr>';
index++;
break;
case '3' :
// console.log(key['DiscountFee']);
rows[index] = '<tr><td>' + key['DiscountFee']['payment_description'] + '</td><td>' + key['DiscountFee']['price'] + '</td><td>' + key['DiscountFee']['remarks'] + '</td><tr>';
index++;
break;
}
});
});
return '<br /><br /><table id="sponsorlist-tbl" class="hover cell-border" width="100%">'+
'<thead>'+
'<tr>'+
'<th width="30%">Payment Description</th>'+
'<th width="10%">Price</th>'+
'<th width="30%">Remarks</th>'+
'</tr>'+
'</thead>'+
'<tbody>'+
rows +
'</tbody>'+
'<table><br /><br /><br />';
}
我想在第 36 行 return 变量 data 一旦我在第 16 行完成循环我的行。但看起来像 return null.Can 有人帮助我更正了我的代码,因为我刚刚研究了 jquery 中的生成 table。任何帮助将不胜感激。谢谢大家
$.getJSON()
是 returns 的异步结果。您可以 return html
字符串和 data
从 $.getJSON()
内调用 $.each()
;使用 .then()
处理结果
function format ( d ) {
var id = d[1],$base = '<?php echo Routed::url('/api/course_fees_setup/getSponsorList?'); ?>';
var rows = '';
return $.getJSON($base+'id=' + id)
.then(function(data){
var index =0;
var newOptions = data.data;
var SponsorbyFee = newOptions.Sponsorship[0].SponsorbyFee;
$.each(SponsorbyFee, function(value,key) {
// console.log(key);
switch(key['fee_type_id']){
case '1' :
// console.log('program');
break;
case '2' :
// console.log(key['OthersFee']);
rows[index] = '<tr><td>' + key['OthersFee']['payment_description'] + '</td><td>' + key['OthersFee']['price'] + '</td><td>' + key['OthersFee']['remarks'] + '</td><tr>';
index++;
break;
case '3' :
// console.log(key['DiscountFee']);
rows[index] = '<tr><td>' + key['DiscountFee']['payment_description'] + '</td><td>' + key['DiscountFee']['price'] + '</td><td>' + key['DiscountFee']['remarks'] + '</td><tr>';
index++;
break;
}
});
return ['<br /><br /><table id="sponsorlist-tbl" class="hover cell-border" width="100%">'+
'<thead>'+
'<tr>'+
'<th width="30%">Payment Description</th>'+
'<th width="10%">Price</th>'+
'<th width="30%">Remarks</th>'+
'</tr>'+
'</thead>'+
'<tbody>'+
rows +
'</tbody>'+
'<table><br /><br /><br />'
, data];
});
}
format(/* parameter */).then(function(results) {
console.log(results)
})
我在 getJson 之后在变量数据中携带数据时遇到问题.. 下面是我的代码
function format ( d ) {
var id = d[1],$base = '<?php echo Routed::url('/api/course_fees_setup/getSponsorList?'); ?>';
var rows = '';
$.getJSON($base+'id=' + id,function(data){
var index =0;
var newOptions = data.data;
var SponsorbyFee = newOptions.Sponsorship[0].SponsorbyFee;
$.each(SponsorbyFee, function(value,key) {
// console.log(key);
switch(key['fee_type_id']){
case '1' :
// console.log('program');
break;
case '2' :
// console.log(key['OthersFee']);
rows[index] = '<tr><td>' + key['OthersFee']['payment_description'] + '</td><td>' + key['OthersFee']['price'] + '</td><td>' + key['OthersFee']['remarks'] + '</td><tr>';
index++;
break;
case '3' :
// console.log(key['DiscountFee']);
rows[index] = '<tr><td>' + key['DiscountFee']['payment_description'] + '</td><td>' + key['DiscountFee']['price'] + '</td><td>' + key['DiscountFee']['remarks'] + '</td><tr>';
index++;
break;
}
});
});
return '<br /><br /><table id="sponsorlist-tbl" class="hover cell-border" width="100%">'+
'<thead>'+
'<tr>'+
'<th width="30%">Payment Description</th>'+
'<th width="10%">Price</th>'+
'<th width="30%">Remarks</th>'+
'</tr>'+
'</thead>'+
'<tbody>'+
rows +
'</tbody>'+
'<table><br /><br /><br />';
}
我想在第 36 行 return 变量 data 一旦我在第 16 行完成循环我的行。但看起来像 return null.Can 有人帮助我更正了我的代码,因为我刚刚研究了 jquery 中的生成 table。任何帮助将不胜感激。谢谢大家
$.getJSON()
是 returns 的异步结果。您可以 return html
字符串和 data
从 $.getJSON()
内调用 $.each()
;使用 .then()
处理结果
function format ( d ) {
var id = d[1],$base = '<?php echo Routed::url('/api/course_fees_setup/getSponsorList?'); ?>';
var rows = '';
return $.getJSON($base+'id=' + id)
.then(function(data){
var index =0;
var newOptions = data.data;
var SponsorbyFee = newOptions.Sponsorship[0].SponsorbyFee;
$.each(SponsorbyFee, function(value,key) {
// console.log(key);
switch(key['fee_type_id']){
case '1' :
// console.log('program');
break;
case '2' :
// console.log(key['OthersFee']);
rows[index] = '<tr><td>' + key['OthersFee']['payment_description'] + '</td><td>' + key['OthersFee']['price'] + '</td><td>' + key['OthersFee']['remarks'] + '</td><tr>';
index++;
break;
case '3' :
// console.log(key['DiscountFee']);
rows[index] = '<tr><td>' + key['DiscountFee']['payment_description'] + '</td><td>' + key['DiscountFee']['price'] + '</td><td>' + key['DiscountFee']['remarks'] + '</td><tr>';
index++;
break;
}
});
return ['<br /><br /><table id="sponsorlist-tbl" class="hover cell-border" width="100%">'+
'<thead>'+
'<tr>'+
'<th width="30%">Payment Description</th>'+
'<th width="10%">Price</th>'+
'<th width="30%">Remarks</th>'+
'</tr>'+
'</thead>'+
'<tbody>'+
rows +
'</tbody>'+
'<table><br /><br /><br />'
, data];
});
}
format(/* parameter */).then(function(results) {
console.log(results)
})