Matthew The Terrible
Matthew The Terrible

Reputation: 1643

jquery plugin, set property value to ajax data

I am struggling here. Using this simple pagination plug (http://flaviusmatis.github.io/simplePagination.js/#page-19) and trying to set the number of items to a value returned from an ajax call. But it will never work. If I set the number of items to a hard coded number it works just fine, but every for every letter I call this function with the number of items will change so I want to do it with this call.

This is what I have:

$(document).ready(function () {

        ko.applyBindings(viewModel);
        var numNames;

        $.ajax({
            type: "GET",
            url: "/api/Name/GetBoyCount?letter=" + viewModel.Letter(),
            complete: function (data) {
                $(selector).pagination('updateItems', parseInt(data));

            }
        });

        $(function () {
            $(selector).pagination({
                items: 100,
                itemsOnPage: 175,
                cssStyle: 'light-theme'
            });
        });

// etc

Upvotes: 0

Views: 132

Answers (1)

Mark F
Mark F

Reputation: 176

Your ajax call is probably finishing before your initialization, try taking the initialization code out of the .ready() call back.

Change this

$(function(){
    $(selector).pagination({
        etc....
    });
});

To this $(selector).pagination({ etc.... });

Also $(document).ready(function(){}) is the same as $(function(){}) so you are calling .ready() from inside another ready check.

Upvotes: 2

Related Questions