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

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.currentPage = params.currentPage || 1;
params.perPageItems = params.perPageItems || 20; params.perPageItems = params.perPageItems || 20;
var sql = "SELECT id, user_id, title, description, url, public, click_count, DATE_FORMAT(created_at, "; 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 (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";
if (user_id) { if (user_id) {
sql += " AND `user_id` = '" + 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) { return new Promise(function(resolve, reject) {
client.query(sql, (err, result) => { client.query(sql, (err, result) => {
if (err) { if (err) {
reject(err); reject(err);
} else { } else {
sql += " LIMIT " + (params.currentPage - 1) * params.perPageItems + ", " + params.perPageItems; var bookmarksClickCount, bookmarksCreatedAt, bookmarksLatestClick;
var totalItems = result.length; result.sort((a, b) => {
console.log(totalItems, sql); var click1 = parseInt(a.click_count);
client.query(sql, (err, result) => { var click2 = parseInt(b.click_count);
if (err) { if (click1 > click2) {
reject(err); return -1;
} else if (click1 == click2) {
return a.created_at >= b.created_at ? -1 : 1;
} else { } 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 = { var bookmarksData = {
totalItems: totalItems, totalItems: result.length,
bookmarks: result, bookmarksClickCount: bookmarksClickCount,
bookmarksCreatedAt: bookmarksCreatedAt,
bookmarksLatestClick: bookmarksLatestClick,
} }
resolve(bookmarksData); resolve(bookmarksData);
} }
}); });
}
});
}) })
} }

View File

@ -14,6 +14,9 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', '
$scope.inputPage = ''; $scope.inputPage = '';
$scope.loadBusy = false; $scope.loadBusy = false;
$scope.waitDelBookmark = {}; $scope.waitDelBookmark = {};
$scope.order = [false, false, false];
$scope.order[($stateParams && $stateParams.orderIndex) || 0] = true;
$scope.bookmarkData = {};
$scope.changeCurrentPage = function(currentPage) { $scope.changeCurrentPage = function(currentPage) {
currentPage = parseInt(currentPage) || 0; 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) { pubSubService.subscribe('EditCtr.inserBookmarsSuccess', $scope, function(event, data) {
console.log('subscribe EditCtr.inserBookmarsSuccess', params); console.log('subscribe EditCtr.inserBookmarsSuccess', params);
@ -183,20 +201,29 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', '
params.currentPage = $scope.currentPage; params.currentPage = $scope.currentPage;
params.perPageItems = perPageItems; params.perPageItems = perPageItems;
} }
var sendData = {
totalItems: 0,
bookmarksClickCount: [],
bookmarksCreatedAt: [],
bookmarksLatestClick: [],
}
bookmarkService.getBookmarks(params) bookmarkService.getBookmarks(params)
.then((data) => { .then((data) => {
if (params.showStyle != 'navigate') { if (params.showStyle != 'navigate') {
$scope.bookmarkData = data;
$scope.totalPages = Math.ceil(data.totalItems / perPageItems); $scope.totalPages = Math.ceil(data.totalItems / perPageItems);
if (data.totalItems == 0) { if (data.totalItems == 0) {
toastr.info('您还没有书签,请点击菜单栏的添加按钮进行添加', "提示"); toastr.info('您还没有书签,请点击菜单栏的添加按钮进行添加', "提示");
} }
if (params.showStyle == 'card') { if (params.showStyle == 'card') {
data.bookmarks.forEach(bookmark => { data.bookmarksCreatedAt.forEach(bookmark => {
$scope.bookmarks.push(bookmark); $scope.bookmarks.push(bookmark);
}) })
$scope.loadBusy = false; $scope.loadBusy = false;
} else { } else {
$scope.bookmarks = data.bookmarks; $scope.changeOrder($scope.order.indexOf(true));
} }
} else { } else {
$scope.bookmarks = data; $scope.bookmarks = data;
@ -243,7 +270,6 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', '
} }
function animation() { function animation() {
var data = ['scale', 'fade', 'fade up', 'fade down', 'fade left', 'fade right', 'horizontal flip', 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', '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' '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) { $scope.changeOrder = function(index) {
if (index < 0 || index >= $scope.order.length) {
return;
}
$scope.order = $scope.order.map(() => false); $scope.order = $scope.order.map(() => false);
$scope.order[index] = true; $scope.order[index] = true;
if ($scope.order[0]) { if ($scope.order[0]) {
@ -63,13 +66,7 @@ app.controller('tagsCtr', ['$scope', '$filter', '$window', '$stateParams', '$tim
bookmarkService.getBookmarksByTag(params) bookmarkService.getBookmarksByTag(params)
.then((data) => { .then((data) => {
$scope.bookmarkData = data; $scope.bookmarkData = data;
if ($scope.order[0]) { $scope.changeOrder($scope.order.indexOf(true));
$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.bookmarkCount = $scope.bookmarkData.totalItems;
$scope.totalPages = Math.ceil($scope.bookmarkCount / perPageItems); $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'"> <table class="ui selectable sortable celled table js-table-bookmarks" ng-if="showStyle === 'table'">
<thead> <thead>
<tr> <tr>
<th>标题</th> <th style="cursor:default;">标题</th>
<th>链接</th> <th style="cursor:default;">链接</th>
<th style="width:90px;">点击次数</th> <th style="width:90px;" class="sorted" ng-class="{descending: order[0]}" ng-click="changeOrder(0)">点击次数</th>
<th style="width:100px;">创建日期</th> <th style="width:100px;" class="sorted" ng-class="{descending: order[1]}" ng-click="changeOrder(1)">创建日期</th>
<th style="width:100px;">最后点击</th> <th style="width:100px;" class="sorted" ng-class="{descending: order[2]}" ng-click="changeOrder(2)">最后点击</th>
<th style="width:150px;">分类</th> <th style="width:150px;cursor:default;">分类</th>
<th style="width:88px;">操作</th> <th style="width:88px;cursor:default;">操作</th>
</tr> </tr>
</thead> </thead>
<tbody> <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> <span title="{{bookmark.url}} 点击复制链接" ng-click="copy(bookmark.id, bookmark.url)" id="url{{bookmark.id}}" style="cursor:default;">{{ bookmark.url }}</span>
</td> </td>
<td>{{ bookmark.click_count }}</td> <td>{{ bookmark.click_count }}</td>
<td>{{ bookmark.created_at }}</td> <td>
<td>{{ bookmark.last_click }}</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> <td>
<div class="ui label" ng-repeat="tag in bookmark.tags" tag-id="{{ tag.id }}" ng-click="jumpToTags(tag.id)"> <div class="ui label" ng-repeat="tag in bookmark.tags" tag-id="{{ tag.id }}" ng-click="jumpToTags(tag.id)">
{{ tag.name }} {{ 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> <span title="{{bookmark.url}} 点击复制链接" ng-click="copy(bookmark.id, bookmark.url)" id="tagurl{{bookmark.id}}" style="cursor:default;">{{ bookmark.url }}</span>
</td> </td>
<td>{{ bookmark.click_count }}</td> <td>{{ bookmark.click_count }}</td>
<td><span title="{{bookmark.created_at}}">{{ bookmark.created_at.substr(0, 10) }} </span></td> <td>
<td><span title="{{bookmark.last_click}}">{{ bookmark.last_click.substr(0, 10) }} </span></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> <td>
<div class="ui label" ng-repeat="tag in bookmark.tags" tag-id="{{ tag.id }}" ng-click="getBookmarks(tag.id, 1)"> <div class="ui label" ng-repeat="tag in bookmark.tags" tag-id="{{ tag.id }}" ng-click="getBookmarks(tag.id, 1)">
{{ tag.name }} {{ tag.name }}

View File

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