VueJS bind input to Date value - vuejs2

I have data with Date fields startDate and stopDate:
data: {
error: '',
config: {rate: 0.00, surcharge: 0.00, startDate: new Date(), stopDate: null, enabled: true},
configurations: []
},
Also I have input field:
<input id="startDate" type="date" v-model="config.startDate">
So while page creation I see warning in console:
vue.js:7629 The specified value "Tue Sep 17 2019 15:52:44 GMT+0700
(\u041A\u0440\u0430\u0441\u043D\u043E\u044F\u0440\u0441\u043A,
\u0441\u0442\u0430\u043D\u0434\u0430\u0440\u0442\u043D\u043E\u0435
\u0432\u0440\u0435\u043C\u044F)" does not conform to the required
format, "yyyy-MM-dd".
Also, input field has no date value just after page has loaded. Seems like initial Date value has not bound to input field.
What is the natural way to bind date value to input field?
thanks for your answers and best regards.

This is a working jsfiddle. I don't receive any error in console.log
HTML
<div id="date">
<p>
New Date: <input id="startDate" type="date" v-
model="config.startDate">
<span>{{config.startDate}}</span>
</p>
</div>
JS
var box = new Vue({
el:'#date',
data:{
config: {rate: 0.00, surcharge: 0.00, startDate: new Date(),
stopDate: null, enabled: true}}
});
https://jsfiddle.net/o6d1x9eb/

According VueJS documentation, v-model="config.startDate" is equals to v-bind:value="config.startDate" v-on:input="config.startDate = $event.target.value".
So, I have two ways:
Store in config variable of data value as String instead of Date and edit it by the input field. By the way, input field returns string value of date in yyyy-mm-dd format.
Use value as Date, filter (or function) to convert Date into String and function to convert String into Date.
So, the way I used is
for inpuut field:
<input id="startDate" type="date"
v-bind:value="config.startDate | inputDateFilter"
v-on:input="config.startDate = getDate($event.target.value)">
and for Vue instance:
filters: {
inputDateFilter: function (date) {
if (!date) {
return '';
}
date = new Date(date);
return date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2);
},
// . . .
methods: {
getDate(value) {
if (!value) {
return null;
}
return new Date(value);
},
// . . .
This is does what I want.

Related

How to show icon next to value in cloumn in aurelia slickgrid/slickgrid?

I want to show en edit icon next to value in Amount column. This is because the Amount column is actually editable.But to give that as a hint to user, i want to show some edit icon next to it. How to do that in aurelia slickgrid?
Or maybe there is a way to highlight a field on hover ?
I am using aurelia slickgrid and looking if there is some option in aurelia slickgrid itself.
Go to the aurelia slickgrid example link and click on the link of example's source code
When you open it, there is a method called defineGrids
/* Define grid Options and Columns */
defineGrids() {
this.columnDefinitions1 = [
...,
...,
...,
...,
...,
{ id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven', formatter: myCustomCheckmarkFormatter, type: FieldType.number, sortable: true, minWidth: 100 }
];
... rest of the code
}
The row with id effort-driven is where the icons are placed. On the other words, when you push a data collection(usually array of json object) to the table, values of the data objects with key name effort-driven are given to column with id effort-driven. Furthermore, for each passed value to the column, the method myCustomCheckmarkFormatter reformat it(for example 0 -> false or null -> not filled) and place it to the corresponding table's cell. look at the below method:
// create my custom Formatter with the Formatter type
const myCustomCheckmarkFormatter: Formatter<DataItem> = (_row, _cell, value) => {
// you can return a string of a object (of type FormatterResultObject), the 2 types are shown below
return value ? `<i class="fa fa-fire red" aria-hidden="true"></i>` : { text: '<i class="fa fa-snowflake-o" aria-hidden="true"></i>', addClasses: 'lightblue', toolTip: 'Freezing' };
};
As you can see, when the method is called, it returns an icon such as <i class="fa fa-fire red" aria-hidden="true"></i> which is placed in the table's cell.
I added an edit icon next to Amount,
{
id: "Edit",
field: "edit",
excludeFromColumnPicker: true,
excludeFromExport: true,
excludeFromQuery: true,
excludeFromGridMenu: true,
excludeFromHeaderMenu: true,
minWidth: 30,
maxWidth: 30,
formatter: Formatters.editIcon,
},
and used this custom format from ghiscoding comment:
const customEditableInputFormatter: Formatter = (_row, _cell, value, columnDef, dataContext, grid) => {
const isEditable = !!columnDef.editor;
value = (value === null || value === undefined) ? '' : value;
return isEditable ? `<div style="background-color: aliceblue">${value}</div>` : value;
};
The result is as shown in the picture.

How to create correct rule for validating zero via vee-validate

"vee-validate": "^2.2.11",
"vue": "^2.5.16",
I need a simple rule, the rule should be that the input must be required,numeric and greater than 0.
So in this case, if I input 0 it validates correctly(return false), but if I input something like this 0.0 vv returns true. Even if I remove is_not:0, the result still the same.
<sui-input
type="text"
v-validate="'required|decimal|is_not:0'"
name="cellSize"
v-model="cellSize">
You could also create a custom rule, like follows.
created() {
this.$validator.extend(
'greaterThanZero',{
getMessage: field => field + ' needs to be > zero.',
validate: (value) => {
// value must be > zero
if (value > 0 ) return true;
return false;
}
});
},
then call the code on your field instance.
v-validate="'required|decimal|greaterThanZero'"
more on custom rules here:
https://vee-validate.logaretm.com/v2/guide/custom-rules.html#creating-a-custom-rule
or you could also use this following style (if you were going to add more than one rule for example). Here the code would be inserted in the area where you do your imports, i.e. directly after the script tag.
import { Validator } from 'vee-validate';
Validator.extend(
'greaterThanZero',
(value) => {
// value must be > zero
if (value > 0 ) return true;
return false;
}
);
let instance = new Validator({ greaterThanZeroField: 'greaterThanZero' });
you can now add a second rule to the style directly above using the following code:
instance.extend('greaterThan1Million', {
getMessage: field => field +' needs to be > 1,000,000',
validate: value => (value > 1000000 ? true : false)
});
instance.attach({
name: 'greaterThan1MillionField',
rules: 'greaterThan1Million'
});
again that second rule could be called as follows:
v-validate="'required|decimal|greaterThan1Million'"
I found this solution(to leave everything greater than 0)
<sui-input
type="text"
v-validate="{ required: true, regex: /^(?=.*[1-9])\d+(\.\d+)?$/ }"
name="cellSize"
v-model="cellSize">
</sui-input>
Did you try to use a regex to exclude 0 ?
Example:
<input v-validate="{ required: true, regex: /[1-9]*/ }">

How to pass Today's Date as default value createFragmentContainer in argumentDefinitions

I am using Relay Modern (V 1.7.0). We have requirement where we need to send today's date to our GraphQL API
export default createFragmentContainer(
UpcomingCampaigns,
{
Viewer: graphql`
fragment UpcomingCampaigns_Viewer on Viewer #argumentDefinitions(
today: {type: "string", defaultValue:{field: "End_Date", value:"Need today's date here"}}
}`
)
So how do i pass today's date in value field ? doing ${new date()} don't work and giving error like Error: Parse error: Error: FindGraphQLTags: Substitutions are not allowed in graphql tags
Thanks in advance
i think you can pass variable as a string
export default createFragmentContainer(
UpcomingCampaigns,
{
Viewer: graphql`
fragment UpcomingCampaigns_Viewer on Viewer #argumentDefinitions(
today: {type: "string", defaultValue:{field: "End_Date", value:"String"}}
}`,
variable:{
value: `${new date()}`
}
)

How to set a default value for <v-select> that will update value if user changes the selection in Vue.js

I have searched online and have not found an answer to this Vue.js question.
I am pretty new to Vue.js. In my form, there is a drop down for end time which gives a choice of times to choose from. The default for this field is 30 minutes after start time, but the user can change this value. For now, the user is able to change the displayed time for end time, but the data property end_time is not getting changed. I need this value to update (not just stay at the default) so I can use it to calculate duration from start time to end time. Code is below. Thanks for the help in advance.
v-select:
<v-select
:items="times"
:value="getEndTime"
label="End Time"
box
placeholder="End Time"
></v-select>
part of my data object (note: end_time)
export default {
data: () => ({
date: new Date().toISOString().slice(0,10),
menu2: false,
times: [],
start_time: '',
end_time: '',
from methods: section:
getStartTimeIndex()
{
// looking for index of start_time in times array
var startTimeIndex=0;
for(var x =0; x<this.times.length;x++)
{
if(this.times[x] == this.start_time)
{
startTimeIndex = x;
break;
}
}
return startTimeIndex;
}
from computed: section:
getEndTime()
{
var startTimeIndex = this.getStartTimeIndex();
//if start time is set and end time is not:
if (this.start_time != '' && this.end_time == '')
{
this.end_time = this.times[startTimeIndex + 2]; // two 15 min. increments is 1/2 hr. later
console.log("getEndTime(): startTimeIndex = " + startTimeIndex);
}
return this.end_time;
}
Use component's change event:
<v-select
:items="times"
:value="getEndTime"
label="End Time"
box
placeholder="End Time"
#change="changeEndTime"
></v-select>
In methods:
changeEndTime(value)
{
this.$set(this, 'end_time', value);
},

Vue-good-table Invalid Date when date is null using

I'm using a table component for Vuejs called vue-good-table.
It allows you to format your columns with different formats like string, number and date.
My issue today is related to the date formatting. I'm being able to format the column properly, however, some of the values that come from the database are null, and then it appears a label Invalid Date for those rows that have null dates.
Is there a way to configure those columns as nullable or format them in a way that I prevent this message to be shown? I'd rather leave the table cell empty instead of displaying that label.
The following example shows the scenario that I'm describing
<template>
<vue-good-table
:columns="columns"
:globalSearch="true"
:paginate="true"
:rows="rows">
</vue-good-table>
</template>
<script>
export default {
name: 'Table',
computed: {
columns() {
return [{
field:"fieldA",
label:"fieldA",
type:"number"
},{
field:"fieldB",
label:"fieldB",
type:"date",
inputFormat: 'YYYY/MM/DD',
outputFormat: 'YYYY/MM/DD'
}];
}
},
rows() {
return [{
fieldA: 1,
fieldB: null
},{
fieldA: 1,
fieldB: '2017/01/04'
}];
}
}
</script>
In that case, I would have one field with the date formatted correctly, and another field with a label Invalid Date
Thanks
-- UPDATED --
This problem was fixed in most recent versions.
Edit: I have just realized the formatFn function overrides the input and output format options, since it will only use the function to format the data. Not sure if it's the best aproach, but instead you should probably format the data yourself on the function (or format the data before passing to the table). Here's an example using moment:
{
label: 'Date/Time',
field: 'date',
type: 'date',
formatFn: function (value) {
return value != null ? moment(value, 'DD/MM/YYYY HH:mm:ss').format('DD-MM-YYYY HH:mm:ss') : null
}
}
I had the same issue and I was able to format the data using the formatFn function (https://github.com/xaksis/vue-good-table#formatfn-function).
{
label: 'Date/Time',
field: 'date',
type: 'date',
dateInputFormat: 'DD/MM/YYYY HH:mm:ss',
dateOutputFormat: 'DD-MM-YYYY HH:mm:ss',
formatFn: function (value) {
return value != null ? value : null
}
}
Hope this helps