Show only top 10 in jQuery template

We use jQuery, jQuery, and the jQuery Wufoo APIs to display data from our forms using the following code:

<div id="people">           
    <ul>
        <script id="attendeeTemplate" type="text/x-jquery-tmpl">
            <li>
                <h4>${Field1}</h4>
                ${Field3} minutes
            </li>
             </script>
    </ul>
</div>

This works well, but we would like to limit it to displaying only the first 10 entries. Now it displays every entry in the database (sorted using JavaScript and the API below). Please note that we are trying to show only the top 10, sorted by the least number of minutes.

Here is the javascript:

<script>
    function processEntries(data) {
        $.each(data.Entries, function(entriesIndex, entriesObject) {
            // Make sure this entry has all the required bits
            if (entriesObject.Field1 && entriesObject.Field3) {
                $("#attendeeTemplate").tmpl(entriesObject).appendTo("#people ul");  
            }
        });
    };

    $.wufooAPI.getEntries({
        "callback"   : processEntries,             // Name of custom function declared above
        "formHash"   : "BLAH",                   // Found by clicking "Code" button from the Form Manager, then the "API Information" button
        "sortID"   : "Field3",                   // Found by clicking "Code" button from the Form Manager, then the "API Information" button
        "sortDirection"   : "DESC",                   // Found by clicking "Code" button from the Form Manager, then the "API Information" button

    });
</script>

Any thoughts on how to limit it to the top 10 will be very useful!

+3
source share
3 answers

Assuming what data.Entriesis an array, you can use Array.slice():

$.each(data.Entries.slice(0, 9), function(entriesIndex, entriesObject) {
+1
source

:

data.Entries.slice(0,9); 
+3

, , :

function processEntries(data) {
    var i = 0;
    $.each(data.Entries, function(entriesIndex, entriesObject) {
        // Make sure this entry has all the required bits
        if (entriesObject.Field1 && entriesObject.Field3 && i<10) {
            $("#attendeeTemplate").tmpl(entriesObject).appendTo("#people ul");
            i++;
        }
    });
};

: , , -

+2

All Articles