简体   繁体   中英

Sum of Sharepoint list items using Rest API

I want to get the total sum of one field from my sharepoint list using REST API ..am using the following code but my output is like 501001005045

I don't know Where am Do the Mistake

Here is my code

function GetSumApi(projectTitle) {
$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('TaskList')/items?$select=TaskPercentage&$filter=Projects eq '" + projectTitle + "'",
    type: "GET",
    async: false,
    headers: {
        "Accept": "application/json;odata=verbose",
    },
    success: function (data) {
        sumfield = data.d.results;
        var sum = 0;
        for (var i = 0; i < sumfield.length; i++) {
            var sum = sum + sumfield[i].TaskPercentage;
        }
        console.log("Total sum of Tasks", sum);
    },
    error: function (error) {
        console.log(JSON.stringify(error));
    }
});

}

In the above code am using list name as TaskList and my field is taskpercentage and the project title is the project name from the user.

Use parseInt to convert the value to int.

For example:

var sum = 0;
for (var i = 0; i < sumfield.length; i++) {
        var sum = sum + parseInt(sumfield[i].TaskPercentage);//parseInt(sumfield[i].TaskPercentage)/100
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM