简体   繁体   中英

Pagination in Suitescript 2.0 Netsuite

I tried implementing netsuite pagination in suitescript 2.0, the prev button is working but instead of 20 search data it's return like whole. Next button is not working always throwing error INVALID_PAGE_RANGE

Here is the suitelet code

         var loadSearch = search.load({
            id: 'customsearch_inv123'

        });
        var i = 0;

        var pagedData = loadSearch.runPaged({
            pageSize: 20
        });

        pagedData.pageRanges.forEach(function (pageRange) {

            var lastPageRange = pagedData.pageRanges[pagedData.pageRanges.length - 1];

            var Page = pagedData.fetch({
                index: lastPageRange.index
            });

            if (context.request.parameters.next) {
                Page = Page.next(); 
            }

            if (context.request.parameters.prev) {
                Page =Page.prev();
            }

            Page.data.forEach(function (result) {

                var number = result.getValue({
                    name: 'inventorynumber'
                });

                if (number) {

                    updateSublist.setSublistValue({
                        id: 'custpage_number',
                        line: i,
                        value: number
                    });
                }


                i++;

            });
        });


        // submit button

        form.addSubmitButton({
            label: 'Submit'
        });

        form.addButton({
            id: '_next',
            label: 'Next',
            functionName: 'next'
        });

        form.addButton({
            id: '_prev',
            label: 'Prev',
            functionName: 'prev'
        });


        form.clientScriptModulePath = 'SuiteScripts/client.js';

In client script i wrote the function next and prev, and redirected to the same page with next or prev parameters and based on that i called page.next and page.prev.

If you have implemented this please help me.

here is client code.

 next: function () {
                window.location.href = 'example.com' + '&next=t';
            },

            prev: function () {
                window.location.href = 'example.com' + '&prev=t';
            }

You should pass current/requested pageNumber from client script and then use it to fetch data for that specific page like below

var pagedData = loadSearch.runPaged({
    pageSize: 20
});

requestedPageNumber = 2

var page = pagedData.fetch({ index: requestedPageNumber });

page.data.forEach(function (result) {
    // result is the searchResult object
    // YOUR CODE GOES HERE
    var number = result.getValue({
        name: 'inventorynumber'
    });
})

What you did wrong was you were starting from the last index and then trying to fetch unexisting array index in case where user clicks next.

Note: previous button should be disabled/hidden where there are no pages so as to not run into the same error and the same could be done for the next button too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM