Getting a summary count in a view export?

前端 未结 2 1510
甜味超标
甜味超标 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.

提交回复
热议问题