2
0
mirror of https://github.com/tenrok/vue2-datepicker.git synced 2026-06-25 03:40:36 +03:00

添加语言选项

This commit is contained in:
Mengxiong10
2017-05-09 13:54:06 +08:00
parent 42e92e4b90
commit 7e7aeac713
6 changed files with 93 additions and 39 deletions
+35 -10
View File
@@ -1,18 +1,43 @@
# vue2-datepicker # vue2-datepicker
> A Vue.js project > A Datepicker Component For Vue2
## Build Setup ## Demo
<>
![image](https://github.com/mengxiong10/vue2-datepicker/tree/master/screenshot/demo.png)
``` bash ## Usage
# install dependencies
npm install
# serve with hot reload at localhost:8080 ```html
npm run dev <template>
<div>
<date-picker v-model="time1"></date-picker>
<date-picker v-model="time2" range></date-picker>
</div>
</template>
# build for production with minification <script>
npm run build import DatePicker from 'datepicker/index.vue'
export default {
components: { DatePicker },
data() {
return {
time1: '',
time2: '',
}
}
}
</script>
``` ```
## Attributes
| Prop | Type | Default | Description |
|-----------------|---------------|-------------|---------------------------------------|
| range | Boolean | false | if true, the type is daterange |
| format | String | yyyy-MM-dd | Date formatting string |
| language | String | zh | Translation (en/zh) |
| placeholder | String | | input placeholder text |
| width | String/Number | 210 | input size |
For detailed explanation on how things work, consult the [docs for vue-loader](http://vuejs.github.io/vue-loader).
Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

+4 -4
View File
@@ -1,12 +1,12 @@
<template> <template>
<div id="app"> <div id="app">
<div class="demo"> <div class="demo">
<span class="label">默认:</span> <span class="label">default:</span>
<date-picker v-model="value1"></date-picker> <date-picker v-model="value1" lang="en"></date-picker>
</div> </div>
<div class="demo"> <div class="demo">
<span class="label">range:</span> <span class="label">range:</span>
<date-picker v-model="value2" range></date-picker> <date-picker v-model="value2" range lang="zh"></date-picker>
</div> </div>
</div> </div>
</template> </template>
@@ -19,7 +19,7 @@ export default {
components:{ DatePicker }, components:{ DatePicker },
data () { data () {
return { return {
value1:'2015-10-10', value1:'2017-5-9',
value2:'', value2:'',
} }
} }
+9 -5
View File
@@ -3,8 +3,8 @@
<div class="calendar-header"> <div class="calendar-header">
<i class="calendar__prev-icon" @click="changeYear(-1)">&laquo;</i> <i class="calendar__prev-icon" @click="changeYear(-1)">&laquo;</i>
<i class="calendar__prev-icon" @click="changeMonth(-1)">&lsaquo;</i> <i class="calendar__prev-icon" @click="changeMonth(-1)">&lsaquo;</i>
<span>{{now.getFullYear() + '年'}}</span> <span>{{months[now.getMonth()]}}</span>
<span>{{now.getMonth() + 1 + '月'}}</span> <span>{{now.getFullYear()}}</span>
<i class="calendar__next-icon" @click="changeYear(1)">&raquo;</i> <i class="calendar__next-icon" @click="changeYear(1)">&raquo;</i>
<i class="calendar__next-icon" @click="changeMonth(1)" >&rsaquo;</i> <i class="calendar__next-icon" @click="changeMonth(1)" >&rsaquo;</i>
</div> </div>
@@ -29,6 +29,8 @@
</template> </template>
<script> <script>
import Languages from './languages.js'
export default { export default {
props: { props: {
startAt: null, startAt: null,
@@ -37,8 +39,10 @@ export default {
show: Boolean, show: Boolean,
}, },
data() { data() {
const translation = this.$parent.translation
return { return {
days: ['日', '一', '二', '三', '四', '五', '六'], days: translation.days,
months: translation.months,
dates: [], dates: [],
now: new Date(), now: new Date(),
} }
@@ -203,8 +207,8 @@ export default {
.calendar-table td, .calendar-table td,
.calendar-table th { .calendar-table th {
width: 30px; width: 32px;
height: 30px; height: 32px;
text-align: center; text-align: center;
} }
+25 -20
View File
@@ -33,6 +33,7 @@
<script> <script>
import CalendarPanel from './calendar-panel.vue' import CalendarPanel from './calendar-panel.vue'
import Languages from './languages.js'
export default { export default {
components: { CalendarPanel }, components: { CalendarPanel },
@@ -49,7 +50,11 @@ export default {
type: [String, Number], type: [String, Number],
default: 210, default: 210,
}, },
palceholder: String, placeholder: String,
lang: {
type: String,
default: 'zh'
},
value: null, value: null,
}, },
data() { data() {
@@ -57,7 +62,7 @@ export default {
showPopup: false, showPopup: false,
showCloseIcon: false, showCloseIcon: false,
currentValue: this.value, currentValue: this.value,
ranges: [], ranges: [], // 快捷选项
} }
}, },
watch: { watch: {
@@ -78,8 +83,11 @@ export default {
}, },
}, },
computed: { computed: {
translation() {
return Languages[this.lang] || Languages['en']
},
innerPlaceholder() { innerPlaceholder() {
return this.placeholder ? this.placeholder : (this.range ? '请选择日期范围' : '请选择日期') return this.placeholder || (this.range ? this.translation.placeholder.dateRange : this.translation.placeholder.date)
}, },
text() { text() {
if (!this.range && this.currentValue) { if (!this.range && this.currentValue) {
@@ -159,32 +167,29 @@ export default {
this.$emit('input', [range.start, range.end]) this.$emit('input', [range.start, range.end])
}, },
initRanges() { initRanges() {
const time = new Date() this.ranges = [{
time.setMonth(time.getMonth() + 1, 0) // 切换到本月最后一天
this.ranges.push({
text: '今天', text: '今天',
start: new Date(), start: new Date(),
end: new Date(), end: new Date(),
}, {
text: '未来一周',
start: new Date(),
end: new Date(Date.now() + 3600 * 1000 * 24 * 7),
}, {
text: '未来一个月',
start: new Date(),
end: new Date(Date.now() + 3600 * 1000 * 24 * 30)
}, { }, {
text: '最近一周', text: '最近一周',
start: new Date(Date.now() - 3600 * 1000 * 24 * 7), start: new Date(Date.now() - 3600 * 1000 * 24 * 7),
end: new Date(), end: new Date(),
}, {
text: '今后一周',
start: new Date(),
end: new Date(Date.now() + 3600 * 1000 * 24 * 7),
}, {
text: '本月',
start: new Date(time.getFullYear(), time.getMonth(), 1),
end: time,
}, { }, {
text: '最近一个月', text: '最近一个月',
start: new Date(Date.now() - 3600 * 1000 * 24 * 30), start: new Date(Date.now() - 3600 * 1000 * 24 * 30),
end: new Date(), end: new Date(),
}, { }]
text: '最近三个月', this.ranges.forEach((v, i) => {
start: new Date(Date.now() - 3600 * 1000 * 24 * 90), v.text = this.translation.pickers[i]
end: new Date()
}) })
}, },
}, },
@@ -221,7 +226,7 @@ export default {
.datepicker-popup { .datepicker-popup {
position: absolute; position: absolute;
width: 234px; width: 248px;
margin-top: 1px; margin-top: 1px;
border: 1px solid #d9d9d9; border: 1px solid #d9d9d9;
background-color: #fff; background-color: #fff;
@@ -230,7 +235,7 @@ export default {
} }
.range { .range {
width: 468px; width: 496px;
} }
.input { .input {
+20
View File
@@ -0,0 +1,20 @@
export default {
'zh': {
'days': ['日', '一', '二', '三', '四', '五', '六'],
'months': ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
'pickers': ['今天', '未来一周', '未来一个月', '最近一周', '最近一个月'],
'placeholder': {
'date': '请选择日期',
'dateRange': '请选择日期范围'
}
},
'en': {
'days': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
'months': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
'pickers': ['today', 'next week', 'next month', 'last week', 'last month'],
'placeholder': {
'date': 'Select Date',
'dateRange': 'Select Date Range'
}
},
}