Getting a summary count in a view export?

前端 未结 2 1513
甜味超标
甜味超标 2021-01-17 01:26

I need to export a view to excel. I have already found some code somewhere and it is working great in xpages. Now the user wants to add a summary of totals. I will expla

相关标签:
2条回答
  • 2021-01-17 01:56

    Bruce, make your live easier and use a ViewNavigator. The ViewNavigator has entries for the categories and when you set a value to be summed up that value is in the view navigator. It also has methods to jump from category to category, so you have less data to read. For example I use this function to get summary data from a categorized view (one or two levels):

    function getCategoryData(viewName, dataColumn, getChildren) {
        var v = database.getView(viewName);
        var nav = v.createViewNav();
        var ve = nav.getFirst();
        var isFirst = true;
        var result = "[";
            while (ve) {
        if (!ve.isTotal()) {
            var curData = ve.getColumnValues();
            if (!isFirst) {
                result += ",";
            }
            result += "{label : \"";
            result += curData[0];
            result += "\", value : ";
            result += curData[dataColumn];
            /* for 2 level categories we fetch additional data */
            if (getChildren) {
                var childve = nav.getChild();
                var firstChild = true;
                result += ", children : [";
                while (childve) {
                    var childData = childve.getColumnValues();
                    if (!firstChild) {
                        result += ",";
                    }
                    result += "{label : \"";
                    result += childData[1];
                    result += "\", value : ";
                    result += childData[dataColumn];
                    result += "}";          
                    firstChild = false;
                    childve = nav.getNextSibling(childve);
                }
                result += "]"
            }
            result += "}";          
            isFirst = false;
        }       
        ve = nav.getNextSibling(ve);
    }
    result += "]";
    return result;
     }
    

    Note: it returns a JSON String, not a JSON object. I needed the string, so you might want to alter that. You could easily amend the function to first select a category based subset:

    function getCumulativeCategoryData(viewName, key, dataColumn, fetchChildren) {
    var v = database.getView(viewName);
    
    var nav = v.createViewNavFromCategory(key);
    var ve = nav.getFirst();
        ...
     }
    

    Where key would be a String or a Vector. You need to be a little bit careful with data types. If a category in the view is a number make sure you use Number() for your key otherwise it won't match.

    0 讨论(0)
  • 2021-01-17 02:02

    You are on the right track in your last paragraph but you don't need to store the values in a session scope. A standard javascript object will do. Assuming you are generating your report by looping through the documents in the view, the quickest and easiest solution in my mind would be to count each equipment id as you loop through the documents and store the counts in an object:

    equipId[equipId1] equipId[equipId2] . . equipId[equipIdx]

    Then after you have looped through all your docs, loop through your equipment ID object values as you have in your sample report above.

    Looping through javascript objects: How do I loop through or enumerate a JavaScript object?

    Sorting a javascript object: Sorting JavaScript Object by property value

    0 讨论(0)
提交回复
热议问题