需求描述
分页显示数据,避免造成服务器宕机。
Django:根据pageNum返回数据切片
Views.py写入业务逻辑
# 数据接口:暴露trs_training_and_test_record数据
def api_trs_training_and_test_record(request,myDateS,myDateE,mySystem,category,EmployeeID,courseName,pageNum=1):
print('api_trs_training_and_test_record收到数据请求,参数为',myDateS,myDateE,mySystem,category,EmployeeID,courseName,pageNum)
# ORM取得数据
data_all = trs_training_and_test_record.objects.all()
data_count = trs_training_and_test_record.objects.count()
data_choose = data_all
pageNum = int(pageNum)
# 筛选&转码
if myDateS:
data_choose = data_choose.filter(Start_Date__gte=myDateS)
if myDateE:
data_choose = data_choose.filter(End_Date__lte=myDateE)
if category:
if category == 'training':
data_choose = data_choose.filter(Type='課程')
elif category == 'test':
data_choose = data_choose.filter(Q(Type='考試')|Q(Type='考核'))
elif category == 'All':
pass
if EmployeeID and EmployeeID !='NA':
data_choose = data_choose.filter(Employee_NO=EmployeeID)
if courseName and courseName !='NA':
data_choose = data_choose.filter(Course__icontains=courseName)
# 记录页码
total = data_choose.count()
# 分页,默认10笔数据为一页
if data_choose:
if (pageNum*10 >= 10) and (pageNum*10 <= data_count):
data_choose = data_choose[(pageNum-1)*10:pageNum*10]
elif (pageNum*10 >= 0) and (pageNum*10 > data_count) and (data_count<10):
data_choose = data_choose[0:data_count]
elif (pageNum*10 >= 10) and ((pageNum-1)*10 <= data_count) and (pageNum*10 > data_count):
data_choose = data_choose[(pageNum-1)*10:data_count]
# 初始化data
data = []
# 将数据塞进去data
for each_queryset in data_choose:
temp_data = {
'training_system': 'TRS',
'Location': each_queryset.Location,
'Function_Code': each_queryset.Function_Code,
'Function_Name': each_queryset.Function_Name,
'Plant': each_queryset.Plant,
'Dept': each_queryset.Dept,
'Employee_NO': each_queryset.Employee_NO,
'Name': each_queryset.Name,
'Grade_Range': each_queryset.Grade_Range,
'Training_Type': each_queryset.Training_Type,
'Priority': each_queryset.Priority,
'Type': each_queryset.Type,
'Course': each_queryset.Course,
'Schedule_Code': each_queryset.Schedule_Code,
'Schedule_Name': each_queryset.Schedule_Name,
'Course_or_Test': each_queryset.Course_or_Test,
'Delivery_Way_or_Test_Way': each_queryset.Delivery_Way_or_Test_Way,
'Start_Date': str(each_queryset.Start_Date),
'End_Date': str(each_queryset.End_Date),
'Present_Status': each_queryset.Present_Status,
'Pass_Score': each_queryset.Pass_Score,
'Listening_Score': each_queryset.Listening_Score,
'Reading_Score': each_queryset.Reading_Score,
'Score': each_queryset.Score,
'Pass_YN': each_queryset.Pass_YN,
'Effective_Date': each_queryset.Effective_Date,
'Duration': each_queryset.Duration,
'Venue': each_queryset.Venue,
'Trainer_Assesor': each_queryset.Trainer_Assesor,
},
data.append(temp_data)
# 将data转化为trs_training_and_test_record_data
trs_training_and_test_record_data = {
'data': data,
'pageSize': 10,
'pageNum':pageNum,
'data_count':data_count,
'total':total,
}
print('api_trs_training_and_test_record发送数据')
# 暴露api
return HttpResponse(json.dumps(trs_training_and_test_record_data,ensure_ascii=False,indent=4), content_type='application/json')
urls.py
path('api/api_trs_down_load_training_and_test_record/<myDateS>/<myDateE>/<mySystem>/<category>/<EmployeeID>/<courseName>/', views.trs_down_load_training_and_test_record, name='trs_down_load_training_and_test_record'),
Vue前端
index.vue写入样式
主要逻辑如下:
- 按照系统不同,来使用v-if显示不同系统数据。
- GoQuery(myDate,mySystem,category,EmployeeID,courseName,pageNum)传入pageNum,以便服务器切片返回数据。
- 使用ElementUI中分页组件的回调参数val。@next-click=“handlenextClick”。
- ElementUI中分页组件尽量使用state的数据。:total=TrsTrainingTestData.total
<template>
<div class="container-fluid">
<!-- 筛选栏 -->
<el-row class="tac p-3">
<el-col span="24">
<h3>数据汇总</h3>
<div class="block py-1">
<span class="demonstration">日期筛选</span>
<span class="demonstration"> </span>
<el-date-picker
value-format="yyyy-MM-dd"
v-model="myDate"
type="daterange"
align="right"
unlink-panels
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期"
:picker-options="pickerOptions">
</el-date-picker>
</div>
<div class="block py-1">
<span class="demonstration">训练系统</span>
<span class="demonstration"> </span>
<span>
<el-radio-group v-model="mySystem">
<el-radio-button label="TRS" :mySystem="TRS"></el-radio-button>
<el-radio-button label="CSOD" :mySystem="CSOD"></el-radio-button>
<el-radio-button label="DLT" :mySystem="DLT"></el-radio-button>
</el-radio-group>
</span>
</div>
<div class="block py-1">
<span class="demonstration">关键指标</span>
<span class="demonstration"> </span>
<span>
<el-radio-group v-model="category">
<el-radio-button label="All" :category="All">All</el-radio-button>
<el-radio-button label="training" :category="training">培训数据</el-radio-button>
<el-radio-button label="test" :category="test">考试数据</el-radio-button>
</el-radio-group>
</span>
</div>
<div class="block py-1">
<span class="demonstration">员工工号</span>
<span class="demonstration"> </span>
<span>
<el-input
class="w-25"
placeholder="请输入内容"
v-model="EmployeeID"
clearable>
</el-input>
</span>
</div>
<div class="block py-1">
<span class="demonstration">课程名称</span>
<span class="demonstration"> </span>
<span>
<el-input
class="w-25"
placeholder="请输入内容"
v-model="courseName"
clearable>
</el-input>
</span>
<span class="demonstration"> </span>
<span class="float-left">
<el-button type="info" plain @click="GoQuery(myDate,mySystem,category,EmployeeID,courseName,pageNum)">查询</el-button>
<el-button type="info" plain @click="DownLoadExcel(myDate,mySystem,category,EmployeeID,courseName)">下载</el-button>
</span>
</div>
</el-col>
</el-row>
<!-- TRS -->
<el-row class="tac" v-if="mySystem=='TRS'">
<el-col span="24">
<table class="table table-striped table-sm table-bordered text-center">
<thead>
<tr class="" style="color:White;background-color:#3366FF;font-family:微軟正黑體,Tahoma,Arial,微軟雅黑體;font-size:15px;">
<th scope="col">训练类别</th>
<th scope="col">厂别</th>
<th scope="col">部门代码</th>
<th scope="col">人员工号</th>
<th scope="col">人员姓名</th>
<th scope="col">知识点名称</th>
<th scope="col">培训日期</th>
<th scope="col">时间</th>
<th scope="col">培训地点</th>
<th scope="col">培训讲师</th>
</tr>
</thead>
<!-- TRS -->
<tr v-for="item in TrsTrainingTestData.data" :key="item.Employee_NO" valign="middle" style="color:Black;border-color:#E0E0E0;font-size:15px;">
<td class="col">{{ item[0].training_system }}</td>
<td class="col">{{ item[0].Plant }}</td>
<td class="col">{{ item[0].Dept }}</td>
<td class="col">{{ item[0].Employee_NO }}</td>
<td class="col">{{ item[0].Name }}</td>
<td class="col">{{ item[0].Schedule_Name }}</td>
<td class="col">{{ item[0].Start_Date }}</td>
<td class="col">{{ item[0].Start_Date }}</td>
<td class="col">{{ item[0].Venue }}</td>
<td class="col"></td>
</tr>
</table>
</el-col>
</el-row>
<!-- 页码 -->
<el-row class="tac" v-if="mySystem=='TRS'">
<el-col span="24">
<el-pagination
background
layout="prev, pager, next"
:total=TrsTrainingTestData.total
@current-change="handlenextClick"
@prev-click="handlenextClick"
@next-click="handlenextClick"
>
</el-pagination>
</el-col>
</el-row>
<!-- CSOD -->
<el-row class="tac" v-if="mySystem=='CSOD'">
<el-col span="24">
<table class="table table-striped table-sm table-bordered text-center">
<thead>
<tr class="" style="color:White;background-color:#3366FF;font-family:微軟正黑體,Tahoma,Arial,微軟雅黑體;font-size:15px;">
<th scope="col">训练类别</th>
<th scope="col">厂别</th>
<th scope="col">部门代码</th>
<th scope="col">人员工号</th>
<th scope="col">人员姓名</th>
<th scope="col">知识点名称</th>
<th scope="col">培训日期</th>
<th scope="col">时间</th>
<th scope="col">培训地点</th>
<th scope="col">培训讲师</th>
</tr>
</thead>
<!-- CSOD -->
<tr v-for="item in CsodTrainingTestData.data" :key="item.Employee_NO" valign="middle" style="color:Black;border-color:#E0E0E0;font-size:15px;">
<td class="col">{{ item[0].training_system }}</td>
<td class="col"></td>
<td class="col"></td>
<td class="col">{{ item[0].USER_ID }}</td>
<td class="col">{{ item[0].USER }}</td>
<td class="col">{{ item[0].TRAINING_TITLE }}</td>
<td class="col">{{ item[0].TRAINING_RECORD_DATE }}</td>
<td class="col">{{ item[0].TRAINING_RECORD_DATE }}</td>
<td class="col"></td>
<td class="col">{{ item[0].Instructor_Full_Name }}</td>
</tr>
</table>
</el-col>
</el-row>
<!-- 页码 -->
<el-row class="tac" v-if="mySystem=='CSOD'">
<el-col span="24">
<el-pagination
background
layout="prev, pager, next"
:total=CsodTrainingTestData.total
@current-change="handlenextClick"
@prev-click="handlenextClick"
@next-click="handlenextClick"
>
</el-pagination>
</el-col>
</el-row>
</div>
</template>
<script>
import { mapState } from 'vuex'
export default {
data() {
return {
pageNum:1,
myDate:[],
mySystem:'TRS',
category: 'training',
EmployeeID:'',
courseName:'',
pageNum:1,
pageSize:10,
reqTrsTrainingTestData:{'data':''},
//日期筛选
pickerOptions: {
shortcuts: [{
text: '最近一周',
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
picker.$emit('pick', [start, end]);
}
}, {
text: '最近一个月',
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
picker.$emit('pick', [start, end]);
}
}, {
text: '最近三个月',
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
picker.$emit('pick', [start, end]);
}
}]
},
};
},
mounted() {
},
computed: {
...mapState({
DLTOrientData: (state) => state.home.DLTOrientData,
DLTGeneralData: (state) => state.home.DLTGeneralData,
DLTSkillTrainingData: (state) => state.home.DLTSkillTrainingData,
DLTSkillLevelTrainingData: (state) => state.home.DLTSkillLevelTrainingData,
TrsTrainingTestData: (state) => state.home.TrsTrainingTestData,
TrsTrainerMaintainData: (state) => state.home.TrsTrainerMaintainData,
CsodTrainingTestData: (state) => state.home.CsodTrainingTestData,
}),
},
methods: {
GoQuery(myDate,mySystem,category,EmployeeID,courseName,pageNum){
// 初始化值
var today=new Date();
console.log('today',today)
if (mySystem == 'TRS'){
if(myDate == ''){
//程序bug:today.getMonth()+1表示当前月
myDate = [today.getFullYear()+"-"+(today.getMonth())+"-"+today.getDate(),
today.getFullYear()+"-"+(today.getMonth()+1)+"-"+today.getDate()
];
}
if(EmployeeID == ''){EmployeeID = 'NA'}
if(courseName == ''){courseName = 'NA'}
this.$store.dispatch('getTrsTrainingTestData',
{
"myDateS":myDate[0],
"myDateE":myDate[1],
"mySystem":mySystem,
"category":category,
"EmployeeID":EmployeeID,
"courseName":courseName,
"pageNum":pageNum,
})
}
if (mySystem == 'CSOD'){
if(myDate == ''){
//程序bug:today.getMonth()+1表示当前月
myDate = [today.getFullYear()+"-"+(today.getMonth())+"-"+today.getDate(),
today.getFullYear()+"-"+(today.getMonth()+1)+"-"+today.getDate()
];
}
if(EmployeeID == ''){EmployeeID = 'NA'}
if(courseName == ''){courseName = 'NA'}
this.$store.dispatch('getCsodTrainingTestData',
{
"myDateS":myDate[0],
"myDateE":myDate[1],
"mySystem":mySystem,
"category":category,
"EmployeeID":EmployeeID,
"courseName":courseName,
"pageNum":pageNum,
})
}
if (mySystem == 'DLT'){
this.$store.dispatch('getDLTOrientData',
{
// "myDate":myDate,
"mySystem":mySystem,
"category":category,
"courseName":courseName,
})
}
console.log(123,myDate,mySystem,category,EmployeeID,courseName,pageNum)
},
handlenextClick(val){
//把回调参数val给pageNum
this.GoQuery(this.myDate,this.mySystem,this.category,this.EmployeeID,this.courseName,val)
console.log('当前页码:',val)
},
DownLoadExcel(myDate,mySystem,category,EmployeeID,courseName){
//初始值
var today=new Date();
if(myDate == ''){
//程序bug:today.getMonth()+1表示当前月
myDate = [today.getFullYear()+"-"+(today.getMonth())+"-"+today.getDate(),
today.getFullYear()+"-"+(today.getMonth()+1)+"-"+today.getDate()
];
}
if(EmployeeID == ''){EmployeeID = 'NA'}
if(courseName == ''){courseName = 'NA'}
if (mySystem == 'TRS'){
window.open('http://127.0.0.1:8000/api/api_trs_down_load_training_and_test_record/'+myDate[0]+'/'+myDate[1]+'/'+mySystem+'/'+category+'/'+EmployeeID+'/'+courseName+'/')
}
if (mySystem == 'CSOD'){
window.open('http://127.0.0.1:8000/api/api_csod_down_load_training_and_test_record/'+myDate[0]+'/'+myDate[1]+'/'+mySystem+'/'+category+'/'+EmployeeID+'/'+courseName+'/')
}
console.log('DownLoadExcel',mySystem)
},
},
}
</script>
api请求数据接口
//请求TRS
export const reqTrsTrainingTestData = (dict)=> requests(
{
url:`/api/api_trs_training_and_test_record/${dict["myDateS"]}/${dict["myDateE"]}/${dict["mySystem"]}/${dict["category"]}/${dict["EmployeeID"]}/${dict["courseName"]}/${dict["pageNum"]}/`,
method:'get',
data:dict,
}
);
export const reqCsodTrainingTestData = (dict)=> requests(
{
url:`/api/api_csod_training_and_test_record/${dict["myDateS"]}/${dict["myDateE"]}/${dict["mySystem"]}/${dict["category"]}/${dict["EmployeeID"]}/${dict["courseName"]}/${dict["pageNum"]}/`,
method:'get',
data:dict,
}
);