diff --git a/database/db.js b/database/db.js index 375238a..b73cfc5 100644 --- a/database/db.js +++ b/database/db.js @@ -269,8 +269,6 @@ db.getBookmarksTable = function(user_id) { } db.getBookmarksSearch = function(params) { - var search_word = params.searchWord || 'test'; - var user_id = '1'; var sql = "SELECT id, user_id, title, description, url, public, click_count, DATE_FORMAT(created_at, '%Y-%m-%d') as created_at, DATE_FORMAT(last_click, '%Y-%m-%d') as last_click FROM `bookmarks` WHERE 1=1"; if (params.dateCreate) { @@ -305,17 +303,33 @@ db.getBookmarksSearch = function(params) { sql += " AND `user_id` IN (SELECT `id` FROM `users` WHERE `username` LIKE '%" + params.username + "%' )" } } - sql += " ORDER BY click_count DESC, created_at DESC LIMIT 0, 50"; + + params.currentPage = params.currentPage || 1; + params.currentPageItems = params.currentPageItems || 20; + sql += " ORDER BY click_count DESC, created_at DESC"; console.log(sql); return new Promise(function(resolve, reject) { client.query(sql, (err, result) => { if (err) { reject(err); } else { - resolve(result); + + sql += " LIMIT " + (params.currentPage - 1) * params.currentPageItems + ", " + params.currentPageItems; + var totalItems = result.length; + client.query(sql, (err, result) => { + if (err) { + reject(err); + } else { + var searchData = { + totalItems: totalItems, + bookmarks: result, + } + resolve(searchData); + } + }); } }); - }); + }) } db.getBookmarksCard = function(user_id) { diff --git a/public/index.html b/public/index.html index 07b2e68..12be2a7 100644 --- a/public/index.html +++ b/public/index.html @@ -32,6 +32,7 @@ + diff --git a/public/scripts/controllers/bookmarks-controller.js b/public/scripts/controllers/bookmarks-controller.js index 15b12e8..3f65fa5 100644 --- a/public/scripts/controllers/bookmarks-controller.js +++ b/public/scripts/controllers/bookmarks-controller.js @@ -6,6 +6,18 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', ' $scope.bookmarkEditHover = false; $scope.showStyle = 'navigate'; // 显示风格'navigate', 'card', 'table' $scope.edit = false; + $scope.totalPages = 0; + $scope.currentPage = 1; + $scope.inputPage = ''; + $scope.changeCurrentPage = function(currentPage) { + currentPage = parseInt(currentPage) || 0; + console.log(currentPage); + if (currentPage <= $scope.totalPages && currentPage >= 1) { + $scope.currentPage = currentPage; + $scope.inputPage = ''; + } + } + var params = { showStyle: $scope.showStyle, } @@ -67,6 +79,8 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', ' bookmarkService.getBookmarks(params) .then((data) => { $scope.bookmarks = data; + $scope.totalPages = parseInt(Math.random() * 1000); + $scope.currentPage = (parseInt(Math.random() * 1000) % $scope.totalPages) + 1; pubSubService.publish('Common.menuActive', { login: true, index: 0 @@ -77,31 +91,6 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', ' $scope.$on('viewContentLoaded', function(elementRenderFinishedEvent) { $('.ui.dropdown').dropdown(); - $('.ui.calendar.js-date-begin').calendar({ - type: 'date', - formatter: { - date: function(date, settings) { - if (!date) return ''; - var day = date.getDate(); - var month = date.getMonth() + 1; - var year = date.getFullYear(); - return year + '/' + month + '/' + day; - } - }, - endCalendar: $('.ui.calendar.js-date-end') - }); - $('.ui.calendar.js-date-end').calendar({ - type: 'date', - formatter: { - date: function(date, settings) { - if (!date) return ''; - var day = date.getDate(); - var month = date.getMonth() + 1; - var year = date.getFullYear(); - return year + '/' + month + '/' + day; - } - }, - startCalendar: $('.ui.calendar.js-date-begin') - }); }); + }]); diff --git a/public/scripts/controllers/search-controller.js b/public/scripts/controllers/search-controller.js index 73c36d1..de237e7 100644 --- a/public/scripts/controllers/search-controller.js +++ b/public/scripts/controllers/search-controller.js @@ -1,5 +1,6 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$window', '$timeout', 'bookmarkService', 'pubSubService', function($scope, $state, $stateParams, $filter, $window, $timeout, bookmarkService, pubSubService) { console.log("Hello searchCtr...", $stateParams); + const currentPageItems = 20; $scope.bookmarks = []; // 书签数据 $scope.showSearch = false; // $scope.showTags = false; // @@ -13,6 +14,18 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi $scope.userRange = ''; $scope.bookmarkCount = 0; $scope.tags = [] + $scope.totalPages = 0; + $scope.currentPage = 1; + $scope.inputPage = ''; + $scope.changeCurrentPage = function(currentPage) { + currentPage = parseInt(currentPage) || 0; + console.log(currentPage); + if (currentPage <= $scope.totalPages && currentPage >= 1) { + $scope.currentPage = currentPage; + $scope.inputPage = ''; + $scope.search(); + } + } bookmarkService.getTags({ user_id: '1', @@ -24,6 +37,8 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi var searchParams = { searchWord: $scope.searchWord, + currentPage: 1, + currentPageItems: currentPageItems, } if ($scope.searchWord) { searchBookmarks(searchParams); @@ -86,7 +101,8 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi params.dateClickBegin = $scope.dateClickBegin; params.dateClickEnd = $scope.dateClickEnd; } - + params.currentPage = $scope.currentPage; + params.currentPageItems = currentPageItems; searchBookmarks(params) console.log('search..', params) } @@ -117,8 +133,10 @@ app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$wi function searchBookmarks(params) { bookmarkService.searchBookmarks(params) .then((data) => { - $scope.bookmarks = data; - $scope.bookmarkCount = data.length; + $scope.bookmarks = data.bookmarks; + $scope.bookmarkCount = data.totalItems; + $scope.totalPages = Math.ceil($scope.bookmarkCount / currentPageItems); + pubSubService.publish('Common.menuActive', { login: true, index: 0 diff --git a/public/scripts/directives/pagination-directive.js b/public/scripts/directives/pagination-directive.js new file mode 100644 index 0000000..22071c6 --- /dev/null +++ b/public/scripts/directives/pagination-directive.js @@ -0,0 +1,12 @@ +// 不要使用已有的html作为指令,如menu,否则angular会陷入死循环 +app.directive('pagination', function() { + return { + restrict: 'EA', + templateUrl: '/views/pagination.html', + replace: true, + // scope: { + // conf: '=' + // }, + link: function(scope, element, attrs) {} + } +}); diff --git a/public/views/bookmarks.html b/public/views/bookmarks.html index 17c70fd..cb47513 100644 --- a/public/views/bookmarks.html +++ b/public/views/bookmarks.html @@ -28,20 +28,6 @@
-|
-
+ |
||||||
|---|---|---|---|---|---|---|
|
-
+ |