我有一个ColdFusion方法getData(),它返回一个查询对象,如下所示:
CustomerCode ServiceCode SubscriberCode Status UserName
-------------------------------------------------------------
811101 8 gertjan OPEN gertjan@blah.net
811101 8 gertjan CLOSING gertjan@blah.net
811101 2 99652444 CLOSED gertjan@blah.net
811101 2 99655000 OPEN gertjan@blah.net
注意前两行 - 除了Status OPEN和CLOSING之外完全相同。
以下函数为ServiceCode = 8且Status为OPEN或CLOSING的每一行创建一个新的select选项,前两行都是这种情况。
数据最终来自我无法控制的网络服务。我需要更改jQuery,如果同一个ServiceCode / SubscriberCode组合存在OPEN和CLOSING记录,前两行就是这种情况,那么只为OPEN记录创建一个选项。
function getInternetLines(){
var CustomerCode=global_customerCode;
var SessionID=global_sessionID;
var lines=[];
$.getJSON("/system.cfc?method=getData&returnformat=json&queryformat=column",
{"SessionID":SessionID,"CustomerCode":CustomerCode},
function(res,code) {
if(res.ROWCOUNT > 0){
for(var i=0; i<res.ROWCOUNT; i++) {
var ServiceCode = res.DATA.ServiceCode[i];
var SubscriberCode = res.DATA.SubscriberCode[i];
var Status = res.DATA.Status[i];
if(ServiceCode == 8 && (Status == 'OPEN' || Status == 'CLOSING')){
lines.push(SubscriberCode);
$('#selInternet').append(
$('<option></option>').val(SubscriberCode).html(SubscriberCode)
);
}
}
global_internet_lines = lines;
if(lines.length == 0){
$('#divBroadbandUsage').html('No Active Broadband Connections.');
}
}else{
$('#divBroadbandUsage').html('No Active Broadband Connections.');
}
});
}
<select name="selInternet" id="selInternet" style="width:120px">
</select>
如果没有相同数据集的多个循环,那么在获得最干净的方法时,任何帮助都会非常受欢迎。
答案 0 :(得分:3)
您需要在读取数据时保留哈希值,如果已找到“打开”,则忽略数据。然后遍历哈希项并输出数据:
if(res.ROWCOUNT > 0){
var hash = {}; // Hash to store data
for(var i=0; i<res.ROWCOUNT; i++) {
var ServiceCode = res.DATA.ServiceCode[i];
var SubscriberCode = res.DATA.SubscriberCode[i];
var Status = res.DATA.Status[i];
if(ServiceCode == 8 && (Status == 'OPEN' || Status == 'CLOSING')){
if( hash[SubscriberCode] != undefined && hash[SubscriberCode].status == 'OPEN' ) {
// If we already have OPEN, don't load the data
continue;
} else {
// Else override whatever data you have for this SubscriberCode
hash[SubscriberCode] = { status: Status, subscriber: SubscriberCode, service: ServiceCode };
}
}
}
// loop through the hash and output the options
for(var x in hash) {
lines.push(hash[x].subscriber);
$('#selInternet').append(
$('<option></option>').val(hash[x].subscriber).html(hash[x].subscriber)
);
}
global_internet_lines = lines;
if(lines.length == 0){
$('#divBroadbandUsage').html('No Active Broadband Connections.');
}
}
我不确定你的案件是什么,但这包括我相信你的描述。我意识到将哈希密钥存储在数据中是很愚蠢的,但为了演示,这就是你如何检索其他数据。此代码存储Status
,SubscriberCode
和ServiceCode
,但您的示例仅使用SubscribercCode
。如果确实如此,那就更简单了:
if(res.ROWCOUNT > 0){
var hash = {}; // Hash to store data
for(var i=0; i<res.ROWCOUNT; i++) {
var ServiceCode = res.DATA.ServiceCode[i];
var SubscriberCode = res.DATA.SubscriberCode[i];
var Status = res.DATA.Status[i];
if(ServiceCode == 8 && (Status == 'OPEN' || Status == 'CLOSING')){
// If we see the subscriber code, add it to our hash
hash[SubscriberCode] = 1;
}
}
// loop through the hash and output the options
for(var sub in hash) {
lines.push(sub);
$('#selInternet').append(
$('<option></option>').val(sub).html(sub)
);
}
global_internet_lines = lines;
if(lines.length == 0){
$('#divBroadbandUsage').html('No Active Broadband Connections.');
}
}