1

I have a ColdFusion method getData() which returns a query object as follows:

CustomerCode  ServiceCode  SubscriberCode  Status    UserName
-------------------------------------------------------------
811101        8            gertjan         OPEN      [email protected]
811101        8            gertjan         CLOSING   [email protected]
811101        2            99652444        CLOSED    [email protected]
811101        2            99655000        OPEN      [email protected]

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.

4
  • Can you post the code where you're outputting the html for each row? Or at least what the html looks like. Also, this seems like logic more suited for backend. But if that is ruled out, and you can only provide a solution front-end (javascript), let us know. Commented Feb 29, 2012 at 0:28
  • This seems like it would be easier to fix on the back end - adjust your query so that it only gives you the OPEN record if there are two records in the same set. Commented Feb 29, 2012 at 0:31
  • I've added the HTML. Yes I understand it would be easier at the back end. But, the data comes ultimately from a web service that is out of my control to change. Commented Feb 29, 2012 at 0:33
  • post some real json, in fiddle.net would be great Commented Feb 29, 2012 at 0:47

1 Answer 1

3

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:

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.');
    }
}

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, and ServiceCode, but your example only uses SubscribercCode. If this is really the case, it is much simpler:

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.');
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jeff, I get hash[SubscriberCode] is undefined, using your first option on the line: if( hash[SubscriberCode].status == 'OPEN'
Oh, you need to check if it is defined first. hash[SubscriberCode] != undefined && hash[SubscriberCode].status == 'OPEN' See fix. That's what I get for answering with untested code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.