首页 > 其他分享 >vue+elementUI 实现设置还款日字母弹窗组件

vue+elementUI 实现设置还款日字母弹窗组件

时间:2023-02-17 12:51:03浏览次数:33  
标签:vue sendNum elementUI 还款 组件 import 弹窗 NumberSelect

1、业务背景

还款业务,设置每月还款日,选每月几号扣款,不需要29、30、31,因为不是每个月都有这三天的

2、预期效果图

image.png

3、代码实现

3.1 初始化vue项目

地址:https://cn.vuejs.org/guide/introduction.html

3.2 在项目中应用elementUI

地址:https://element.eleme.io/#/zh-CN/component/installation

3.3 在main.js中引用

import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import router from './router'

Vue.use(ElementUI)

new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

3.4 编写字母按钮组件(完整代码在最后)

<template>
  <div class='box'>
    <span
      class='inner'
      @click='selectClick(index, item)'
      :class="'list_'+index"
      v-for='(item, index) in numberArr'
      :key='index'
      >{{ item }}</span
    >
  </div>
</template>

3.5 引用字母按钮组件(完整代码在最后)

<template>
  <div id="app">
    <button @click="handleNumberClick">点击调用获取还款日组件方法</button>
    <el-dialog
      title="设置还款日"
      append-to-body
      :visible.sync="showDialog"
      width="350px">
      <number-select ref="numberSelect" :showDialog="showDialog" @hideModal="hideModal" />
    </el-dialog>
    <div>设置还款日期为{{this.sendNum}}</div>
  </div>
</template>

4、实际效果图

chrome-capture-2023-1-17.gif

5、完整代码

5.1 设置还款日组件页面 NumberSelect.vue

<!-- 设置还款日组件页面 NumberSelect.vue -->
<template>
  <div class='box'>
    <span
      class='inner'
      @click='selectClick(index, item)'
      :class="'list_'+index"
      v-for='(item, index) in numberArr'
      :key='index'
      >{{ item }}</span
    >
  </div>
</template>

<script>
export default {
  props: {
    sendNumber: Number,
    showDialog: {
      type: Boolean,
      default: false,
      required: true // 必传递
    }
  },
  name: 'numberSelect',
  data () {
    return {
      numberArr: [],
      index: 0
    }
  },
  methods: {
    // 选择按钮
    selectClick (idx, item) {
      this.sendNum = item
      var len = this.numberArr.length
      for (var i = 0; i < len; i++) {
        if (i === idx) {
          document.getElementsByClassName(
            'list_' + i
          )[0].style.backgroundColor = 'skyblue'
        } else {
          document.getElementsByClassName(
            'list_' + i
          )[0].style.backgroundColor = ''
        }
      }
      this.$emit('hideModal')
    }
  },
  // 创建28个字母按钮数组
  created () {
    for (var i = 1; i <= 28; i++) {
      this.numberArr.push(i)
    }
  }
}
</script>

<style>
.box {
  margin: 0 auto;
  background-color: #fff;
  width: 300px;
  height: 240px;
  display: flex;
  flex-wrap: wrap;
}
.inner {
  margin-left: 10px;
  margin-top: 10px;
  text-align: center;
  line-height: 30px;
  width: 30px;
  height: 30px;
  border: 1px solid #000;
  border-radius: 50%;
  background-color: #fff;
  display: inline-block;
  cursor: pointer;
}
</style>

5.2 设置还款日组件页面 NumberSelect.vue

<template>
  <div id="app">
    <button @click="handleNumberClick">点击调用获取还款日组件方法</button>
    <el-dialog
      title="设置还款日"
      append-to-body
      :visible.sync="showDialog"
      width="350px">
      <number-select ref="numberSelect" :showDialog="showDialog" @hideModal="hideModal" />
    </el-dialog>
    <div>设置还款日期为{{this.sendNum}}</div>
  </div>
</template>

<script>
import NumberSelect from './components/NumberSelect.vue'
export default {
  components: { NumberSelect },
  name: 'App',
  data () {
    return {
      showDialog: false, // 是否显示还款日组件
      sendNum: 0 // 还款日组件选中的值
    }
  },
  methods: {
    // 点击设置还款日按钮
    handleNumberClick () {
      this.showDialog = true
    },
    // 关闭弹窗
    hideModal () {
      this.showDialog = false
      console.log('确认的数字为:' + this.$refs.numberSelect.sendNum)
      this.sendNum = this.$refs.numberSelect.sendNum
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

若本文有帮助到阅读本文的同学,欢迎点赞、关注、收藏,互相学习交流。

标签:vue,sendNum,elementUI,还款,组件,import,弹窗,NumberSelect
From: https://blog.51cto.com/u_10040183/6063969

相关文章

  • vue2 - 局部过滤器 全局过滤器,自定义局部指令 自定义全局指令
    1.过滤器局部过滤器<!--使用一个过滤器,单个参数--><div>{{"levi"|filterTest1}}</div><!--使用一个过滤器,多个参数--><inputtype="text"v-bind:......
  • vue2 - vue.set 添加属性与修改属性,修改数组的注意事项
    1.vue.set添加属性与修改属性Vue.set(target,propertyName/index,value)vm.$set(target,propertyName/index,value)<divid="root"><buttonv-on:click="addAttr">点击......
  • vue2 - 条件渲染,列表熏染
    1.条件熏染v-if(1).v-if="表达式"(2).v-else-if="表达式"(3).v-else="表达式"适用于:切换频率较低的场景特点:不展示的DOM元素直接被移除注意:v-if可以和:v-else-if,v-e......
  • vue2和vue3的响应式区别
    vue响应式依赖的函数vue2vue2中依赖es5的Object.defineProperty属性,该方法可以在一个对象上定义一个新属性,或者修改一个对象的现有属性,并返回这个对象。监听的是对象......
  • vue项目打包后,自由修改配置如接口地址等
    背景描述:项目打包后,如果想更换接口域名或者项目名称,就需要再次打包。但是这样子操作有点耗费时间。如果把这些配置写到一个文件中,然后在index.html文件中引入使用,这样子......
  • vue+elementUI 实现设置还款日字母弹窗组件
    1、业务背景还款业务,设置每月还款日,选每月几号扣款,不需要29、30、31,因为不是每个月都有这三天的2、预期效果图3、代码实现3.1初始化vue项目地址:https://cn.vuejs.or......
  • vue中的echarts地图
    地图万能模板+地图配置+全国数据+地图重绘0.echarts图表只能在mounted生命周期函数调用,created钩子中页面还没挂载1、样例that.chart0=this.$echarts.init(document......
  • vue 面试
    vue是什么是一个用于创建用户界面的开源JavaScript框架,也是一个创建单页应用的Web应用框架.vue特性有数据驱动(MVVM),组件化,指令系统什么是spasinglepageapplicati......
  • 2023前端开发最新面试题收集-Vue2/3篇
    Vue整理1、谈谈MVVM的理解MVC(react):数据流是单向的,View和Model之间通过controller连接通信,用户操作会请求服务器,路由拦截分发请求,调用对应的控制器controller,控制器会......
  • 安装vue脚手架@vue/cli 4.5.13(稳定版)
    若NPM下载过慢,请参考本处改换镜像源:Here首先新建一个vue测试项目,还记得以前自己是用vue-cli2.0的版本去构建的,毕竟里面有默认配置好的webpack配置,但现在很多小伙......