123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <template>
- <view class="u-picker-warrper">
- <view v-if="hasInput" class="u-picker-input cursor-pointer" @click="showByClickInput = !showByClickInput">
- <slot>
- <view>
- {{ inputLabel && inputLabel.length ? inputLabel.join('/') : placeholder }}
- </view>
- </slot>
- </view>
- <u-popup
- :show="show || (hasInput && showByClickInput)"
- :mode="popupMode"
- @close="closeHandler"
- >
- <view class="u-picker">
- <u-toolbar
- v-if="showToolbar"
- :cancelColor="cancelColor"
- :confirmColor="confirmColor"
- :cancelText="cancelText"
- :confirmText="confirmText"
- :title="title"
- :rightSlot="toolbarRightSlot ? true : false"
- @cancel="cancel"
- @confirm="confirm"
- >
- <template #right>
- <slot name="toolbar-right"></slot>
- </template>
- </u-toolbar>
- <slot name="toolbar-bottom"></slot>
- <picker-view
- class="u-picker__view"
- :indicatorStyle="`height: ${addUnit(itemHeight)}`"
- :value="innerIndex"
- :immediateChange="immediateChange"
- :style="{
- height: `${addUnit(visibleItemCount * itemHeight)}`
- }"
- @change="changeHandler"
- >
- <picker-view-column
- v-for="(item, index) in innerColumns"
- :key="index"
- class="u-picker__view__column"
- >
- <view
- v-if="testArray(item)"
- class="u-picker__view__column__item u-line-1"
- v-for="(item1, index1) in item"
- :key="index1"
- :style="{
- height: addUnit(itemHeight),
- lineHeight: addUnit(itemHeight),
- fontWeight: index1 === innerIndex[index] ? 'bold' : 'normal',
- display: 'block'
- }"
- >{{ getItemText(item1) }}</view>
- </picker-view-column>
- </picker-view>
- <view
- v-if="loading"
- class="u-picker--loading"
- >
- <u-loading-icon mode="circle"></u-loading-icon>
- </view>
- </view>
- </u-popup>
- </view>
- </template>
- <script>
- import { props } from './props';
- import { mpMixin } from '../../libs/mixin/mpMixin';
- import { mixin } from '../../libs/mixin/mixin';
- import { addUnit, deepClone, sleep } from '../../libs/function/index';
- import test from '../../libs/function/test';
- export default {
- name: 'u-picker',
- mixins: [mpMixin, mixin, props],
- data() {
- return {
-
- lastIndex: [],
-
- innerIndex: [],
-
- innerColumns: [],
-
- columnIndex: 0,
- showByClickInput: false,
- }
- },
- watch: {
-
- defaultIndex: {
- immediate: true,
- deep:true,
- handler(n) {
- this.setIndexs(n, true)
- }
- },
-
- columns: {
- immediate: true,
- deep:true,
- handler(n) {
- this.setColumns(n)
- }
- },
- },
- emits: ['close', 'cancel', 'confirm', 'change', 'update:modelValue', 'update:show'],
- computed: {
- inputLabel() {
- let items = this.innerColumns.map((item, index) => item[this.innerIndex[index]])
- let res = []
- items.forEach(element => {
- res.push(element[this.keyName])
- });
- return res
- },
- inputValue() {
- let items = this.innerColumns.map((item, index) => item[this.innerIndex[index]])
- let res = []
- items.forEach(element => {
- res.push(element['id'])
- });
- return res
- }
- },
- methods: {
- addUnit,
- testArray: test.array,
-
- getItemText(item) {
- if (test.object(item)) {
- return item[this.keyName]
- } else {
- return item
- }
- },
-
- closeHandler() {
- if (this.closeOnClickOverlay) {
- if (this.hasInput) {
- this.showByClickInput = false
- }
- this.$emit('update:show', false)
- this.$emit('close')
- }
- },
-
- cancel() {
- if (this.hasInput) {
- this.showByClickInput = false
- }
- this.$emit('update:show', false)
- this.$emit('cancel')
- },
-
- confirm() {
- this.$emit('update:modelValue', this.inputValue)
- if (this.hasInput) {
- this.showByClickInput = false
- }
- this.$emit('update:show', false)
- this.$emit('confirm', {
- indexs: this.innerIndex,
- value: this.innerColumns.map((item, index) => item[this.innerIndex[index]]),
- values: this.innerColumns
- })
- },
-
- changeHandler(e) {
- const {
- value
- } = e.detail
- let index = 0,
- columnIndex = 0
-
- for (let i = 0; i < value.length; i++) {
- let item = value[i]
- if (item !== (this.lastIndex[i] || 0)) {
-
- columnIndex = i
-
- index = item
- break
- }
- }
- this.columnIndex = columnIndex
- const values = this.innerColumns
-
- this.setLastIndex(value)
- this.setIndexs(value)
- this.$emit('update:modelValue', this.inputValue)
- this.$emit('change', {
-
-
-
-
- value: this.innerColumns.map((item, index) => item[value[index]]),
- index,
- indexs: value,
-
- values,
- columnIndex
- })
- },
-
- setIndexs(index, setLastIndex) {
- this.innerIndex = deepClone(index)
- if (setLastIndex) {
- this.setLastIndex(index)
- }
- },
-
- setLastIndex(index) {
-
-
- this.lastIndex = deepClone(index)
- },
-
- setColumnValues(columnIndex, values) {
-
- this.innerColumns.splice(columnIndex, 1, values)
-
- this.setLastIndex(this.innerIndex.slice(0, columnIndex))
-
- let tmpIndex = deepClone(this.innerIndex)
- for (let i = 0; i < this.innerColumns.length; i++) {
- if (i > this.columnIndex) {
- tmpIndex[i] = 0
- }
- }
-
- this.setIndexs(tmpIndex)
- },
-
- getColumnValues(columnIndex) {
-
-
- (async () => {
- await sleep()
- })()
- return this.innerColumns[columnIndex]
- },
-
- setColumns(columns) {
-
- this.innerColumns = deepClone(columns)
-
- if (this.innerIndex.length === 0) {
- this.innerIndex = new Array(columns.length).fill(0)
- }
- },
-
- getIndexs() {
- return this.innerIndex
- },
-
- getValues() {
-
-
- (async () => {
- await sleep()
- })()
- return this.innerColumns.map((item, index) => item[this.innerIndex[index]])
- }
- },
- }
- </script>
- <style lang="scss" scoped>
- @import "../../libs/css/components.scss";
- .u-picker {
- position: relative;
- &__view {
- &__column {
- @include flex;
- flex: 1;
- justify-content: center;
- &__item {
- @include flex;
- justify-content: center;
- align-items: center;
- font-size: 16px;
- text-align: center;
- /* #ifndef APP-NVUE */
- display: block;
- /* #endif */
- color: $u-main-color;
- &--disabled {
- /* #ifndef APP-NVUE */
- cursor: not-allowed;
- /* #endif */
- opacity: 0.35;
- }
- }
- }
- }
- &--loading {
- position: absolute;
- top: 0;
- right: 0;
- left: 0;
- bottom: 0;
- @include flex;
- justify-content: center;
- align-items: center;
- background-color: rgba(255, 255, 255, 0.87);
- z-index: 1000;
- }
- }
- </style>
|