0

I've got a SharePoint site that I'm running some Rest API queries on and filtering results. I was just wondering if anyone had a quick tip on if its possible to pull a count from the following code snippit from a datatable

rowCallback: function(row, data){
if (data["RegistrationDate"] <= expiration.toISOString()){
    alert (the number of items that come back from this filter?);
    $('td:eq(6)', row).css('background-color', 'red');
}
else{
}
1

Without seeing the rest of your code, based on just that little snippet there, could you not initialize a counter variable somewhere outside of that function, and then inside that if statement (where you have your alert line), increment the counter?

There's probably a better way to do it wherever you get your REST responses back, but without seeing the rest of your code it's hard to say.


To address your comment - you're saying you're looking for a way "to filter the count inside the if statement", but I think that's the wrong way to look at it. The if statement in your code snippet is inside the DataTables rowCallback function, which fires once for every row. So at that point in the code, the data object really only represents one SharePoint list item (I'm assuming you are retrieving list items through a REST call to SharePoint and then passing those results to DataTables). So you can't really "filter" per se at that point in code, because you don't have access to all the list items, you only have access to the one list item that's being put in that particular row. That's why I suggested a counter, because the rowCallback function gets fired once for every item. So it would be something like this:

// before you create the table, initialize a counter variable
var counter = 0;

// create the table, and the rowCallback will get fired once for every list item
$('#example').dataTable({
    "rowCallback": function (row, data) {
        if (data["RegistrationDate"] <= expiration.toISOString()) {
            $('td:eq(6)', row).css('background-color', 'red');

            // increment the count by one
            counter++;
        }
        else {
        }
    }
});

The better way to do it would be to filter the results you get back from SharePoint before you create the table. I can give you an example, but you will have to interpret how to work that into the existing code you have, without seeing your other code I can't really structure my example in a way that would naturally fit what you already have. But see if this sparks any ideas:

$.ajax({
    url: your-url,
    method: 'GET',
    headers: {
        "accept": "application/json;odata=verbose"
    }
}).done(function(data) {
    // now you have your results
    var filteredResults = data.d.results.filter(function(item) {
        return item["RegistrationDate"] <= expiration.toISOString();
    });

    // now you have your count, before you make your table
    var count = filteredResults.length;

    // go ahead and make your table
    $('#example').dataTable({
        "rowCallback": function (row, data) {
            if (data["RegistrationDate"] <= expiration.toISOString()) {
                $('td:eq(6)', row).css('background-color', 'red');
            }
            else {
            }
        }
    });
});
  • I suppose I could run a completely separate ajax query and build the count there, That was one of the solutions I ran with but it makes for ugly code, i was just wondering if there was a quick easy way to filter the count inside the if statement and pass that variable up to my table. Not a big deal, was just wondering if there was cleaner coding. Sorry for the lack of code, I was hoping what i posted was sufficient to get the point across. The network I'm writing all this on is a closed network and its hard to pass it back and forth, not as easy as a copy/paste to post the full code. – Chuck.M Aug 16 '18 at 13:29
  • @Chuck.M updated my answer. – Dylan Cristy Aug 16 '18 at 14:40
  • That did it. When you mentioned running it outside the function i didn't know about the .filter function within the call.done. I was thinking run another ajax query with the filter built into Rest and then working with that count and that got really ugly. I really appreciate you Dylan, you've helped me on a couple of these. I really don't know much about what I'm doing so I'm very appreciative for your assistance – Chuck.M Aug 16 '18 at 16:53
  • @Chuck.M absolutely no problem, I remember back when I was getting started and was trying to figure everything out, I relied on good answers in support forums like this. So I'm just kind of paying it forward. – Dylan Cristy Aug 16 '18 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.