Files
myprojplanet_vite/src/components/CMySelect/CMySelect.ts

201 lines
5.2 KiB
TypeScript
Raw Normal View History

2021-12-11 00:25:35 +01:00
import { computed, defineComponent, onMounted, ref } from 'vue'
2021-09-16 21:08:02 +02:00
import { useI18n } from '@src/boot/i18n'
import { useUserStore } from '@store/UserStore'
import { useGlobalStore } from '@store/globalStore'
import { useQuasar } from 'quasar'
2021-12-11 00:25:35 +01:00
import { costanti } from '@costanti'
import { fieldsTable } from '@store/Modules/fieldsTable'
import { shared_consts } from '@/common/shared_vuejs'
2021-09-16 21:08:02 +02:00
export default defineComponent({
name: 'CMySelect',
emits: ['update:value', 'changeval'],
2021-09-16 21:08:02 +02:00
props: {
options: {
type: Array,
required: true,
},
2021-10-08 00:38:22 +02:00
value: [String, Number],
2021-09-16 21:08:02 +02:00
label: {
type: String,
required: true,
},
myclass: {
type: String,
required: false,
default: ''
},
tablesel: {
type: String,
required: false,
default: ''
},
optlab: [String, Function],
2021-09-16 21:08:02 +02:00
optval: {
type: String,
required: true,
},
useinput: {
type: Boolean,
required: false,
default: true
},
pickup: {
type: Boolean,
required: false,
default: false
},
2021-12-11 00:25:35 +01:00
addall: {
type: Boolean,
required: false,
default: false
},
2021-09-16 21:08:02 +02:00
dense: {
type: Boolean,
required: false,
default: true
},
multiple: {
type: Boolean,
required: false,
default: false
},
newvaluefunc: {
type: Function,
required: false,
2021-09-16 21:08:02 +02:00
},
funcgetvaluebyid: {
type: Function,
required: false,
2021-09-16 21:08:02 +02:00
},
},
components: {},
setup(props, { emit }) {
const $q = useQuasar()
const { t } = useI18n()
const userStore = useUserStore()
const globalStore = useGlobalStore()
const optFiltered = ref(<any>[])
const valori = ref(<any>[])
2021-10-08 00:38:22 +02:00
const myvalue = ref(<string | number>'')
2021-09-16 21:08:02 +02:00
const valoriload = computed(() => {
2021-12-11 00:25:35 +01:00
let myarr = props.options
if (props.addall) {
let myobj: any = {}
if (typeof props.optlab === 'string') {
myobj[props.optlab] = '(Tutti)'
myobj[props.optval] = costanti.FILTER_TUTTI
}
myarr = [myobj, ...myarr]
}
return myarr
})
2021-09-16 21:08:02 +02:00
function changeval(newval: any) {
if (props.tablesel === shared_consts.TAB_COUNTRY)
myvalue.value = newval && newval['value'] ? newval['value'] : newval
else if (props.tablesel === shared_consts.TAB_PHONES)
myvalue.value = newval && newval['code'] ? newval['code'] : newval
else
myvalue.value = newval
console.log('Myselect changeval', myvalue.value)
emit('update:value', myvalue.value)
emit('changeval', myvalue.value)
2021-09-16 21:08:02 +02:00
}
function mounted() {
// console.log('mounted myselect', props.options)
if (props.options) {
const rec: any = props.options.find((myrec: any) => myrec[`${props.optval}`] === props.value)
/*console.log('rec', rec, 'props.useinput', props.useinput)
2021-10-08 00:38:22 +02:00
console.log('props.value', props.value)
console.log('options', props.options)
console.log('optval', props.optval)
console.log('optlab', props.optlab)*/
2021-09-16 21:08:02 +02:00
2021-10-08 00:38:22 +02:00
if (rec) {
if (props.funcgetvaluebyid)
myvalue.value = props.funcgetvaluebyid(rec[`${props.optval}`])
else
myvalue.value = rec[`${props.optlab}`]
// console.log('myvalue', myvalue, 'optval', props.optval, 'rec', rec[`${props.optval}`])
2021-10-08 00:38:22 +02:00
} else {
if (!props.useinput) {
if (props.value) {
myvalue.value = props.value
}
}
2021-09-16 21:08:02 +02:00
}
}
// console.log('cmyselect: myvalue.value', myvalue.value)
2021-09-16 21:08:02 +02:00
}
function filterFn(val: any, update: any, abort: any) {
update(
async () => {
console.log('Filter val', val, val.length)
let myarr: any = []
if (val.length < 1) {
abort()
return
}
let mystr = val.toLocaleLowerCase()
if (fieldsTable.tableRemotePickup.includes(props.tablesel)) {
// if (myvalue.value.length > 1) {
if (mystr !== '')
myarr = await globalStore.loadPickup({ table: props.tablesel, search: mystr })
// const needle = val.toLocaleLowerCase()
// optFiltered.value = optFiltered.value.filter((v: any) => v.toLocaleLowerCase().indexOf(needle) > -1)
// }
} else {
myarr = props.options
}
if (props.addall) {
let myobj: any = {}
if (typeof props.optlab === 'string') {
myobj[props.optlab] = '(Tutti)'
myobj[props.optval] = costanti.FILTER_TUTTI
}
myarr = [myobj, ...myarr]
}
valori.value = myarr
console.log('tablesel', props.tablesel, 'filterFn', myarr)
},
// "ref" is the Vue reference to the QSelect
(ref: any) => {
if (val !== '' && ref.options.length > 0) {
ref.setOptionIndex(-1) // reset optionIndex in case there is something selected
ref.moveOptionSelection(1, true) // focus the first selectable option and do not update the input-value
}
}
)
}
2021-09-16 21:08:02 +02:00
onMounted(mounted)
valori.value = valoriload.value
2021-09-16 21:08:02 +02:00
return {
changeval,
myvalue,
2021-12-11 00:25:35 +01:00
valori,
filterFn,
2021-09-16 21:08:02 +02:00
}
}
})