0

I'm struggling to implement pagination on another developer's existing code. I've tried implementing some of the solutions explained in a previous SO Post: Pagination on a list using ng-repeat

The code takes a two-dimensional array, parses through it converting to an object of strings, and then displays that on the page. Each object key is used as a column.

Not sure how to utilize Angular's builtin Limit filter with this setup. I think that would be the ideal solution. But I'm open to just a JS solution as well. (I've started it out as a JS solution below towards bottom of post.)

var data = [["c1","blah","yup","halted","bacon", "blue"],
            ["c2","blank","everything","nothing test"],
            ["c3","test","vista","xp, 7 "],
            ["c4","true"],
            ["c5","do","something else really long text", "and more"]
        ]

$rootScope.resultsObject = parseResults(data);

var columnSize = ["twelve", "six", "four", "three", "two", "two"]

$rootScope.columnCountShow = columnSize[columnCounter($rootScope.resultsObject)-1]

factory('parseResults', function(){
        return function(resultsData){
            var resultsObject = {};
            var column;
            for(var i = 0; resultsData.length > i; i++){
                column = "column" +i;
                resultsObject[column] = resultsData[i].toString().replace(/[\,]/g, "<br>")
            }
            return resultsObject;
        }
    })

HTML:

<div class="{{columnCountShow}} columns results-column" ng-repeat="resultsObject in resultsObject">
        <div ng-bind-html-unsafe="resultsObject">
</div>

How would I go about doing a show more style of pagination? It'll go through and display the first 2 results from each array, they click show more, and it loads the next 2, etc.

The current method that I'm using to try and do this: Take the data (before parsed) and slice off a section, and parse only that bit. Then when they select show more, it'll take the next few lines, etc.

    var limit = 3;
    var start = 0;
    for (var i = 0; i < data.length; i++) {
        var ndata = [[]];
        ndata = data[i].slice(start,limit);
        console.log(ndata);
    }

Issue with this, it seems thatdata remains intact. slice doesn't seem to actually remove that portion from the array?

Update

slice was definitely not working out for me. Realized JS has a splice method. (for some reason I didn't think it existed. My current way of splicing up the data is:

$scope.submitWords = function(){
        event.preventDefault();
        var columnCount = columnCounter($scope.keywords);
        var sendObject = keyWordSplit($scope.keywords, columnCount)

        var limit = 2;
        var start = 0;
        var ndata = [[]];
        for (var i = 0; i < $scope.testData.length; i++) {
            ndata[i] = $scope.testData[i].splice(start,limit);
            console.log(ndata);
            console.log("original data <br>" + $scope.testData);
        $rootScope.resultsObject = parseResults(ndata);
        }
        var columnSize = ["twelve", "six", "four", "three", "two", "two"];
        $rootScope.columnCountShow = columnSize[columnCounter($rootScope.resultsObject)-1];
    }

This correctly splits up the data. Now I just need to have it updating my model. When selecting show_more() it replaces the existing data rather than appending to it.

Community
  • 1
  • 1
EnigmaRM
  • 7,523
  • 11
  • 46
  • 72
  • slice won't remove if you assign it to another variable. Do you have a jsfiddle/plunker? – Ven May 09 '13 at 20:23
  • No. I began setting one up, but every time I tried running it, I got an error message about using POST instead. I didn't want to take the time to troubleshoot that as well. – EnigmaRM May 09 '13 at 20:26
  • http://jsfiddle.net/enigmarm/L7CSD/6/ Here is the fiddle. Although I get an error message about needing to use POST. Nothing should be trying to submit. Not sure what to do with that. But now you have a larger source code. Can you help me out? – EnigmaRM May 14 '13 at 16:52

0 Answers0