0

I want paginate data in the JavaScript array. The number of products to be displayed on the page is 3. There will be pagination after every 3 products. But these pages should come when I make a request. Below is an example of for loop where I brought the page data.

Below is a sample JavaScript Array And Fiddle

           //Products 
        var product = [{
        "product_name": "Product 1",
        "product_id": "8991",
        "product_price": "$69",
        categories: [{
            categoryName: 'Tea'
        }, {
            categoryName: 'Coffee'
        }],

    },

    {
        "product_name": "Product 2",
        "product_id": "8951",
        "product_price": "29 TL",
        categories: [{
            categoryName: 'Tea'
        }]


    },
    {
        "product_name": "Product 3",
        "product_id": "8941",
        "product_price": "79 TL",
        categories: [{
            categoryName: 'Gift'
        }, {
            categoryName: 'Coffee'
        }],




    },
    {
        "product_name": "Product 4",
        "product_id": "8931",
        "product_price": "39 TL",
        categories: [{
            categoryName: 'Gift'
        }, {
            categoryName: 'Tea'
        }],



    },
    {
        "product_name": "Product 5",
        "product_id": "8911",
        "product_price": "49 TL",
        categories: [{
            categoryName: 'Gift'
        }],



    },
    {
        "product_name": "Product 6",
        "product_id": "8971",
        "product_price": "59 TL",
        categories: [{
            categoryName: 'Toys'
        }, {
            categoryName: 'Gift'
        }, {
            categoryName: 'Tea'
        }],


    },
    {
        "product_name": "Product 7",
        "product_id": "8921",
        "product_price": "69 TL",
        categories: [{
            categoryName: 'Coffe'
        }, {
            categoryName: 'Kahve'
        }],


    },
    {
        "product_name": "Product 8",
        "product_id": "8431",
        "product_price": "19 TL",
        categories: [{
            categoryName: 'Food'
        }],


    },
    {
        "product_name": "Product 9",
        "product_id": "8438",
        "product_price": "19 TL",
        categories: [{
            categoryName: 'Food'
        }],


    },
    {
        "product_name": "Product 10",
        "product_id": "8440",
        "product_price": "19 TL",
        categories: [{
            categoryName: 'Food'
        }],


    },
    {
        "product_name": "Product 11",
        "product_id": "8441",
        "product_price": "19 TL",
        categories: [{
            categoryName: 'Food'
        }],


    },
   {
        "product_name": "Product 12",
        "product_id": "8442",
        "product_price": "19 TL",
        categories: [{
            categoryName: 'Food'
        }],


    },

    {
        "product_name": "Product 13",
        "product_id": "8444",
        "product_price": "19 TL",
        categories: [{
            categoryName: 'Food'
        }],


    },

    {
        "product_name": "Product 14",
        "product_id": "8445",
        "product_price": "19 TL",
        categories: [{
            categoryName: 'Food'
        }],


    },

    {
        "product_name": "Product 15",
        "product_id": "8446",
        "product_price": "19 TL",
        categories: [{
            categoryName: 'Food'
        }],


    },

];

JavaScript Function

   function getProducts(products = [], categoryName = 'Food') {
                    let result = [];

                    for (let product of products) {
                    if (product.categories === undefined || !Array.isArray(product.categories)) {
                    break;
                    }

            for (let category of product.categories) {
                if (category.categoryName === categoryName) {
                    result.push(product);
                    break;
                }
            }
                    }

            return result;
        }

JavaScript Loop

const categoryName = $(this).data('category');
const products = getProducts(product, categoryName);    


for (let product of products) {
     $(tab).append( "<p>Product name:"+product.product_name+"</p><p>Product Id:"+product.product_id+"</p><p>Price:"+product.product_price+"</p>");
               }
Erdal Bakkal
  • 343
  • 1
  • 6
  • 21
  • I'm having difficulties understanding what you are asking / trying to achieve. – connexo Jan 15 '20 at 13:37
  • @connexo updated my question. I need paginate per 3 items. – Erdal Bakkal Jan 15 '20 at 13:40
  • If your intention was to split array of objects into chunks, you might want to check out [following solution](https://stackoverflow.com/a/57357024/11299053) – Yevhen Horbunkov Jan 15 '20 at 13:52
  • @YevgenGorbunkov I was think split it but I want to when i click to next button and then it will work and get to next 3 item. Not get one time get all item and slide it. – Erdal Bakkal Jan 15 '20 at 13:55
  • I have [posted](https://stackoverflow.com/a/59748794/11299053) my implementation of similar (as I understood that, at least) use case recently. It is based on React.js, however core principle, very basic actually, you might find of use either. – Yevhen Horbunkov Jan 15 '20 at 14:05
  • @YevgenGorbunkov Thank you answer. Can you create fiddle basic sample for JavasScript as my code? – Erdal Bakkal Jan 15 '20 at 14:09
  • ...so post a sample of your codebase targeting some specific feature, preferrably in a form of executable snippet, reproducing the issue you're having, point out specific problem you attempt to solve along with expected behavior, then you may expect adequate support – Yevhen Horbunkov Jan 15 '20 at 14:17
  • That's a huge leap forward and yet there's missing one small, but critical part - what have you tried to achieve the feature you've been after and where exactly you have failed? – Yevhen Horbunkov Jan 15 '20 at 14:37
  • @YevgenGorbunkov Obviously I couldn't execute an idea and add code to paging. I want to paging inside to tabs content. There are coming category based product. I need get 3 product and next button get next 3 product ... like that. – Erdal Bakkal Jan 15 '20 at 14:42

0 Answers0