How to use vue-bootstrap-datetimepicker date plugin in vue-cli 3

How to use vue-bootstrap-datetimepicker date plugin in vue-cli 3

Demand Background

Recently, I plan to use Vue and combine it with the front-end engineering system to reconstruct a previous Demo. One of the functions is to use the bootstrap datepicker plug-in to select the query date. I found a datepicker plug-in based on Vue extension on the Internet: vue-bootstrap-datepicker. This blog mainly introduces how to use the plugin in projects created with vue-cli 3. Project address: https://gitlab.com/JiaoXN/vuecli3usedatetimepicker.git

Install the plugin and its dependencies

This plugin has two versions: one is developed based on bootstrap 3.x, and the other is developed based on bootstrap 4.x. This blog will introduce the installation and use of the latter plug-in.

First of all, you need to install the plugin dependencies, including bootstrap 4.x, jquery >= 1.8.3, moment.js 2.22, and pc-bootstrap4-datetimepicker.

  • Install bootstrap
npm install [email protected] --save-dev
  • Install jQuery
npm install [email protected] --save-dev
  • Install moment
npm install [email protected] --save-dev
  • Install pc-bootstrap4-datetimepicker
npm install [email protected] --save-dev

Or set package.json directly and install it via npm install. The package.json configuration is as follows:

...
"devDependencies": {
	"pc-bootstrap4-datetimepicker": "^4.17.50",
	"moment": "^2.22.2",
	"jquery": "^3.3.1",
	"bootstrap": "4.0.0"
}

Then install vue-bootstrap-datetimepicker. The installation method is the same as the above dependency installation.

Plugin Configuration

Since the original version of the vue-bootstrap-datetimepicker plug-in was developed based on Bootstrap 3.x, it was later expanded to adapt to Bootstrap 4.x (at this time, pc-bootstrap4-datetimepicker can be seen as a patch for Bootstrap 4.x). However, if you use this plug-in directly, the default icons (similar to time icons or date icons) will not be displayed, so the following configuration is required.

The reason for the above problem is that Bootstrap 4.x deletes the glyphicon icon, so first you need to install the fortawesome plug-in. The installation method is as follows:

npm install @fortawesome/[email protected] --save-dev

Then use the following code configuration in the Vue file that uses the datetimepicker plugin:

<script>

import '@fortawesome/fontawesome-free/css/all.css'

import $ from 'jquery'

export default {
	...
	created: function() {
		icons:
			time: 'far fa-clock',
	    date: 'far fa-calendar',
	    up: 'fas fa-arrow-up',
	    down: 'fas fa-arrow-down',
	    previous: 'fas fa-chevron-left',
	    next: 'fas fa-chevron-right',
	    today: 'fas fa-calendar-check',
	    clear: 'far fa-trash-alt',
	    close: 'far fa-times-circle'
		}
	}
}

</script

The created function in the above code belongs to a hook function in the Vue life cycle

Plugin Usage

Install related dependent plug-ins and configure plug-in images. Then you can use this plug-in. The entire Vue code is as follows:

<template>
	<div class="container">
		<div class="row>
			<div class="col-md-12">
				<date-picker
					v-model="date"
					:config="options"
					@dp-hide="showDatePickResult"/>
			</div>
		</div>
	</div>
</template>
<script>
import 'bootstrap/dist/css/bootstrap.css'

import datePicker from 'vue-bootstrap-datetimepicker'

import 'pc-bootstrap4-datetimepicker/build/css/bootstrap-datetimepicker.css'

import '@fortawesome/fontawesome-free/css/all.css'

import $ from 'jquery'

export default {
	name: 'HelloWorld',
	data () {
		return {
			date: new Date(),
			options:
				format: 'YYYY-MM-DD HH:mm:ss',
				useCurrent: false,
				locale: 'zh-cn',
				tooltips:
				 selectTime: ''
				}
			}
		}
	},
	methods: {
		showDatePickResult: function () {
			console.log(this.date)
		}
	},
	components:
		datePicker
	},
	created: function () {
		$.extend(true, $.fn.datetimepicker.defaults, {
	 		icons:
	  		time: 'far fa-clock',
	  		date: 'far fa-calendar',
	  		up: 'fas fa-arrow-up',
	  		down: 'fas fa-arrow-down',
	  		previous: 'fas fa-chevron-left',
	  		next: 'fas fa-chevron-right',
	  		today: 'fas fa-calendar-check',
	  		clear: 'far fa-trash-alt',
	  		close: 'far fa-times-circle'
	 		}
		})
	}
}
</script>

There is no need to elaborate on the content in <template></template>. Those who know Vue will know it basically. If you don’t know much about Vue, you can check Vue’s official website.

The options in data are the configurations of the datetimepicker plugin. For the overall configuration, please refer to this link. The current configuration is described as follows:

  • format: Date format. It should be noted that if you replace HH in HH:mm:ss with hh, the plugin will describe the date in AM and PM.
  • locale: indicates the language used, zh-cn indicates simplified Chinese
  • tooltips: indicates the tooltip content. There is a bug in this plugin. The tooltips for selecting date and time are both "Select Time", so this tooltip is set to empty.

The above is the details of how to use the vue-bootstrap-datetimepicker date plug-in in vue-cli 3. For more information on using the vue-bootstrap-datetimepicker date plug-in, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Example code of vue using Moment plugin to format time
  • The whole process of developing a Google plug-in with vue+element
  • Example of using swiper plugin to implement carousel in Vue
  • How to introduce Excel table plug-in into Vue
  • How to build a drag and drop plugin using vue custom directives
  • Vue code highlighting plug-in comprehensive comparison and evaluation
  • Based on vue-simple-uploader, encapsulate the global upload plug-in function of file segment upload, instant upload and breakpoint resume
  • Vue plugin error: Vue.js is detected on this page. Problem solved

<<:  Solution for installing opencv 3.2.0 in Ubuntu 18.04

>>:  Explain how to analyze SQL efficiency

Recommend

Detailed explanation of how MySQL (InnoDB) handles deadlocks

1. What is deadlock? The official definition is a...

How to add custom system services to CentOS7 systemd

systemd: The service systemctl script of CentOS 7...

mysql 5.7.20 win64 installation and configuration method

mysql-5.7.20-winx64.zipInstallation package witho...

What does the n after int(n) in MySQL mean?

You may already know that the length 1 of int(1) ...

Example analysis of interval calculation of mysql date and time

This article uses an example to describe the inte...

HTML markup language - table tag

Click here to return to the 123WORDPRESS.COM HTML ...

CSS and HTML and front-end technology layer diagram

The relationship between Javascript and DOM is ve...

A complete guide to the Docker command line (18 things you have to know)

Preface A Docker image consists of a Dockerfile a...

What is html file? How to open html file

HTML stands for Hypertext Markup Language. Nowada...

Solution to MySQL master-slave delay problem

Today we will look at why master-slave delay occu...

Analysis of several situations where MySQL index fails

1. Best left prefix principle - If multiple colum...

Brief analysis of the various versions of mysql.data.dll driver

Here is the mysql driver mysql.data.dll Notice: T...

Initial summary of the beginner's website building tutorial

After writing these six articles, I started to fee...

idea uses docker plug-in to achieve one-click automated deployment

Table of contents environment: 1. Docker enables ...

Detailed explanation of the entry-level use of MySql stored procedure parameters

Use of stored procedure in parameters IN paramete...