I am building a date picker using Flowbite Datepicker Plugin: https://flowbite.com/docs/plugins/datepicker/ . Here is my html code:
<form #submit.prevent="checkAvailability" method="POST" class="space-y-6">
<div class="items-center">
<label for="start" class="text-sm">Pick Up Date</label>
<div class="relative mb-4">
<div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
<svg class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z" clip-rule="evenodd"></path></svg>
</div>
<input datepicker datepicker-autohide v-model="startDate" type="text" class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Select date">
</div>
</div>
<div>
<button type="submit" class="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">Check Availability</button>
</div>
</form>
and here is my Vue JS Code:
<script>
export default {
data(){
return {
startDate: '',
}
},
methods:{
checkAvailability(e) {
console.log("start date", this.startDate);
}
}
}
However, when I try to print start date in the console, it is showing as empty even if I had selected the date in the date picker.
I have tried to set #change method, but it seems like the function of onChange is not being called once I set the date.
Any help is appreacited!
Not 100% sure - but the plugin does not update in a vue way. I guess it is because the value is set by javascript, so no change event will raise.
It looks like the "data.startDate" is not updated when you click on a date but if you type something it will be changed.
What you can do is a "workaround", make a ref in the input
<input ref="datepicker1" datepicker ...>
and and use the refs value like that
console.log(this.$refs.datepicker1.value);
Just writing because you appreciate any help
Related
I am trying to output the value of individual checkboxes and also add a class to the label when the checkbox is checked. I can do one of the other but not both together. If I add :value="attendance" the output works as individual instances but the adding of the class doesn't work and if I add value="attendance" then it treats the 2 checkboxes as one value.
Can someone help please?
<div class="container">
<div class="row">
<div class="col-sm">
<label
class="btn btn-outline-primary label-in-filter"
:class="{
showattendances:
showattendancesisChecked('attendance'),
}"
v-for="attendance in uniqueattendances"
:key="attendance"
>
<!-- <input
value="attendance"
id="attendance"
name="showattendances"
class="ck-in-filter"
type="checkbox"
v-model="attendances"
/> -->
<input
id="attendance"
name="showattendances"
class="ck-in-filter"
type="checkbox"
:value="attendance"
v-model="attendances"
/>
{{ attendance }}
</label>
</div>
<p v-for="attendance in attendances" :key="attendance">
{{ attendance }}
</p>
</div>
</div>
methods: {
showattendancesisChecked(value) {
return this.attendances.includes(value);
},}
I have an Ajax form on my view. It relates to Newsletter
<div class="d-flex flex-column">
<div class="d-flex align-items-center">
<div class="px-lg-8">
<div class="d-flex align-items-center">
<div class=" ml-3">
<svg xmlns="http://www.w3.org/2000/svg" width="21.238" height="15.291" viewBox="0 0 21.238 15.291">
<path d="M0 0v15.292h21.238V0zm.849.849h19.54v1.062l-8.31 7.244-.04.04a2.194 2.194 0 0 1-1.42.571 2.2 2.2 0 0 1-1.42-.571c-.158-.138-1.293-1.118-2.111-1.832C4.661 5.255.974 2.021.849 1.912zm0 2.19c.737.642 3.35 2.917 5.575 4.858L.849 12.305zm19.539 0v9.266l-5.575-4.407c2.226-1.943 4.839-4.216 5.576-4.858zM7.075 8.469l1.566 1.367.013.013a3.057 3.057 0 0 0 1.965.77 3.051 3.051 0 0 0 1.978-.783c.12-.1 1.059-.913 1.58-1.367l6.212 4.911v1.062H.849V13.38z" data-name="Path 319"></path>
</svg>
</div>
<h5 class="mb-0"> #_localizer["NewsLetterMemberShip"]</h5>
</div>
<p class="my-3 ">#_localizer["ReceiveNewsLetter"]</p>
<div class="textbox-footer ">
<form asp-controller="Home" asp-action="NewsLetter" data-ajax="true" data-ajax-mode="replace" data-ajax-success="NewLetterPostSuccess">
<input asp-for="Email" id="NewsLetterEmail" class="form-control input-textbox-footer "
placeholder="#_localizer["EnterEmail"]">
<span asp-validation-for="Email"></span>
<button type="submit" class="btn btn--orange footer__send-btn">#_localizer["SendButton"]</button>
</form>
</div>
</div>
</div>
</div>
It works well. but now I see after click on newsletter ,It inserts email twice,
It has a reference:
<script src="/lib/jquery-unobtrusive-ajax/jquery.unobtrusive-ajax.min.js"></script>
If I delete this refrence, it will be work correct and insert 1 time.
This is my script :
function NewLetterPostSuccess(result) {
swal('info', result.text, 'info');
$('#NewsLetterEmail').val('');
}
I see the way for resolve is delete this reference.
But is not for ajax form?
and If I delete reference I have another misktake too.
How can resolve this problem?
I find my mistake .
It referenced to page twice with "unobtrusive" at 2 different Route.
<script src="/lib/jquery-unobtrusive-ajax/jquery.unobtrusive-ajax.min.js"></script>
I deleted one of them and It works well now.
Check all your project for the reference always! :)
I have an array of colors. I want to use them to choose colors for the text. I use the radio button because I only want to select one. I custom the default radio by using a label. The problem I'm having is that the radio only selects one but my label can select multiple. How can I fix it? It's hard to understand so this is a picture about my problem
<div class="d-flex align-items-center flex-wrap mb-7">
<span class="w--26 flex-fixed pe-3 mb-5">{{
$t('setting.setting_theme.title_background')
}}</span>
<div v-for="color in list_colors" :key="color.id" class="me-5 mb-5">
<input
:v-model="color.checked"
type="radio"
class=""
:id="color.id"
name="selectTitleBackground"
#click="color.checked = !color.checked"
/>
<label
class="w--8 h--8 flex-center cursor-pointer border"
:for="color.id"
:style="{ backgroundColor: color.value }"
><i
v-if="color.checked"
:class="{ 'text-dark': color.value == '#fff' }"
class="fal fa-check text-light"
></i
></label>
</div>
</div>
enter image description here
The model that is used on the inputs is color.checked. This color object is coming from v-for="color in list_colors". So checking the input is changing each individual color object. You need to use a data property to store the checked color:
data() {
return {
selectedColorId: 1
}
}
<input
:v-model="selectedColorId"
type="radio"
:id="color.id"
:value="color.id"
name="selectTitleBackground"
/>
You also dont need the click event on the input as this should be handled by the input binding. https://v2.vuejs.org/v2/guide/forms.html#Radio
And the adding of the tick icon:
<i v-if="color.id === selectedColorId"
:class="{ 'text-dark': color.value == '#fff' }"
class="fal fa-check text-light"
></i>
I have an element part of a modal, like this:
<template>
<portal to="portal-level-3">
<div
v-if="isOpen"
class="fixed z-50 px-4 pb-4 inset-0 flex items-center justify-center"
:class="[isContentHeightGreaterThanHolder ? 'items-baseline' : 'items-center']"
>
<div
class="fixed inset-0 transition-opacity"
enter-active-class="ease-out duration-300"
enter-class="opacity-0"
enter-to-class="scale-100"
leave-active-class="ease-in duration-200"
leave-class="opacity-100"
leave-to-class="opacity-0"
>
<div class="absolute inset-0 bg-gray-500 opacity-75"></div>
</div>
<div
v-if="isOpen"
v-click-outside="closeModal"
ref="refContent"
:class="[
notFullWidth ? '' : 'w-full',
modalWidthClass
]"
class="bg-white rounded-lg overflow-hidden shadow-xl transform transition-all px-4 pt-5 pb-4"
enter-active-class="ease-out duration-300"
enter-class="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
enter-to-class="opacity-100 translate-y-0 sm:scale-100"
leave-active-class="ease-in duration-200"
leave-class="opacity-100 translate-y-0 sm:scale-100"
leave-to-class="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
>
<div class="flex mb-4">
<div v-if="title" class="flex-1">
<h3 class="text-lg leading-6 font-medium text-brand">
{{ title }}
</h3>
</div>
<div class="pl-3">
<button
#click.prevent="closeModal()"
type="button"
class="text-gray-400 hover:text-gray-500 focus:outline-none focus:text-gray-500 transition ease-in-out duration-150"
>
<font-awesome-icon class="h-6 w-6" :icon="['fal', 'times']" />
</button>
</div>
</div>
<slot></slot>
</div>
</div>
</portal>
</template>
In the Vue instance, I watch for the modal opening like this:
watch: {
visible(val) {
this.isOpen = val
},
isOpen(val) {
if (val) {
this.$nextTick(() => {
let holderHeight = this.$el.offsetHeight
let contentHeight = this.$refs.refContent.offsetHeight
this.isContentHeightGreaterThanHolder = contentHeight >= (holderHeight - 30)
})
}
}
},
But this results in:
"TypeError: Cannot read property 'offsetHeight' of undefined"
But if I console.log(this.$refs) I get:
{}
refContent: div.bg-white.rounded-lg.overflow-hidden.shadow-xl.transform.transition-all.px-4.pt-5.pb-4.w-full.max-w-xl
##clickoutsideContext: {id: 5, methodName: "closeModal", documentHandler: ƒ, bindingFn: ƒ}
accessKey: ""
align: ""
ariaAtomic: null
ariaAutoComplete: null
ariaBusy: null
ariaChecked: null
ariaColCount: null
ariaColIndex: null
ariaColSpan: null
ariaCurrent: null
ariaDescription: null
ariaDisabled: null
ariaExpanded: null
....
offsetHeight: 805
Why can I not access this.$refs.refContent.offsetHeight? Thanks!
In my vue 2.5.17 app I have a select input and text input and I have a create a validation rule that only 1 of them musdt be filled :
<div class="form-row p-3">
<label for="existing_user_list_id" class="col-12 col-sm-4 col-form-label">Select already existing list</label>
<div class="col-12 col-sm-8">
<v-select
v-model="selection_existing_user_list_id"
data-vv-name="selection_existing_user_list_id"
:options="userListsByUserIdArray"
v-validate="''"
id="existing_user_list_id"
name="existing_user_list_id"
class="form-control editable_field"
placeholder="Select existing user list"
v-on:input="onChangeSelectionExistingUserListId($event);"
></v-select>
<span v-show="vueErrorsList.has('existing_user_list_id')" class="text-danger">{{ vueErrorsList.first('existing_user_list_id') }}</span>
</div>
</div>
<div class="form-row p-1 m-1 ml-4">
<strong>OR</strong>
</div>
<div class="form-row p-2`">
<label for="new_user_list_title" class="col-12 col-sm-4 col-form-label">Create a new list</label>
<div class="col-12 col-sm-8">
<input class="form-control" value="" id="new_user_list_title" v-model="new_user_list_title" #change="onChangeBewUserListTitle($event);">
<span v-show="vueErrorsList.has('title')" class="text-danger">{{ vueErrorsList.first('title') }}</span>
</div>
</div>
I found this :
https://baianat.github.io/vee-validate/guide/custom-rules.html#creating-a-custom-rule
But I am confused how to use it in my case. I try :
import { Validator } from 'vee-validate';
Validator.extend('new_user_list_title', {
getMessage: field => 'The ' + field + ' value is not a valid new_user_list_title.',
validate: value => false // for simplicity I try to raise error always
// validate: value => value.length == 0 && selection_existing_user_list_id == null
})
But my custom error is never triggered, even when I set always false,
Which is right way ?
Thanks!
After adding custom rule, you need to use it in component (in v-validate):
<input
class="form-control"
id="new_user_list_title"
v-model="new_user_list_title"
#change="onChangeBewUserListTitle($event);"
v-validate="'new_user_list_title'">