I have a ColdFusion method getData() which returns a query object as follows:
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
Note the first two rows – exactly the same except for Status OPEN and CLOSING respectively.
The following function creates a new select option for each row where ServiceCode=8 and Status is either OPEN or CLOSING, which would be the case for both the first two rows.
The data ultimately comes via a web service which is out of my control to change. I need to change the jQuery such that if BOTH an OPEN and CLOSING record exists for the same ServiceCode/SubscriberCode combination, which is the case for the first two rows, then only create an option for the OPEN record.
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.');
}
});
}
HTML
<select name="selInternet" id="selInternet" style="width:120px">
</select>
Any assistance greatly appreciated in getting the cleanest approach to this, without multiple loops of the same dataset, for example.
You would need to keep a hash as you read the data, ignoring data if an ‘OPEN’ was already found. Then loop through the hash items and output the data:
I’m not sure what your cases are, but this covers your description I believe. I realize it is silly to have the hash key stored in the data, but for demonstration, this is how you would retrieve other data. This code stores
Status,SubscriberCode, andServiceCode, but your example only usesSubscribercCode. If this is really the case, it is much simpler: