במידה ואתם מבצעים שמירה אחרי טעינה על הכפתור טען מוצרים -

יש לקחת את ההיברידי הבא מהחנות

autonahariya

https://secure.konimbo.co.il/admin/user_files/205708/edit

כפתור "טען מוצרים נוספים" בממשק ניהול


זה הקוד המעודכן



<script>

{% if var.isActive == 'yes' %}

    // variables

    var counter{{id}}           = 0; // Init counter - counts the number of pages that have been call

    var remainingPages{{id}};       // remianing pages to call

    

    $(document).ready(function () {

        try {

            if ($('body.items.index').length) {

                var currPageNum         = Number($('.pagination span.current').text().trim()); 

                var lastPageNum         = Number($('.pagination a:eq(' +Number($('.pagination a').length -2) + ')').text());

                remainingPages{{id}}    = lastPageNum - currPageNum; // Calculate the difference of the remaining pages


                addUniuqeClass();


                // Add button once

                if (!($('#btn_load_more_items').length)){

                    $('#admin_items').after('<div id="btn_load_more_items{{id}}" class="glow-on-hover">טען מוצרים</div>');

                }

                // Handle on click "load more" items

                $('#btn_load_more_items{{id}}').click(function (e) {

                    if(counter{{id}} < remainingPages{{id}}){ // If - the number of pages is less than the difference between the pages

                        var urlFeed = getNextPageUrl(currPageNum);

                        fetchItems(urlFeed);

                        counter{{id}}++;

                    }

                });

            }

        }

        catch (error) {

            console.log('error hybrid_id {{id}}', error);

        }

    });


    $(document).ajaxComplete(function( event, xhr, settings ) {

        if ($('body.items.index').length) {

            if(counter{{id}} >= remainingPages{{id}}){ // If - no more items to load disable button

                 $('#btn_load_more_items{{id}}').removeClass('glow-on-hover').addClass('disabled').text('אין מוצרים נוספים');

            }

        }

    });


    function addUniuqeClass() {

        try {

            $('#admin_items > tbody tr:not(.trChecked)').each(function(){

                $(this).addClass('trChecked');

            })

        } catch (error) {

            console.log('error addUniuqeClass {{id}}', error);

        }

    }


    function addClickEventToInventoryItem(){

        try {

            $('#admin_items tbody tr:not(.trChecked) a.get_inventory[data-item-id]').click(function() {

                get_ajax_html($(this).attr('data-href'), $('#inventory_item_' + $(this).attr('data-item-id')));

            });

        } catch (error) {

            console.log('error addClickEventToInventoryItem {{id}}', error);

        }

    }

    

    // Function that create a string of next page url respectively

    function getNextPageUrl(currPageNum) { // currPageNum: used to get the next page respectively

        try {

            // variables

            var nextPageUrl;

            var currUrl     = window.location.href;

            nextPageNum     = currPageNum + counter{{id}} + 1; // add an additional page according to current page and counter

                

            if (currUrl.indexOf('&page=') > -1) { // if - it's not page one replace the URL page to the upcoming page

                nextPageUrl = currUrl.replace('&page=' + currPageNum, '&page=' + nextPageNum);

            } else { // else - adding the parameter of the upcoming page number

                nextPageUrl = currUrl + "&page=" + nextPageNum;

            }

            // console.log('nextPageUrl= ', nextPageUrl);

            return nextPageUrl;

        }

        catch (error) {

            console.log('error getNextPageUrl {{id}}', error);

        }

    } // end getNextPageUrl


    // Function that make an ajax call to get data of the following url

    function fetchItems(url) { // url: the next page url to retrive data

        try {

            $.get(url, function (data) {

                if ($(data).find('#admin_items tbody tr').length) {

                    addItems(data);

                    console.log("Get items successfully (ajax - ", url, " )");

                }

            })

        } catch (error) {

            console.log('error fetchItems {{id}}', error);

        }

    } // end fetchItems

    

    // Function that adding items to table

    // function addItems(data) { // data: contains the information of the following page

    //     try {

    //         $('table#admin_items > tbody').append($(data).find('#admin_items tbody tr'));

    //         addClickEventToInventoryItem();

    //         addUniuqeClass();

    //     } catch (error) {

    //         console.log('error addItems {{id}}', error);

    //     }

    // } // end addItems

    // פונקציה שמוסיפה את המוצרים החדשים לטבלה ומנקה להם את השמות

    function addItems(data) { 

        try {

            // שליפת השורות החדשות מהנתונים שהגיעו מהשרת

            var $newRows = $(data).find('#admin_items tbody tr');

            

            // ניקוי השמות מהשורות החדשות לפני ההזרקה לדף

            $newRows.find('input, select').each(function () {

                var $el = $(this);

                var currentName = $el.attr('name');

                

                if (currentName) {

                    $el.attr('data-name', currentName);

                    $el.removeAttr('name');

                    

                    // הצמדת אירוע שיחזיר את השם רק אם המשתמש ישנה את הערך

                    $el.bind('change', function() {

                        var nameToRestore = $(this).attr('data-name');

                        if (nameToRestore) {

                            $(this).attr('name', nameToRestore);

                            // לוגיקה ייחודית לצ'קבוקסים של קונימבו (שדות נסתרים)

                            if($(this).attr('type') == "checkbox") {

                                $(this).siblings('input[data-name="'+nameToRestore+'"]').attr('name', nameToRestore);

                            }

                        }

                    });

                }

            });


            // הזרקת השורות "הנקיות" לטבלה

            $('table#admin_items > tbody').append($newRows);

            

            addClickEventToInventoryItem();

            addUniuqeClass();

        } catch (error) {

            console.log('error addItems 205708', error);

        }

    }

{% endif %}

</script>