3.0.5

switch statement in setDataProperty cause wrong sorting result

in the below function, if I use if statement, it will have no problem, both the value property and sorting works fine. But once I change to switch statement, the value property is also set correctly, but when I sort the column, the items are not in order. Any ideas?

obj.setDataProperty("value", function(i,j){ 
    var strTemp = myData[i][j];
    var strResult = strTemp;
/*
    if (j==1) {
        //	string
        if (strTemp == '-') strResult = maxNegStr;
    } else if (j==17) {
        //	string date
        if (strTemp == '-') strResult = maxNeg
        else {
            var arrDate = strTemp.split("-");
            var exDivDate = new Date( arrDate[2], arrDate[1] - 1, arrDate[0] );
            var now = new Date();
            strResult = Math.round((exDivDate.getTime() - now.getTime()) / 86400) / 1000;
        }	
    } else if (j==18) {
        //	number << better
        if (strTemp == '-') strResult = maxPos;
    } else if (j==21 || j==22) {
        //	number with suffix
        if (strTemp == '-') strResult = maxNeg
        else {
            switch (strTemp.substring(strTemp.length-1, strTemp.length)) {
                case 'k':
                    strResult = strTemp.slice(0, -1) * 1000;
                    break;
                case 'M':
                    strResult = strTemp.slice(0, -1) * 1000000;
                    break;
                case 'G':
                    strResult = strTemp.slice(0, -1) * 1000000000;
                    break;
                case 'T':
                    strResult = strTemp.slice(0, -1) * 1000000000000;
                    break;
                default:
                    strResult = strTemp.slice(0, -1);
            }
        }
    } else {
        //	number
        if (strTemp == '-') strResult = maxNeg;
        var text = "" + this.getDataText(i, j);
        if (text.match(/^\s*$/)) strResult = maxNeg;
        var value = Number(text.replace(/[ ,%\$]/gi, "").replace(/\((.*)\)/, "-$1"));
        if (isNaN(value)) {
            strResult = maxNeg;
        } else {
            strResult = value;
        }
    }
*/
switch (j) {
    case 1:
    case 16:
    case 23:
    case 24:
        //	string
        if (strTemp == '-') strResult = maxNegStr;
        break;
    case 17:
        //	string date
        if (strTemp == '-') strResult = maxNeg
        else {
            var arrDate = strTemp.split("-");
            var exDivDate = new Date( arrDate[2], arrDate[1] - 1, arrDate[0] );
            var now = new Date();
            strResult = Math.round((exDivDate.getTime() - now.getTime()) / 86400) / 1000;
        }
        break;
    case 18:
        //	number << better
        if (strTemp == '-') strResult = maxPos;
        break;
    case 21:
    case 22:
        //	number with suffix
        if (strTemp == '-') strResult = maxNeg
        else {
            switch (strTemp.substring(strTemp.length-1, strTemp.length)) {
                case 'k':
                    strResult = strTemp.slice(0, -1) * 1000;
                    break;
                case 'M':
                    strResult = strTemp.slice(0, -1) * 1000000;
                    break;
                case 'G':
                    strResult = strTemp.slice(0, -1) * 1000000000;
                    break;
                case 'T':
                    strResult = strTemp.slice(0, -1) * 1000000000000;
                    break;
                default:
                    strResult = strTemp.slice(0, -1);
            }
        }
        break;
    default:
        //	number
        if (strTemp == '-') strResult = maxNeg;
        var text = "" + this.getDataText(i, j);
        if (text.match(/^\s*$/)) strResult = "";
        var value = Number(text.replace(/[ ,%\$]/gi, "").replace(/\((.*)\)/, "-$1"));
        var value = Number(text);
        if (isNaN(value)) {
            strResult = maxNeg;
        } else {
            strResult = value;
        }
}

    return formats[j].dataToValue(strResult);
});
Henry Ng
September 13,
please ignore the previous coding, here is the updated one, but still have the problem.
obj.setDataProperty("value", function(i,j){ 
    var strTemp = myData[i][j];
    var strResult = strTemp;
/*
    if (j==1 || j==16 || j==23) {
        //	string
        if (strTemp == '-') strResult = maxNegStr;
    } else if (j==17) {
        //	string date
        if (strTemp == '-') strResult = maxNeg
        else {
            var arrDate = strTemp.split("-");
            var exDivDate = new Date( arrDate[2], arrDate[1] - 1, arrDate[0] );
            var now = new Date();
            strResult = Math.round((exDivDate.getTime() - now.getTime()) / 86400) / 1000;
        }	
    } else if (j==18) {
        //	number << better
        if (strTemp == '-') strResult = maxPos;
    } else if (j==21 || j==22) {
        //	number with suffix
        if (strTemp == '-') strResult = maxNeg
        else {
            switch (strTemp.substring(strTemp.length-1, strTemp.length)) {
                case 'k':
                    strResult = strTemp.slice(0, -1) * 1000;
                    break;
                case 'M':
                    strResult = strTemp.slice(0, -1) * 1000000;
                    break;
                case 'G':
                    strResult = strTemp.slice(0, -1) * 1000000000;
                    break;
                case 'T':
                    strResult = strTemp.slice(0, -1) * 1000000000000;
                    break;
                default:
                    strResult = strTemp.slice(0, -1);
            }
        }
    } else {
        //	number
        if (strTemp == '-') strResult = maxNeg;
        var text = "" + this.getDataText(i, j);
        if (text.match(/^\s*$/)) strResult = maxNeg;
        var value = Number(text.replace(/[ ,%\$]/gi, "").replace(/\((.*)\)/, "-$1"));
        if (isNaN(value)) {
            strResult = maxNeg;
        } else {
            strResult = value;
        }
    }
*/
switch (j) {
    case 1:
    case 16:
    case 23:
        //	string
        if (strTemp == '-') strResult = maxNegStr;
        break;
    case 17:
        //	string date
        if (strTemp == '-') strResult = maxNeg
        else {
            var arrDate = strTemp.split("-");
            var exDivDate = new Date( arrDate[2], arrDate[1] - 1, arrDate[0] );
            var now = new Date();
            strResult = Math.round((exDivDate.getTime() - now.getTime()) / 86400) / 1000;
        }
        break;
    case 18:
        //	number << better
        if (strTemp == '-') strResult = maxPos;
        break;
    case 21:
    case 22:
        //	number with suffix
        if (strTemp == '-') strResult = maxNeg
        else {
            switch (strTemp.substring(strTemp.length-1, strTemp.length)) {
                case 'k':
                    strResult = strTemp.slice(0, -1) * 1000;
                    break;
                case 'M':
                    strResult = strTemp.slice(0, -1) * 1000000;
                    break;
                case 'G':
                    strResult = strTemp.slice(0, -1) * 1000000000;
                    break;
                case 'T':
                    strResult = strTemp.slice(0, -1) * 1000000000000;
                    break;
                default:
                    strResult = strTemp.slice(0, -1);
            }
        }
        break;
    default:
        //	number
        if (strTemp == '-') strResult = maxNeg;
        var text = "" + this.getDataText(i, j);
        if (text.match(/^\s*$/)) strResult = "";
        var value = Number(text.replace(/[ ,%\$]/gi, "").replace(/\((.*)\)/, "-$1"));
        if (isNaN(value)) {
            strResult = maxNeg;
        } else {
            strResult = value;
        }
}

    return formats[j].dataToValue(strResult);
});
Henry Ng
September 13,

This topic is archived.

See also:


Back to support forum