Result count in json

3 posts, 0 answers
  1. Steven
    Steven avatar
    2 posts
    Member since:
    Sep 2012

    Posted 25 Sep 2012 Link to this post

    Hey guys,

    I'm using DataSource to drive a sort of "live filtering". After a filter's been applied I reload the data-source with the appropriate get-vars, but I want to add the results-count to this. I'm pretty flexible in how I construct my json (example of current json below), but I'm not sure how I would use kendo to place this somewhere in my html. My json looks like this:

    {
     "results": [
    {
     "title": "My entry",
     "description: "My description"
    },
    {
     "title": "Another entry",
     "description: "Another description"
    },
    ]
    }

    So my questions are:

    - Where in my json would I add the result-count?
    - How do I make kendo render this to the current document?

    Thanks guys!

    - Steven
  2. Todd Anglin
    Todd Anglin avatar
    2040 posts
    Member since:
    Aug 2005

    Posted 26 Sep 2012 Link to this post

    Hello Steven-

    I'm not sure why you would need the results count as a static value in your JSON. If you just need the count of results, you should be able to simply grab the length of the results array. Right?

    What I do see people do more often is include the total record count in a JSON response to both A) help with paging, and B) show users the total record count without loading all results. To do that, you normally create two top level properties in your JSON response, one with the results, one with the row count:


    "__count": 100,
    "results": [ .. ]
    }

    The DataSource can then be mapped to each of these properties, pulling-off the total result count and then grabbing the results collection. You can actually see an example of that in this online demo (be sure to inspect the JSON from the Netflix web service):

    http://demos.kendoui.com/web/datasource/remote-operations.html

    As for the second part of your question, I'm not quite sure what you're trying to do. Perhaps you can create a quick jsBin or jsFiddle to illustrate what you're after and we can quick talk around code.

    Hope this helps.

    -Todd
  3. Kendo UI is VS 2017 Ready
  4. Steven
    Steven avatar
    2 posts
    Member since:
    Sep 2012

    Posted 28 Sep 2012 Link to this post

    Hey Todd,

    Thanks for your response, I've been experimenting and I've gotten it to work (with kendo.data.ObservableObject). I now have a weird situation on my hand: it's working fine, but sporadically it'll not output my results at all. This is my code:

    var jobCount = new kendo.data.ObservableObject({
            count: 20
        });
     
        jobCount.bind('change', function(){
            if(this.count == 0){
                $('#result-wrapper').prepend('<h2>Er zijn geen vacatures gevonden.</h2>');
            } else if(this.count == 1){
                $('#result-wrapper').prepend('<h2>Er is <span class="blue">'+this.count+'</span> vacature gevonden.</h2>');
            } else {
                $('#result-wrapper').prepend('<h2>Er zijn <span class="blue">'+this.count+'</span> vacatures gevonden.</h2>');
            }
        });

    On the change-event of my dataSource I update the ObservableObject:

    jobCount.set('count', this.view().length);

    I'm sure it's not the most elegant solution, but it had appeared to work. You can find the page here: http://high-quality.dierclients.com/kandidaten/vacatures/. If you click one of the filters it updates the data-source. After selecting a couple and changing it back and forth it'll randomly not display the count a couple of times, then start displaying it again... If you could have a look, much appreciated!

    Thanks,

    - Steven
Back to Top