DGRID - give numeric value for multiple Characters in field

77 views Asked by At

6.30.15 - HOW CAN I MAKE THIS QUESTION BETTER AND MORE HELPFUL TO OTHERS? FEEDBACK WOULD BE HELPFUL. THANKS!

I'm still rather new to Dgrid, Dojo, Javascript - really all of it. In a dgrid cell - there is a value '----------------!!!-' Or variations of the same format. There might be 1 exclamation point or multiple - and in multiple combinations.

I need to get the count of each exclamation point starting FROM THE LEFT. For instance - in my example above - those are items 2,3, and 4. I need 2,3 and 4 to display in the dgrid cell grid - not the string above.

Does anyone have any clue how to do this?? I can use a formatter - but this becomes tricky when I have multiple items in the string and different combinations. Any advice or leads in the right direction would be helpful.!

UPDATE with code example: 6.16.15

This works great as long as there is only one exclamation mark. It's the multiple combinations. I'm learning javascript on the fly so part of problem is not knowing exactly how to ask the right questions when researching for an answer.

function XXXXInfoFormatter(item){
                //console.log(item,typeof(item));
                var newItem;
                if ( item == '--------------------' ) 
                    newItem = 'No Faults'
                else if ( item == '-------------------!' )
                    newItem = 'XXXX 1'
                else if ( item == '------------------!-' )
                    newItem = 'XXXX 2'
                else if ( item == '-----------------!--' )
                    newItem = 'XXXX 3'
                else if ( item == '----------------!---' )
                    newItem = 'XXXX 4'
                else if ( item == '---------------!----' )
                    newItem = 'XXXX 5'
                else if ( item == '--------------!-----' )
                    newItem = 'XXXX 6'
                else if ( item == '-------------!------' )
                    newItem = 'XXXX 7'
                else if ( item == '------------!-------' )
                    newItem = 'XXXX 8'
                else if ( item == '-----------!--------' )
                    newItem = 'XXXX 9'
                else if ( item == '----------!---------' )
                    newItem = 'XXXX 10'
                else if ( item == '---------!----------' )
                    newItem = 'XXXX 11'
                else if ( item == '--------!-----------' )
                    newItem = 'XXXX 12'
                else if ( item == '-------!------------' )
                    newItem = 'XXXX 13'
                else if ( item == '------!-------------' )
                    newItem = 'XXXX 14'
                else if ( item == '-----!--------------' )
                    newItem = 'XXXX 15'
                else if ( item == '----!---------------' )
                    newItem = 'XXXX 16'
                else if ( item == '---!----------------' )
                    newItem = 'XXXX 17'
                else if ( item == '--!-----------------' )
                    newItem = 'XXXX 18'
                else if ( item == '-!------------------' )
                    newItem = 'Head 19'
                else if ( item == '!-------------------' )
                    newItem = 'XXXX 20'
                else if ( item == '!!!!!!!!!!!!!!!!!!!!' )
                    newItem = 'All Fault'



         return newItem;

    }
1

There are 1 answers

7
fuyushimoya On BEST ANSWER

Altered.

function XXXXInfoFormatter(item) {
    var length = item.length;
    var i;
    var Wrongs = [];

    // Scan chars from right to left.
    for (i = length - 1; i >= 0; --i) {
        if ('!' === item[i]) {
            // If its a '!', put it id from Right to the Wrong list.
            Wrongs.push(length - i);
        }
    }
    // Check if the item is no faults or all faults.
    var wlength = Wrongs.length;
    if (wlength === 0) {
        return 'No Faults';
    } else if (wlength === length) {
        return 'All Faults';
    }

    // Form a Result str from wrong list.
    var str = 'XXXX in: ';

    for (i = 0; i < wlength; ++i) {
        str += ' ' + Wrongs[i] ;
    }
    return str;
}

Hope this is what you need.