Calculating Totals

Comments

1 comment

  • Rainer Grabowski

    Hi Stefano,

    Sorry to say that, but your calculation doesn't make any sense. What's the purpose of your vars value (1-4)?
    a) FYI: ".length" always returns a number. So if @All of Programm pulls 80 items  @All of Program.length returns 80
    b) === compares values. So you compare e.g. 80 with "Lease".
    c) So you don't get a real value for all of your vars value 1-4.
    d) If you want to check if a specific category (e.g. Lease) is selected in a multi-select category field you can't use "if equal to" (category[i] == "Lease") because there can be 2 options selected, but you need "it contains" (category[i].indexOf("Lease) > -1)
    e) So you for() loop doesn't work because value (1-4) must be a number, e.g. @all of program.length
    f) You have 4 column headers "Attribute | Condition | Amount | Costs" but only 2 columns --- | ---

    Maybe this code helps you: 

    var i,j;
    var conds = @All of Interior Condition;
    var prog = @All of Program;
    var progs = [];
    for(i = 0; i < prog.length;i ++){
        progs.push(prog[i]);
    }
    var progs = progs.join().split(",");
    var programs = progs.filter(function(item, pos) {
    return progs.indexOf(item) == pos;
    });

    var progRows = [];
    for(i = 0; i < programs.length; i++){
        progSum = 0;
        for(j = 0; j < prog.length; j++){
            if(programs[i].indexOf(prog[j]) > -1  && conds[j].indexOf("Major Repairs") > -1){
               progSum += 1
            }
        }
        progRows.push(programs[i] + " | " + progSum +  " | | " );
    }

    var header = "Attribute | Condition | Amount | Costs\n"
    var colums = "--- | --- | --- | --- \n";
    var table = header + columns + progRows.join("\n");
    table

    The table will show the program names in the first column (under "Attribute") and in the second column (under "Condition") the number of items where that program and "Major Repairs" is selected. The other 2 columns ("Amount" and "Costs") will be empty because you don't pull the values with @All of field name. 

    You wrote: Originally, "Program Type" category was a (5) option multi-select text category.
    That means for me that in one item multiple programms can be selected. Correct? 

    This part:

    var progs = [];
    for(i = 0; i < prog.length;i ++){
        progs.push(prog[i]);
    }
    var progs = progs.join().split(",");
    var programs = progs.filter(function(item, pos) {
    return progs.indexOf(item) == pos;
    });

    collects first all selected programs from all items and then (in var programms) removes all duplicates so that you get an array of unique program names: maximum 5, but can be less if one program isn't selected in any item. Means: In the table you won't get that program listed, only programs with minumum one selection in the category "Program" will be listed. The advantage of this approach is that you don't need to change the calculation when you change the program names in the category field or if you add/remove one. But if you want all programs to be listed you can replace that part with 

    var programs = ["Facility Survey","ADPI Survey","Lease","F&P Project","Special Request"]

    Rainer

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk