Monday, 29 October 2012

JS Code for Pagination and Sorting


  //Script for displaying specific number of pages
    $(".TableSize").change(function () {
        var numberOfApps = $(".TableSize").val();
            $.ajax({
                type: "POST",
                url: "/Home/JsonAppManager",
                success: function (imageList) {
                                var imageCount = imageList.length;
                                var pages = parseInt((imageCount / numberOfApps), 10);
                                var totalPages, currentPage, startRow, endRow,tempList=[];
                                    if ((imageCount % numberOfApps) > 0) {
                                        totalPages = pages + 1;
                                    }
                                    else {
                                        totalPages = pages;
                                    }
                                    if (imageCount <= numberOfApps) {
                                        totalPages = 1;
                                    }
                                $('#FeaturedAppsTable').remove();
                                $('.AppsTableDiv').html('');
                                $('<table id="FeaturedAppsTable"></table>').appendTo('.AppsTableDiv');
                                    for (rowNum = 0; rowNum < numberOfApps; rowNum++) {
                                        currentPage = 1;
                                        if (imageList[rowNum] == null) {
                                            break;
                                        }
                                        tempList[rowNum] = imageList[rowNum];
                                    }
                                createTable.call(this,tempList);
                                $('<div class="PaginationDiv"></div>').appendTo('.AppsTableDiv');
                                $('<div class="PageNumberDiv"></div>').appendTo('.PaginationDiv');
                                $('<label id="CurrentPage">' + currentPage + '  of  ' + totalPages + '</label>').appendTo('.PaginationDiv');
                                    for (pages = 1; pages <= totalPages; pages++) {
                                        $('<a href="#" class="PageNumberAnchor"><span class="PageNumber">' + pages + '</span></a>').appendTo('.PageNumberDiv');
                                        if (pages != totalPages) {
                                            $('<span> |   </span>').appendTo('.PageNumberDiv');
                                        }
                                    }

                                     //Script for Pagination for Featured Apps Table
                                    $('.PageNumber').click(function () {
                                        $('#FeaturedAppsTable').html('');
                                        var page = $(this).html();
                                        currentPage = parseInt(page, 10);
                                        startRow = (currentPage - 1) * numberOfApps;
                                        endRow = parseInt(startRow, 10) + parseInt(numberOfApps, 10);
                                        tempList = [];
                                            for (rowNum = startRow,page=0; rowNum < endRow; rowNum++) {
                                                if (imageList[rowNum] == null) {
                                                    break;
                                                }
                                                tempList[page++] = imageList[rowNum];
                                            }
                                        createTable.call(this, tempList);
                                        $('#CurrentPage').text(currentPage + '  of  ' + totalPages);

                                        //Script to sort list in Ascending order
                                        $('#SortAsec').click(function () {
                                            imageList = sortListInAscending.call(this, imageList);
                                            tempList = [];
                                            for (rowNum = startRow, page = 0; rowNum < endRow; rowNum++) {
                                                if (imageList[rowNum] == null) {
                                                    break;
                                                }
                                                tempList[page++] = imageList[rowNum];
                                            }
                                            reCreateTableBody.call(this, tempList);
                                        });
                                        //Script to sort list in Descecinding Order
                                        $('#SortDesc').click(function () {
                                            imageList = sortListInDescending.call(this, imageList);
                                            tempList = [];
                                            for (rowNum = startRow, page = 0; rowNum < endRow; rowNum++) {
                                                if (imageList[rowNum] == null) {
                                                    break;
                                                }
                                                tempList[page++] = imageList[rowNum];
                                            }
                                            reCreateTableBody.call(this, tempList);
                                        });
                                    });

                                    //Script to sort list in Ascending order
                                                $('#SortAsec').click(function () {
                                                    imageList = sortListInAscending.call(this, imageList);
                                                    tempList = [];
                                                    for (rowNum = 0; rowNum < numberOfApps; rowNum++) {
                                                        if (imageList[rowNum] == null) {
                                                            break;
                                                        }
                                                        tempList[rowNum] = imageList[rowNum];
                                                    }
                                                    reCreateTableBody.call(this, tempList);
                                                });
                                    //Script to sort list in Descecinding Order
                                                $('#SortDesc').click(function () {
                                                    imageList = sortListInDescending.call(this, imageList);
                                                    tempList = [];
                                                    for (rowNum = 0; rowNum < numberOfApps; rowNum++) {
                                                        if (imageList[rowNum] == null) {
                                                            break;
                                                        }
                                                        tempList[rowNum] = imageList[rowNum];
                                                    }
                                                    reCreateTableBody.call(this, tempList);
                                                });

                }
            });
    });

    //User Defined function for Creating a Table using given Image List
    function createTable(imageList) {
        $('<thead id="TableHead"><tr><th>Image</th><th>Title <a href="#" class="SortAnchor"><span id="SortAsec" title="Sort Asecinding">&Delta;</span></a><a href="#" class="SortAnchor"><span id="SortDesc" title="Sort Descinding">&nabla;</span></a></th><th>Description</th><th></th><th></th></tr></thead>').appendTo('#FeaturedAppsTable');
        $('<tbody id="TableBody"></tbody>').appendTo('#FeaturedAppsTable');
        $(imageList).each(function (imageList, value) {
            $('<tr><td><img src="' + value.ImagePath + '" alt="'+value.Title+'" height="50" width="50"/></td><td class="AppTitleColumn">' + value.Title + '</td><td class="AppDescriptionColumn">' + value.Description + '</td><td><a href="/Home/editApp/' + value.ID + '" title="Edit This App Details"><img src="../../Content/themes/base/images/edit.png" height="20" width="20" class="EditAppImg"/></a></td><td><a href="/Home/deleteApp/' + value.ID + '" title="Delete This App"><img src="../../Content/themes/base/images/delete.png" class="DeleteAppImg" height="20" width="20"/></a></td></tr>').appendTo('#TableBody');
        });
        $('<tfoot id="TableFooter"><tr><th>Image</th><th>Title</th><th>Description</th><th></th><th></th></tr></tfoot>').appendTo('#FeaturedAppsTable');
        return 1;
    }

    //User Defined function for Changing the body of a table using given image List
    function reCreateTableBody(imageList) {
        $('#TableBody').html('');
        $(imageList).each(function (imageList, value) {
            $('<tr><td><img src="' + value.ImagePath + '" alt="' + value.Title + '" height="50" width="50"/></td><td class="AppTitleColumn">' + value.Title + '</td><td class="AppDescriptionColumn">' + value.Description + '</td><td><a href="/Home/editApp/' + value.ID + '" title="Edit This App Details"><img src="../../Content/themes/base/images/edit.png" height="20" width="20" class="EditAppImg"/></a></td><td><a href="/Home/deleteApp/' + value.ID + '" title="Delete This App"><img src="../../Content/themes/base/images/delete.png" class="DeleteAppImg" height="20" width="20"/></a></td></tr>').appendTo('#TableBody');
        });
    }

    //user Defined funtion for Sorting a List in Ascending Order
    function sortListInAscending(tempList) {
        tempList.sort(function (a, b) {
            var titleA = a.Title.toLowerCase(), titleB = b.Title.toLowerCase();

            if (titleA < titleB)
                return -1;
            else if (titleA > titleB)
                return 1;
            else

                return 0;

        });
        return tempList;
    }
    //User Defined function for Sorting List in decending Order
    function sortListInDescending(tempList) {
        tempList.sort(function (a, b) {
            var titleA = a.Title.toLowerCase(), titleB = b.Title.toLowerCase();

            if (titleA > titleB)
                return -1;
            else if (titleA < titleB)
                return 1;
            else

                return 0;

        });
        return tempList;
    }
});

No comments:

Post a Comment