前言
Vue.js是當下很火的一個JavaScript MVVM庫,它是以數據驅動和組件化的思想構建的。相比于Angular.js,Vue.js提供了更加簡潔、更易于理解的API,使得我們能夠快速地上手并使用Vue.js。
本文主要介紹的是關于利用Vue.js實現職位查詢功能的相關內容,分享出來供大家參考學習,下面來一起看看詳細的介紹:
知識點:
v-on, v-for, v-if, props, $emit,動態Prop, Class 與 Style 綁定
P1 分頁查詢
查詢參數
查詢參數:公司名稱company, 職位類型type, 月薪范圍salaryMin salaryMax
說明:通過axios.post
攜帶參數發出請求,后端采取分頁查詢的方式向前臺返回指定條數的數據。主要利用MongoDB Limit()
限制讀取的記錄條數, Skip()
跳過指定數量的數據,數據量很小1w+。
// 分頁exports.pageQuery = function (page, pageSize, Model, populate, queryParams, projection, sortParams, callback) { var start = (page - 1) * pageSize; // 根據 page 和 pageSize 得到 skip 要跳過的記錄量 var $page = { pageNumber: page }; async.parallel({ count: function (done) { // 查詢到總共有count條數據 Model.count(queryParams).exec(function (err, count) { done(err, count); }); }, records: function (done) { // 查詢得到排序和排除字段的記錄 Model.find(queryParams, projection).skip(start).limit(pageSize).populate(populate).sort(sortParams).exec(function (err, doc) { done(err, doc); }); } }, function (err, results) { var list = new Array(); for (let item of results.records) { list.push(item.toObject()) } var count = results.count; $page.pageCount = parseInt((count - 1) / pageSize + 1); // 總頁數 $page.results = list; // 單頁結果 $page.count = count; // 總記錄量 callback(err, $page); });};
有了分頁函數,查詢工作函數只要傳入參數即可.
關于MongoDB的模糊查詢
// 數據庫命令,就是個正則表達式: / 參數 /db.getCollection('jobs').find({company: /網易/})// js里如果直接寫 /data.company/會是個字符串,Model.find({})函數識別不了,只能用 new RegExp()company: new RegExp(data.company)
// 查詢工作exports.findJobs = function (data, cb) { let searchItem = { company: new RegExp(data.company), type: new RegExp(data.type), money: { $gte: data.salaryMin, $lte: data.salaryMax } } for (let item in searchItem) { // 若條件為空則刪除該屬性 if (searchItem[item] === '//') { delete searchItem[item] } } var page = data.page || 1 this.pageQuery(page, PAGE_SIZE, Job, '', searchItem, {_id: 0, __v: 0}, { money: 'asc' }, function (error, data) { ... })}
P2 展示查詢結果
查詢結果
數據結構
說明:查詢到的數據結果是對象數組,通過嵌套使用v-for輕松實現內容的展示
// html<div class="searchResult"> <table class="table table-hover"> <tbody class="jobList"> <tr> <th v-for="item in title">{{ item }}</th> </tr> <tr v-for="(item, index) in searchResults" @click="showDesc(index)"> <td v-for="value in item">{{ value }}</td> </tr> </tbody> </table></div>
// onSubmit()Axios.post('http://localhost:3000/api/searchJobs', searchData).then(res => { this.searchResults = res.data.results // 單頁查詢結果 this.page.count = res.data.pageCount // 總頁數 console.log('總頁數' + this.page.count) // 總數據量 ...}).catch(err => { console.log(err)})
P3 詳情卡片
詳情卡片
說明: 通過點擊單行數據顯示自定義的詳情框組件DescMsg來展示具體內容。
組成: 遮罩 + 內容框
思路: 點擊父組件 SearchJob 中的單行數據,通過 props 向子組件 DescMsg傳遞選中行的數據 jobDesc 和 showMsg: true 顯示子組件。點擊子組件 DescMsg 除詳情框外的其余部分,使用 $emit('hideMsg')
觸發關閉詳情頁事件,父組件在使用子組件的地方直接用 v-on 來監聽子組件觸發的事件,設置 showMsg: false
關閉詳情頁。
// 父組件中使用 DescMsg<DescMsg :jobDesc="jobDesc" :showMsg="showMsg" v-on:hideMsg="hideJobDesc"></DescMsg>
// 顯示詳情框showDesc (index) { let item = this.searchResults[index] this.jobDesc = [ { title: '標題', value: item.posname }, { title: '公司', value: item.company }, { title: '月薪', value: item.money }, { title: '地點', value: item.area }, { title: '發布時間', value: item.pubdate }, { title: '最低學歷', value: item.edu }, { title: '工作經驗', value: item.exp }, { title: '詳情', value: item.desc }, { title: '福利', value: item.welfare }, { title: '職位類別', value: item.type }, { title: '招聘人數', value: item.count } ] this.showMsg = true},// 關閉詳情框hideJobDesc () { this.showMsg = false}
// 子組件 DescMsg<template> <div class="wrapper" v-if="showMsg"> <div class="shade" @click="hideShade"></div> <div class="msgBox"> <h4 class="msgTitle">詳情介紹</h4> <table class="table table-hover"> <tbody class="jobList"> <tr v-for="item in jobDesc" :key="item.id"> <td class="title">{{ item.title }}</td> <td class="ctn">{{ item.value }}</td> </tr> </tbody> </table> <div class="ft"> <button type="button" class="btn btn-primary" @click="fllow">關注</button> </div> </div> </div></template><script>export default { data () { return { } }, props: { jobDesc: { type: Array, default: [] }, showMsg: { type: Boolean, default: false } }, methods: { hideShade () { this.$emit('hideMsg') }, fllow () { alert('1') } }}</script>
P4 頁號
頁號
說明: 根據查詢得到的總頁數 count,規定一次最多顯示10個頁號。
思路: 通過v-for渲染頁號,即v-for="(item, index) of pageList"
,并為每個li綁定Class 即 :class="{active: item.active}
。當頁數大于10時,點擊大于6的第n個頁號時,頁數整體向右移動1,否則整體向左移動1。為點擊某一頁數后item.active = true
,該頁數添加樣式.active
。
html
<!-- 底部頁號欄 --><div class="pageButtons"> <nav aria-label="Page navigation"> <ul class="pagination"> <li :class="{disabled: minPage}"> <a aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <li v-for="(item, index) of pageList" :class="{active: item.active}"> <a @click="onSubmit(index)">{{ item.value }}</a> </li> <li :class="{disabled: maxPage}"> <a aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </ul> </nav></div>
js
export default { data () { return { page: { selected: 0, // 選中頁數 count: 0, // 總頁數 size: 10 // 最大顯示頁數 }, pageList: [ {active: false, value: 1} // 默認包含頁數1 ] } }, methods: { // index 代表從左到開始第index個頁號,好吧我也搞混了,最多10個 onSubmit (index) { if (index === -1) { // index為-1代表直接點擊查詢按鈕觸發的事件,初始化數據 index = 0 this.page.selected = 0 this.pageList = [ {active: false, value: 1} ] } Axios.post('http://localhost:3000/api/searchJobs', searchData) .then(res => { this.page.count = res.data.pageCount // 總頁數 let pageNumber = 1 // 默認第1頁 // 若index >= 6且顯示的最后一個頁號小于總頁數,則整體向后移動1,選中的頁號相應向左移動1,即index-- if (index >= 6 && (this.page.count - this.pageList[9].value) > 0) { pageNumber = this.pageList[1].value index-- } else if (index < 6 && this.pageList[0].value !== 1) { pageNumber = this.pageList[0].value - 1 index++ } this.pageList = [] // 初始化 pageList,之后會重新渲染 this.page.size = (this.page.count > 10) ? 10 : this.page.count for (let i = 0; i < this.page.size; i++) { let item = { active: false, value: pageNumber } pageNumber++ this.pageList.push(item) } // 改變當前選中頁號下標樣式,index 代表從左到開始第index個頁號,最多10個 this.pageList[this.page.selected].active = false this.pageList[index].active = true this.page.selected = index console.log(searchData.page) }) .catch(err => { console.log(err) }) } }}
源碼下載地址:Github源碼
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如有疑問大家可以留言交流,謝謝大家對武林網的支持。
新聞熱點
疑難解答