给分类加个排序

This commit is contained in:
luchenqun 2017-03-06 12:43:29 +08:00
parent f88077b7ac
commit 23b4ed211a
4 changed files with 105 additions and 57 deletions

View File

@ -567,16 +567,38 @@ db.getBookmarksByTag = function(params) {
params.currentPage = params.currentPage || 1;
params.perPageItems = params.perPageItems || 20;
var sql = "SELECT bookmarks.id, bookmarks.user_id, bookmarks.title, bookmarks.description, bookmarks.url, bookmarks.public, bookmarks.click_count, DATE_FORMAT(bookmarks.created_at, '%Y-%m-%d') as created_at, DATE_FORMAT(bookmarks.last_click, '%Y-%m-%d') as last_click FROM `tags_bookmarks`, `bookmarks` WHERE tags_bookmarks.tag_id = '" + tag_id + "' AND tags_bookmarks.bookmark_id = bookmarks.id ORDER BY bookmarks.click_count DESC, bookmarks.created_at DESC";
var sql = "SELECT bookmarks.id, bookmarks.user_id, bookmarks.title, bookmarks.description, bookmarks.url, bookmarks.public, bookmarks.click_count, DATE_FORMAT(bookmarks.created_at, '%Y-%m-%d %H:%i:%s') as created_at, DATE_FORMAT(bookmarks.last_click, '%Y-%m-%d %H:%i:%s') as last_click FROM `tags_bookmarks`, `bookmarks` WHERE tags_bookmarks.tag_id = '" + tag_id + "' AND tags_bookmarks.bookmark_id = bookmarks.id";
return new Promise(function(resolve, reject) {
client.query(sql, (err, result) => {
if (err) {
reject(err);
} else {
var bookmarksClickCount, bookmarksCreatedAt, bookmarksLatestClick;
result.sort((a, b) => {
var click1 = parseInt(a.click_count);
var click2 = parseInt(b.click_count);
if (click1 > click2) {
return -1;
} else if (click1 == click2) {
return a.created_at >= b.created_at ? -1 : 1;
} else {
return 1;
}
})
bookmarksClickCount = result.slice((params.currentPage - 1) * params.perPageItems, params.currentPage * params.perPageItems);
result.sort((a, b) => a.created_at >= b.created_at ? -1 : 1);
bookmarksCreatedAt = result.slice((params.currentPage - 1) * params.perPageItems, params.currentPage * params.perPageItems);
result.sort((a, b) => a.last_click >= b.last_click ? -1 : 1);
bookmarksLatestClick = result.slice((params.currentPage - 1) * params.perPageItems, params.currentPage * params.perPageItems);
var bookmarksData = {
totalItems: result.length,
bookmarks: result.slice((params.currentPage - 1) * params.perPageItems, params.currentPage * params.perPageItems),
bookmarksClickCount: bookmarksClickCount,
bookmarksCreatedAt: bookmarksCreatedAt,
bookmarksLatestClick: bookmarksLatestClick,
}
resolve(bookmarksData);
}

View File

@ -4,6 +4,8 @@ app.controller('tagsCtr', ['$scope', '$filter', '$window', '$stateParams', '$tim
const perPageItems = 20;
var dialog = null;
$scope.order = [false, false, false];
$scope.order[($stateParams && $stateParams.orderIndex) || 0] = true;
$scope.loadBookmarks = false;
$scope.loadTags = false;
$scope.tags = []; // 书签数据
@ -19,12 +21,25 @@ app.controller('tagsCtr', ['$scope', '$filter', '$window', '$stateParams', '$tim
$scope.newTag = '';
$scope.waitDelTag = {};
$scope.waitDelBookmark = {};
$scope.bookmarkData = {};
pubSubService.subscribe('MenuCtr.tags', $scope, function(event, data) {
console.log('subscribe MenuCtr.tags', data);
getTags({});
});
$scope.changeOrder = function(index) {
$scope.order = $scope.order.map(() => false);
$scope.order[index] = true;
if ($scope.order[0]) {
$scope.bookmarks = $scope.bookmarkData.bookmarksClickCount;
} else if ($scope.order[1]) {
$scope.bookmarks = $scope.bookmarkData.bookmarksCreatedAt;
} else {
$scope.bookmarks = $scope.bookmarkData.bookmarksLatestClick;
}
}
$scope.getBookmarks = function(tagId, currentPage) {
$scope.bookmarkClicked = true;
$scope.currentTagId = tagId;
@ -44,10 +59,18 @@ app.controller('tagsCtr', ['$scope', '$filter', '$window', '$stateParams', '$tim
perPageItems: perPageItems,
};
$('.js-tags-table').transition('hide');
bookmarkService.getBookmarksByTag(params)
.then((data) => {
$scope.bookmarks = data.bookmarks;
$scope.bookmarkCount = data.totalItems;
$scope.bookmarkData = data;
if ($scope.order[0]) {
$scope.bookmarks = $scope.bookmarkData.bookmarksClickCount;
} else if ($scope.order[1]) {
$scope.bookmarks = $scope.bookmarkData.bookmarksCreatedAt;
} else {
$scope.bookmarks = $scope.bookmarkData.bookmarksLatestClick;
}
$scope.bookmarkCount = $scope.bookmarkData.totalItems;
$scope.totalPages = Math.ceil($scope.bookmarkCount / perPageItems);
$scope.inputPage = '';

View File

@ -51,13 +51,13 @@
<table class="ui selectable sortable celled table js-tags-table" ng-if="bookmarkCount > 0" style="margin-top:-14px;" ng-show="!loadBookmarks && !editMode">
<thead>
<tr>
<th>标题</th>
<th>链接</th>
<th style=" width:90px;" class="sorted descending">点击次数</th>
<th style="width:100px;" class="sorted ascending">创建日期</th>
<th style="width:100px;" class="sorted descending">最后点击</th>
<th style="width:150px;">分类</th>
<th style="width:88px;">操作</th>
<th style="cursor:default;">标题</th>
<th style="cursor:default;">链接</th>
<th style=" width:90px;" class="sorted" ng-class="{descending: order[0]}" ng-click="changeOrder(0)">点击次数</th>
<th style="width:100px;" class="sorted" ng-class="{descending: order[1]}" ng-click="changeOrder(1)">创建日期</th>
<th style="width:100px;" class="sorted" ng-class="{descending: order[2]}" ng-click="changeOrder(2)">最后点击</th>
<th style="width:150px;cursor:default;">分类</th>
<th style="width:88px;cursor:default;">操作</th>
</tr>
</thead>
<tbody>
@ -72,8 +72,8 @@
<span title="{{bookmark.url}} 点击复制链接" ng-click="copy(bookmark.id, bookmark.url)" id="tagurl{{bookmark.id}}" style="cursor:default;">{{ bookmark.url }}</span>
</td>
<td>{{ bookmark.click_count }}</td>
<td>{{ bookmark.created_at }}</td>
<td>{{ bookmark.last_click }}</td>
<td>{{ bookmark.created_at.substr(0, 10) }}</td>
<td>{{ bookmark.last_click.substr(0, 10) }}</td>
<td>
<div class="ui label" ng-repeat="tag in bookmark.tags" tag-id="{{ tag.id }}" ng-click="getBookmarks(tag.id, 1)">
{{ tag.name }}

View File

@ -358,14 +358,18 @@ api.get('/bookmarksByTag', function(req, res) {
var totalItems = 0;
var totalItems = 0;
var sendData = {
totalItems: totalItems,
bookmarks: []
totalItems: 0,
bookmarksClickCount: [],
bookmarksCreatedAt: [],
bookmarksLatestClick: [],
}
db.getBookmarksByTag(params)
.then((bookmarksData) => {
bookmarks = bookmarksData.bookmarks;
totalItems = bookmarksData.totalItems;
var bookmarkIds = bookmarks.map((bookmark) => bookmark.id);
sendData = bookmarksData;
var bookmarkIds = []
.concat(sendData.bookmarksClickCount.map((bookmark) => bookmark.id))
.concat(sendData.bookmarksCreatedAt.map((bookmark) => bookmark.id))
.concat(sendData.bookmarksLatestClick.map((bookmark) => bookmark.id))
return db.getTagsBookmarks(bookmarkIds);
})
.then((tbs) => {
@ -373,9 +377,11 @@ api.get('/bookmarksByTag', function(req, res) {
return db.getTags(userId);
})
.then((tags) => {
var data = [];
// 获取每个书签的所有分类标签
bookmarks.forEach(function(bookmark) {
var objectName = ['bookmarksClickCount', 'bookmarksCreatedAt', 'bookmarksLatestClick'];
objectName.forEach((name) => {
console.log(JSON.stringify(tagsBookmarks));
sendData[name].forEach(function(bookmark, index) {
var bookmarkTags = [];
tagsBookmarks.forEach(function(tb) {
if (tb.bookmark_id == bookmark.id) {
@ -386,12 +392,9 @@ api.get('/bookmarksByTag', function(req, res) {
})
}
});
bookmark.tags = bookmarkTags;
data.push(bookmark);
sendData[name][index].tags = bookmarkTags;
})
})
sendData.totalItems = totalItems;
sendData.bookmarks = data;
res.json(sendData);
})
.catch((err) => console.log('bookmarks table or card err', err))