Vue-good-table Invalid Date when date is null using - vue.js

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

Related

In vue, how do I filter a data object to only show values from the last 24 hours?

I have an array with some data objects that were created on various dates. I would like to only display the objects that were created within the last 24 hours.
I have tried to use moment for this, by using subtract on the date values, but it has no effect. Maybe someone here could come up with a suggestion.
Here are my computed properties. I use these because I am outputting the data in a bootstrap table, so the "key" represents the different values inside the object.
My table:
<b-card class="mt-4 mb-4">
<b-table
:items="tasks"
:fields="fields"
sort-desc
/>
</b-card>
My array (I am actually importing from a database, but for this question I will just write it manually) Please note I am just showing a single object here. In reality I have hundreds of objects
data: {
tasks: [
{ message: 'Foo' },
{ creationDateTime: '03-02-2022' },
{ isRead: false }
]
}
In my computed properties I then pass them to the table
computed: {
fields() {
return [
key: 'message',
label: 'message'),
sortable: true,
},
{
key: 'creationDateTime',
label: 'Date created',
formatter: date => moment(date).subtract(24, 'hours').locale(this.$i18n.locale).format('L'),
sortable: true,
},
{
key: 'isRead',
label: 'Has been read'),
sortable: true,
}
]
},
},
As I said, using subtract does not work. It still shows all objects in my database
I tried doing the reduction on the whole array as well, but I just get the error:
"TypeError: this.list.filter is not a function"
newTasks(){
if(this.tasks){
return moment(this.tasks.filter(task => !task.done)).subtract(24, 'hours')
}
}
I'm out of ideas.
In Moment, you can check if a date is within the last 24 hours with:
moment().diff(yourDate, 'hours') < 24
(note that future dates will also pass this check, but you can easily adjust it).
You can put this into your computed property:
newTasks(){
if(!this.tasks){
return []
}
return this.tasks.filter(task => !task.done && moment().diff(task.creationDateTime, 'hours') < 24)
}
And that's it, now newTasks should contain all tasks from the last 24 hours that are not done.

Query from a table to get a JSON result

I have a table
I'd like to get the JSON result like this:
[
{
"Type":"I", //If group name is empty display ‘I’ otherwise, display ‘G’
"StartDate":"20/12/2020 8:00am", //This is the start time
"Additional":{ //Because it is non-group (GroupName is null), so Additional is an object
"Info":"having food"
}
},
{
"Type":"G", //Group by Date and Group Name.
"GroupName":"School", //If record has the same group name,
"StartDate":"20/12/2020 9:00am", //!!! Display the first entry’s start date
"Additional":[ // It is an array due to Group Name being a real value, the items in array is the record whose StartDate has the same Date value.
{
"StartDate":"20/12/2020 9:00am", //Display the start date
"Info":"Take bus"
},
{
"StartDate":"20/12/2020 9:30am",", //Display the start date
"Info":"On the beach"
}
]
},
{
"Type":"G",
"GroupName":"Class 1",
"StartDate":"20/12/2020 11:00am",
"Additional":[
{
"StartDate":"20/12/2020 11:00am",
"Info":"Restaurant "
},
{
"StartDate":"20/12/2020 13:00pm",
"Info":"Walk on Lane"
}
]
},
{
"Type":"I",
"StartDate":"20/12/2020 15:00pm",
"Additional":{
"Info":"In museum"
}
}
]
Would any one help me on this? Thank you so much.
I also attach the JSON data in this picture so the JSON format would be clearer.
There is a typo sorry, this is the new picture:
As per Microsoft SQL Server documentation, FOR JSON, will work best for you. It is supported on SQL Server 2016 and above.
SELECT CASE
WHEN GroupName is null THEN 'I'
ELSE 'G'
END as Type
,GroupName
,StartDate .....
FROM table
FOR JSON AUTO;
https://learn.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server?view=sql-server-ver15
When you form a JSON structure. Better to use with "INCLUDE_NULL_VALUES". So that it will have NULL for attribute If the value is missing.
SELECT Column1, Column2,... FROM Table FOR JSON AUTO, INCLUDE_NULL_VALUES

How to select specific fields on FaunaDB Query Language?

I can't find anything about how to do this type of query in FaunaDB. I need to select only specifics fields from a document, not all fields. I can select one field using Select function, like below:
serverClient.query(
q.Map(
q.Paginate(q.Documents(q.Collection('products')), {
size: 12,
}),
q.Lambda('X', q.Select(['data', 'title'], q.Get(q.Var('X'))))
)
)
Forget the selectAll function, it's deprecated.
You can also return an object literal like this:
serverClient.query(
q.Map(
q.Paginate(q.Documents(q.Collection('products')), {
size: 12,
}),
q.Lambda(
'X',
{
title: q.Select(['data', 'title'], q.Get(q.Var('X')),
otherField: q.Select(['data', 'other'], q.Get(q.Var('X'))
}
)
)
)
Also you are missing the end and beginning quotation marks in your question at ['data, title']
One way to achieve this would be to create an index that returns the values required. For example, if using the shell:
CreateIndex({
name: "<name of index>",
source: Collection("products"),
values: [
{ field: ["data", "title"] },
{ field: ["data", "<another field name>"] }
]
})
Then querying that index would return you the fields defined in the values of the index.
Map(
Paginate(
Match(Index("<name of index>"))
),
Lambda("product", Var("product"))
)
Although these examples are to be used in the shell, they can easily be used in code by adding a q. in front of each built-in function.

VueJS bind input to Date value

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.

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()}`
}
)