Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
training-dhtd2019-mysql
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Phạm Văn Đoan
training-dhtd2019-mysql
Commits
55d65215
Commit
55d65215
authored
Dec 16, 2019
by
Phạm Văn Đoan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
phân trang ajax
parent
edc4bf49
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
245 additions
and
47 deletions
+245
-47
consts.js
config/consts.js
+1
-0
utils.js
libs/utils.js
+97
-0
task.controller.js
modules/task/controllers/task.controller.js
+40
-36
task.model.js
modules/task/models/task.model.js
+41
-4
pagination.css
public/stylesheets/pagination.css
+35
-0
index.ejs
views/pages/task/index.ejs
+18
-4
ajax_list.ejs
views/pages/task/partials/ajax_list.ejs
+11
-3
head.ejs
views/partials/head.ejs
+2
-0
No files found.
config/consts.js
View file @
55d65215
'use strict'
;
'use strict'
;
module
.
exports
=
{
module
.
exports
=
{
PAGE_SIZE
:
10
,
DB_TABLE
:
{
DB_TABLE
:
{
TASKS
:
'tasks'
TASKS
:
'tasks'
},
},
...
...
libs/utils.js
View file @
55d65215
...
@@ -16,4 +16,100 @@ exports.getStatusName = function (code) {
...
@@ -16,4 +16,100 @@ exports.getStatusName = function (code) {
}
else
{
}
else
{
return
''
;
return
''
;
}
}
}
/**
* Hàm tạo link phân trang ở danh sách
*
* @param $totalRecord
* @param $pageSize
* @param $pageIndex
* @param $paginationName
* @returns {string}
*/
exports
.
genPaginationLink
=
function
(
$totalRecord
,
$pageSize
=
10
,
$pageIndex
=
1
,
$paginationName
=
'changePagination'
)
{
var
$pagination
=
""
;
if
(
$pageIndex
<
1
)
$pageIndex
=
1
;
if
(
$pageSize
>=
1
&&
$pageIndex
>=
1
)
{
var
$adjacents
=
"2"
;
var
$prev
=
$pageIndex
-
1
;
var
$next
=
$pageIndex
+
1
;
var
$lastpage
=
Math
.
ceil
(
$totalRecord
/
$pageSize
);
var
$lpm1
=
$lastpage
-
1
;
}
else
{
return
$pagination
;
}
var
$counter
=
1
;
if
(
$lastpage
>
1
)
{
$pagination
+=
"<div class='pagination'>"
;
if
(
$pageIndex
>
1
)
{
$pagination
+=
"<a href=
\"
#page="
+
(
$prev
)
+
"
\"
onClick='"
+
$paginationName
+
"("
+
(
$prev
)
+
");'>« </a>"
;
}
else
{
$pagination
+=
"<span class='disabled'>« </span>"
;
}
if
(
$lastpage
<
7
+
(
$adjacents
*
2
))
{
for
(
$counter
=
1
;
$counter
<=
$lastpage
;
$counter
++
)
{
if
(
$counter
==
$pageIndex
)
{
$pagination
+=
"<span class='current'>"
+
$counter
+
"</span>"
;
}
else
{
$pagination
+=
"<a href=
\"
#page="
+
(
$counter
)
+
"
\"
onClick='"
+
$paginationName
+
"("
+
(
$counter
)
+
");'>"
+
$counter
+
"</a>"
;
}
}
}
else
if
(
$lastpage
>
5
+
(
$adjacents
*
2
))
{
if
(
$pageIndex
<
1
+
(
$adjacents
*
2
))
{
for
(
$counter
=
1
;
$counter
<
4
+
(
$adjacents
*
2
);
$counter
++
)
{
if
(
$counter
==
$pageIndex
)
{
$pagination
+=
"<span class='current'>"
+
$counter
+
"</span>"
;
}
else
{
$pagination
+=
"<a href=
\"
#page="
+
(
$counter
)
+
"
\"
onClick='"
+
$paginationName
+
"("
+
(
$counter
)
+
");'>"
+
$counter
+
"</a>"
;
}
}
$pagination
+=
"..."
;
$pagination
+=
"<a href=
\"
#page="
+
(
$lpm1
)
+
"
\"
onClick='"
+
$paginationName
+
"("
+
(
$lpm1
)
+
");'>$lpm1</a>"
;
$pagination
+=
"<a href=
\"
#page="
+
(
$lastpage
)
+
"
\"
onClick='"
+
$paginationName
+
"("
+
(
$lastpage
)
+
");'>"
+
$lastpage
+
"</a>"
;
}
else
if
(
$lastpage
-
(
$adjacents
*
2
)
>
$pageIndex
&&
$pageIndex
>
(
$adjacents
*
2
))
{
$pagination
+=
"<a href=
\"
#page=
\"
1
\"\"
onClick='"
+
$paginationName
+
"(1);'>1</a>"
;
$pagination
+=
"<a href=
\"
#page=
\"
2
\"\"
onClick='"
+
$paginationName
+
"(2);'>2</a>"
;
$pagination
+=
"..."
;
for
(
$counter
=
$pageIndex
-
$adjacents
;
$counter
<=
$pageIndex
+
$adjacents
;
$counter
++
)
{
if
(
$counter
==
$pageIndex
)
{
$pagination
+=
"<span class='current'>"
+
$counter
+
"</span>"
;
}
else
{
$pagination
+=
"<a href=
\"
#page="
+
(
$counter
)
+
"
\"
onClick='"
+
$paginationName
+
"("
+
(
$counter
)
+
");'>"
+
$counter
+
"</a>"
;
}
}
$pagination
+=
".."
;
$pagination
+=
"<a href=
\"
#page="
+
(
$lpm1
)
+
"
\"
onClick='"
+
$paginationName
+
"("
+
(
$lpm1
)
+
");'>"
+
$lpm1
+
"</a>"
;
$pagination
+=
"<a href=
\"
#page="
+
(
$lastpage
)
+
"
\"
onClick='"
+
$paginationName
+
"("
+
(
$lastpage
)
+
");'>"
+
$lastpage
+
"</a>"
;
}
else
{
$pagination
+=
"<a href=
\"
#page=
\"
1
\"\"
onClick='"
+
$paginationName
+
"(1);'>1</a>"
;
$pagination
+=
"<a href=
\"
#page=
\"
2
\"\"
onClick='"
+
$paginationName
+
"(2);'>2</a>"
;
$pagination
+=
".."
;
for
(
$counter
=
$lastpage
-
(
2
+
(
$adjacents
*
2
));
$counter
<=
$lastpage
;
$counter
++
)
{
if
(
$counter
==
$pageIndex
)
{
$pagination
+=
"<span class='current'>"
+
$counter
+
"</span>"
;
}
else
{
$pagination
+=
"<a href=
\"
#page="
+
(
$counter
)
+
"
\"
onClick='"
+
$paginationName
+
"("
+
(
$counter
)
+
");'>"
+
$counter
+
"</a>"
;
}
}
}
}
if
(
$pageIndex
<
$counter
-
1
)
{
$pagination
+=
"<a href=
\"
#page="
+
(
$next
)
+
"
\"
onClick='"
+
$paginationName
+
"("
+
(
$next
)
+
");'> »</a>"
;
}
else
{
$pagination
+=
"<span class='disabled'> »</span>"
;
}
$pagination
+=
"</div><p>(Tổng số kết quả: "
+
$totalRecord
+
")</p>"
;
}
return
$pagination
;
}
}
\ No newline at end of file
modules/task/controllers/task.controller.js
View file @
55d65215
...
@@ -3,37 +3,29 @@
...
@@ -3,37 +3,29 @@
var
async
=
require
(
'async'
);
var
async
=
require
(
'async'
);
var
moment
=
require
(
'moment'
);
var
moment
=
require
(
'moment'
);
var
utils
=
require
(
__libs_path
+
'/utils'
);
var
utils
=
require
(
__libs_path
+
'/utils'
);
var
consts
=
require
(
__config_path
+
'/consts'
);
var
Task
=
require
(
'../models/task.model'
);
var
Task
=
require
(
'../models/task.model'
);
/**
* Hàm hiển thị form ds task
*
* @param req
* @param res
* @param next
*/
exports
.
showList
=
function
(
req
,
res
,
next
)
{
exports
.
showList
=
function
(
req
,
res
,
next
)
{
var
skip
=
req
.
body
.
skip
||
0
;
res
.
render
(
'pages/task/index'
,
{
var
limit
=
req
.
body
.
limit
||
10
;
title
:
'Task list'
var
key_word
=
''
;
var
input
=
{
key_word
:
key_word
,
skip
:
skip
,
limit
:
limit
};
async
.
waterfall
([
function
(
cb
)
{
Task
.
getListing
(
input
,
function
(
err
,
res
)
{
if
(
err
)
return
cb
(
err
);
cb
(
null
,
res
);
})
}
],
function
(
err
,
result
)
{
res
.
render
(
'pages/task/index'
,
{
title
:
'Task list'
,
data
:
result
,
moment
:
moment
,
getPriorityName
:
utils
.
getPriorityName
,
getStatusName
:
utils
.
getStatusName
,
});
});
});
}
}
/**
* Hàm xử lý thêm mới một task
*
* @param req
* @param res
* @param next
*/
exports
.
store
=
function
(
req
,
res
,
next
)
{
exports
.
store
=
function
(
req
,
res
,
next
)
{
var
name
=
req
.
body
.
name
;
var
name
=
req
.
body
.
name
;
var
priority
=
req
.
body
.
priority
;
var
priority
=
req
.
body
.
priority
;
...
@@ -59,43 +51,56 @@ exports.store = function (req, res, next) {
...
@@ -59,43 +51,56 @@ exports.store = function (req, res, next) {
});
});
}
}
/**
* Hàm xử lý tìm kiếm theo ajax
*
* @param req
* @param res
* @param next
*/
exports
.
ajaxList
=
function
(
req
,
res
,
next
)
{
exports
.
ajaxList
=
function
(
req
,
res
,
next
)
{
var
key_word
=
req
.
body
.
key_word
;
var
key_word
=
req
.
body
.
key_word
;
var
priority
=
parseInt
(
req
.
body
.
priority
||
-
1
);
var
priority
=
parseInt
(
req
.
body
.
priority
||
-
1
);
var
status
=
parseInt
(
req
.
body
.
status
||
-
1
);
var
status
=
parseInt
(
req
.
body
.
status
||
-
1
);
var
page_index
=
parseInt
(
req
.
body
.
page_index
||
1
);
var
input
=
{
var
input
=
{
key_word
:
key_word
,
key_word
:
key_word
,
priority
:
priority
,
priority
:
priority
,
status
:
status
,
status
:
status
,
skip
:
0
,
page_index
:
page_index
limit
:
10
};
};
async
.
waterfall
([
async
.
waterfall
([
function
(
cb
)
{
function
(
cb
)
{
Task
.
getTotal
(
input
,
function
(
err
,
res
)
{
if
(
err
)
return
cb
(
err
);
if
(
res
[
0
].
hasOwnProperty
(
'total_record'
))
{
cb
(
null
,
res
[
0
].
total_record
);
}
else
{
cb
(
null
,
0
);
}
})
},
function
(
total_record
,
cb
)
{
Task
.
getListing
(
input
,
function
(
err
,
res
)
{
Task
.
getListing
(
input
,
function
(
err
,
res
)
{
if
(
err
)
return
cb
(
err
);
if
(
err
)
return
cb
(
err
);
cb
(
null
,
res
);
cb
(
null
,
{
res
:
res
,
total_record
:
total_record
}
);
})
})
}
}
],
function
(
err
,
result
)
{
],
function
(
err
,
result
)
{
res
.
render
(
'pages/task/partials/ajax_list'
,
{
res
.
render
(
'pages/task/partials/ajax_list'
,
{
data
:
result
,
data
:
result
.
res
,
pagination
:
utils
.
genPaginationLink
(
result
.
total_record
,
consts
.
PAGE_SIZE
,
page_index
),
order_number
:
(
page_index
-
1
)
*
consts
.
PAGE_SIZE
,
moment
:
moment
,
moment
:
moment
,
getPriorityName
:
utils
.
getPriorityName
,
getPriorityName
:
utils
.
getPriorityName
,
getStatusName
:
utils
.
getStatusName
getStatusName
:
utils
.
getStatusName
});
});
});
});
}
}
exports
.
delete
=
function
(
req
,
res
,
next
)
{
exports
.
delete
=
function
(
req
,
res
,
next
)
{
console
.
log
(
req
.
body
.
id
);
console
.
log
(
req
.
body
.
id
);
}
}
\ No newline at end of file
modules/task/models/task.model.js
View file @
55d65215
...
@@ -4,8 +4,16 @@ var db = require(__config_path + '/db');
...
@@ -4,8 +4,16 @@ var db = require(__config_path + '/db');
var
consts
=
require
(
__config_path
+
'/consts'
);
var
consts
=
require
(
__config_path
+
'/consts'
);
var
moment
=
require
(
'moment'
);
var
moment
=
require
(
'moment'
);
/**
* Hàm lấy ds phân trang
*
* @param input
* @param callback
*/
exports
.
getListing
=
function
(
input
,
callback
)
{
exports
.
getListing
=
function
(
input
,
callback
)
{
console
.
log
(
input
);
if
(
input
.
page_index
<
1
)
input
.
page_index
=
1
;
var
limit
=
consts
.
PAGE_SIZE
;
var
offset
=
(
input
.
page_index
-
1
)
*
limit
;
let
sql
=
'SELECT T.* FROM '
+
consts
.
DB_TABLE
.
TASKS
+
' T WHERE T.id > 0 '
;
let
sql
=
'SELECT T.* FROM '
+
consts
.
DB_TABLE
.
TASKS
+
' T WHERE T.id > 0 '
;
...
@@ -23,14 +31,43 @@ exports.getListing = function (input, callback) {
...
@@ -23,14 +31,43 @@ exports.getListing = function (input, callback) {
}
}
sql
+=
' ORDER BY created_at DESC '
;
sql
+=
' ORDER BY created_at DESC '
;
sql
+=
' LIMIT '
+
input
.
skip
+
','
+
input
.
limit
;
sql
+=
' LIMIT '
+
offset
+
','
+
limit
;
var
ss
=
db
.
query
(
sql
,
[],
callback
);
db
.
query
(
sql
,
[],
callback
);
console
.
log
(
ss
.
sql
);
}
/**
* Hàm lấy tổng số bàn ghi theo đk tìm kiếm
*
* @param input
* @param callback
*/
exports
.
getTotal
=
function
(
input
,
callback
)
{
var
sql
=
'SELECT COUNT(T.id) AS total_record FROM '
+
consts
.
DB_TABLE
.
TASKS
+
' T WHERE T.id > 0 '
;
if
(
input
.
key_word
!=
''
)
{
sql
+=
' AND (T.name LIKE "%'
+
input
.
key_word
+
'%" '
;
sql
+=
' OR T.description LIKE "%'
+
input
.
key_word
+
'%") '
;
}
if
(
input
.
priority
!=
-
1
)
{
sql
+=
' AND T.priority = '
+
input
.
priority
;
}
if
(
input
.
status
!=
-
1
)
{
sql
+=
' AND T.status = '
+
input
.
status
;
}
db
.
query
(
sql
,
[],
callback
);
}
}
/**
* Hàm thêm mới 1 task
*
* @param data
* @param callback
*/
exports
.
store
=
function
(
data
,
callback
)
{
exports
.
store
=
function
(
data
,
callback
)
{
let
sql
=
'INSERT INTO '
+
consts
.
DB_TABLE
.
TASKS
+
' SET ?'
;
let
sql
=
'INSERT INTO '
+
consts
.
DB_TABLE
.
TASKS
+
' SET ?'
;
db
.
query
(
sql
,
{
db
.
query
(
sql
,
{
...
...
public/stylesheets/pagination.css
0 → 100644
View file @
55d65215
div
.pagination
{
padding
:
3px
;
margin
:
3px
;
text-align
:
center
;
}
div
.pagination
a
{
padding
:
2px
5px
2px
5px
;
margin
:
2px
;
border
:
1px
solid
#3c8dbc
;
text-decoration
:
none
;
/* no underline: #00908c */
color
:
#3c8dbc
;
}
div
.pagination
a
:hover
,
div
.digg
a
:active
{
border
:
1px
solid
#3c8dbc
;
color
:
#000
;
}
div
.pagination
span
.current
{
padding
:
2px
5px
2px
5px
;
margin
:
2px
;
border
:
1px
solid
#3c8dbc
;
font-weight
:
bold
;
background-color
:
#3c8dbc
;
color
:
#FFF
;
}
div
.pagination
span
.disabled
{
padding
:
2px
5px
2px
5px
;
margin
:
2px
;
border
:
1px
solid
#EEE
;
color
:
#DDD
;
}
views/pages/task/index.ejs
View file @
55d65215
...
@@ -12,6 +12,8 @@
...
@@ -12,6 +12,8 @@
<script>
<script>
$(document).ready(function () {
$(document).ready(function () {
$('#data-loading').hide();
var url_ajax = '/task/ajax-list';
var url_ajax = '/task/ajax-list';
filterBySelectBox('task_priority', url_ajax);
filterBySelectBox('task_priority', url_ajax);
...
@@ -19,22 +21,31 @@
...
@@ -19,22 +21,31 @@
var oldTimeout = '';
var oldTimeout = '';
$('#task_key_word').keyup(function(){
$('#task_key_word').keyup(function(){
$('#data-loading').show();
clearTimeout(oldTimeout);
clearTimeout(oldTimeout);
oldTimeout = setTimeout(function(){
oldTimeout = setTimeout(function(){
getPageByAjax(url_ajax,
0
);
getPageByAjax(url_ajax,
1
);
}, 250);
}, 250);
});
});
getPageByAjax(url_ajax, 0
);
changePagination(1
);
});
});
function filterBySelectBox(id, url_ajax){
function filterBySelectBox(id, url_ajax){
$('#'+id).change(function(){
$('#'+id).change(function(){
getPageByAjax(url_ajax,
0
);
getPageByAjax(url_ajax,
1
);
});
});
}
}
function changePagination(pageId) {
var url_ajax = '/task/ajax-list';
getPageByAjax(url_ajax, pageId);
}
function getPageByAjax(url_ajax, pageId) {
function getPageByAjax(url_ajax, pageId) {
$('#data-loading').show();
var key_word = $('#task_key_word').val();
var key_word = $('#task_key_word').val();
var priority = $('#task_priority').val();
var priority = $('#task_priority').val();
var status = $('#task_status').val();
var status = $('#task_status').val();
...
@@ -47,12 +58,15 @@
...
@@ -47,12 +58,15 @@
priority: priority,
priority: priority,
status: status,
status: status,
skip: 0,
skip: 0,
limit: 10
limit: 10,
page_index: pageId
},
},
success: function(result) {
success: function(result) {
$('#ajax_list').html(result);
$('#ajax_list').html(result);
$('#data-loading').hide();
},
},
error: function (jqXhr, textStatus, errorMessage) {}
error: function (jqXhr, textStatus, errorMessage) {}
});
});
}
}
</script>
</script>
views/pages/task/partials/ajax_list.ejs
View file @
55d65215
<div style="text-align: center; margin-bottom: 10px" id="data-loading">
<i class="fa fa-refresh fa-spin" style="font-size: 30px;"></i> Đang tải dữ liệu...
</div>
<table class="table table-striped table-bordered">
<table class="table table-striped table-bordered">
<thead class="thead-dark">
<thead class="thead-dark">
<tr>
<tr>
<th class="text-center" scope="col">TT</th>
<th class="text-center" scope="col">#ID</th>
<th class="text-center" scope="col">#ID</th>
<th class="text-center" scope="col">Tên công việc</th>
<th class="text-center" scope="col">Tên công việc</th>
<th class="text-center" scope="col">Độ ưu tiên</th>
<th class="text-center" scope="col">Độ ưu tiên</th>
...
@@ -11,12 +15,15 @@
...
@@ -11,12 +15,15 @@
</thead>
</thead>
<tbody>
<tbody>
<% if (data.length > 0) { %>
<% if (data.length > 0) { %>
<% var index=0; %>
<% data.forEach(function(item){ %>
<% data.forEach(function(item){ %>
<% index++; %>
<tr>
<tr>
<td class="text-center" scope="row"><%= (order_number + index) %></td>
<td class="text-center" scope="row"><%= item.id %></td>
<td class="text-center" scope="row"><%= item.id %></td>
<td><%= item.name %></td>
<td><%= item.name %></td>
<td><%= getPriorityName(item.priority) %></td>
<td><%= getPriorityName(item.priority) %></td>
<td>
<td
class="text-center"
>
<% if (item.status == 0) { %>
<% if (item.status == 0) { %>
<i class="fa fa-edit" style="color: red; font-size: 20px"></i>
<i class="fa fa-edit" style="color: red; font-size: 20px"></i>
<% } else { %>
<% } else { %>
...
@@ -43,4 +50,5 @@
...
@@ -43,4 +50,5 @@
</tr>
</tr>
<% } %>
<% } %>
</tbody>
</tbody>
</table>
</table>
\ No newline at end of file
<div class="text-center"><%- pagination %></div>
\ No newline at end of file
views/partials/head.ejs
View file @
55d65215
...
@@ -7,4 +7,5 @@
...
@@ -7,4 +7,5 @@
<link href="/bootstrap_4.3.1/css/bootstrap.min.css" rel="stylesheet">
<link href="/bootstrap_4.3.1/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
<link href="/bootstrap_4.3.1/css/jumbotron.css" rel="stylesheet">
<link href="/bootstrap_4.3.1/css/jumbotron.css" rel="stylesheet">
<link href="/stylesheets/pagination.css" rel="stylesheet">
<script src="/javascripts/jquery-3.3.1.min.js"></script>
<script src="/javascripts/jquery-3.3.1.min.js"></script>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment