≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

利用Vue.js实现求职在线之职位查询功能

人气:428 时间:2019-04-07

这篇文章主要为大家详细介绍了利用Vue.js实现求职在线之职位查询功能,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

 

前言

 

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源码

 

总结

 

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如有疑问大家可以留言交流,谢谢大家对四海网的支持。

本文来自:http://www.q1010.com/184/3335-0.html

注:关于利用Vue.js实现求职在线之职位查询功能的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • vue2项目使用sass的示例代码
  • 分析vue项目构建与实战
  • 分析Vue.js搭建路由报错 router.map is not a function
  • Vue.js基础指令实例讲解(各种数据绑定、表单渲染大总结)
  • vue元素实现动画过渡效果
  • 分析vue2父组件传递props异步数据到子组件的问题
  • 在vue-cli脚手架中配置一个vue-router前端路由
  • 分析vue中computed 和 watch的异同
  • Vue.js列表渲染绑定jQuery插件的正确姿势
  • Vue和Bootstrap的整合思路分析
上一篇:Vue.js实例方法之生命周期分析
下一篇:在vue-cli脚手架中配置一个vue-router前端路由
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。