显示全部的表格可以按照点击次数,增加时间,最后点击排序

This commit is contained in:
luchenqun 2017-03-15 21:14:01 +08:00
parent 45411cd86b
commit 1c22f88085
6 changed files with 124 additions and 87 deletions

View File

@ -523,42 +523,44 @@ db.getBookmarksTable = function(params) {
params.currentPage = params.currentPage || 1;
params.perPageItems = params.perPageItems || 20;
var sql = "SELECT id, user_id, title, description, url, public, click_count, DATE_FORMAT(created_at, ";
if (params.showStyle == 'card') {
sql += "'%Y-%m-%d %H:%i:%s'";
} else {
sql += "'%Y-%m-%d'";
}
sql += ") as created_at, DATE_FORMAT(last_click, '%Y-%m-%d') as last_click FROM `bookmarks` WHERE 1=1";
var sql = "SELECT id, user_id, title, description, url, public, click_count, DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s') as created_at, DATE_FORMAT(last_click, '%Y-%m-%d %H:%i:%s') as last_click FROM `bookmarks` WHERE 1=1";
if (user_id) {
sql += " AND `user_id` = '" + user_id + "'";
if (params.showStyle == 'card') {
sql += " ORDER BY bookmarks.created_at DESC, bookmarks.click_count DESC";
} else {
sql += " ORDER BY bookmarks.click_count DESC, bookmarks.created_at DESC";
}
}
return new Promise(function(resolve, reject) {
client.query(sql, (err, result) => {
if (err) {
reject(err);
} else {
sql += " LIMIT " + (params.currentPage - 1) * params.perPageItems + ", " + params.perPageItems;
var totalItems = result.length;
console.log(totalItems, sql);
client.query(sql, (err, result) => {
if (err) {
reject(err);
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: totalItems,
bookmarks: result,
totalItems: result.length,
bookmarksClickCount: bookmarksClickCount,
bookmarksCreatedAt: bookmarksCreatedAt,
bookmarksLatestClick: bookmarksLatestClick,
}
resolve(bookmarksData);
}
});
}
});
})
}

View File

@ -14,6 +14,9 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', '
$scope.inputPage = '';
$scope.loadBusy = false;
$scope.waitDelBookmark = {};
$scope.order = [false, false, false];
$scope.order[($stateParams && $stateParams.orderIndex) || 0] = true;
$scope.bookmarkData = {};
$scope.changeCurrentPage = function(currentPage) {
currentPage = parseInt(currentPage) || 0;
@ -163,6 +166,21 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', '
}
}
$scope.changeOrder = function(index) {
if (index < 0 || index >= $scope.order.length) {
return;
}
$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;
}
}
pubSubService.subscribe('EditCtr.inserBookmarsSuccess', $scope, function(event, data) {
console.log('subscribe EditCtr.inserBookmarsSuccess', params);
@ -183,20 +201,29 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', '
params.currentPage = $scope.currentPage;
params.perPageItems = perPageItems;
}
var sendData = {
totalItems: 0,
bookmarksClickCount: [],
bookmarksCreatedAt: [],
bookmarksLatestClick: [],
}
bookmarkService.getBookmarks(params)
.then((data) => {
if (params.showStyle != 'navigate') {
$scope.bookmarkData = data;
$scope.totalPages = Math.ceil(data.totalItems / perPageItems);
if (data.totalItems == 0) {
toastr.info('您还没有书签,请点击菜单栏的添加按钮进行添加', "提示");
}
if (params.showStyle == 'card') {
data.bookmarks.forEach(bookmark => {
data.bookmarksCreatedAt.forEach(bookmark => {
$scope.bookmarks.push(bookmark);
})
$scope.loadBusy = false;
} else {
$scope.bookmarks = data.bookmarks;
$scope.changeOrder($scope.order.indexOf(true));
}
} else {
$scope.bookmarks = data;
@ -243,7 +270,6 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', '
}
function animation() {
var data = ['scale', 'fade', 'fade up', 'fade down', 'fade left', 'fade right', 'horizontal flip',
'vertical flip', 'drop', 'fly left', 'fly right', 'fly up', 'fly down', 'swing left', 'swing right', 'swing up',
'swing down', 'browse', 'browse right', 'slide down', 'slide up', 'slide left', 'slide right'

View File

@ -29,6 +29,9 @@ app.controller('tagsCtr', ['$scope', '$filter', '$window', '$stateParams', '$tim
});
$scope.changeOrder = function(index) {
if (index < 0 || index >= $scope.order.length) {
return;
}
$scope.order = $scope.order.map(() => false);
$scope.order[index] = true;
if ($scope.order[0]) {
@ -63,13 +66,7 @@ app.controller('tagsCtr', ['$scope', '$filter', '$window', '$stateParams', '$tim
bookmarkService.getBookmarksByTag(params)
.then((data) => {
$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.changeOrder($scope.order.indexOf(true));
$scope.bookmarkCount = $scope.bookmarkData.totalItems;
$scope.totalPages = Math.ceil($scope.bookmarkCount / perPageItems);

View File

@ -50,13 +50,13 @@
<table class="ui selectable sortable celled table js-table-bookmarks" ng-if="showStyle === 'table'">
<thead>
<tr>
<th>标题</th>
<th>链接</th>
<th style="width:90px;">点击次数</th>
<th style="width:100px;">创建日期</th>
<th style="width:100px;">最后点击</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>
@ -71,8 +71,14 @@
<span title="{{bookmark.url}} 点击复制链接" ng-click="copy(bookmark.id, bookmark.url)" id="url{{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>
<span title="{{bookmark.created_at}}">{{ bookmark.created_at.substr(0, 10) }}
</span>
</td>
<td>
<span title="{{bookmark.last_click}}">{{ bookmark.last_click.substr(0, 10) }}
</span>
</td>
<td>
<div class="ui label" ng-repeat="tag in bookmark.tags" tag-id="{{ tag.id }}" ng-click="jumpToTags(tag.id)">
{{ tag.name }}

View File

@ -72,8 +72,14 @@
<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><span title="{{bookmark.created_at}}">{{ bookmark.created_at.substr(0, 10) }} </span></td>
<td><span title="{{bookmark.last_click}}">{{ bookmark.last_click.substr(0, 10) }} </span></td>
<td>
<span title="{{bookmark.created_at}}">{{ bookmark.created_at.substr(0, 10) }}
</span>
</td>
<td>
<span title="{{bookmark.last_click}}">{{ bookmark.last_click.substr(0, 10) }}
</span>
</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

@ -298,20 +298,22 @@ api.get('/bookmarks', function(req, res) {
})
.catch((err) => console.log('bookmarks navigate err', err));
} else {
var bookmarks = [];
var tagsBookmarks = [];
var totalItems = 0;
var totalItems = 0;
var sendData = {
totalItems: totalItems,
bookmarks: []
totalItems: 0,
bookmarksClickCount: [],
bookmarksCreatedAt: [],
bookmarksLatestClick: [],
}
params.userId = userId;
db.getBookmarksTable(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) => {
@ -319,9 +321,9 @@ api.get('/bookmarks', function(req, res) {
return db.getTags(userId);
})
.then((tags) => {
var data = [];
// 获取每个书签的所有分类标签
bookmarks.forEach(function(bookmark) {
var objectName = ['bookmarksClickCount', 'bookmarksCreatedAt', 'bookmarksLatestClick'];
objectName.forEach((name) => {
sendData[name].forEach(function(bookmark, index) {
var bookmarkTags = [];
tagsBookmarks.forEach(function(tb) {
if (tb.bookmark_id == bookmark.id) {
@ -332,11 +334,9 @@ api.get('/bookmarks', 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))