Null date column

1 Answer 4188 Views
Grid
Alison
Top achievements
Rank 1
Alison asked on 22 Feb 2013, 06:09 AM
Hi

I've done some searching but can't find a solution to this.

Essentially, I have a settlement date column in my grid, however, this column can be null because the settlement date may not have been set yet.

How do I show a date if there's a date entered, blank string if there's no date entered yet?

I'm using JSON data in my datasource. I couldn't find a way to bring NULL dates in JSON so I'm currently bringing in the NULL dates as 01/01/0001 (which, by searching, seems to be the earliest date in JSON).

My column at the moment is:
columns: [
{ field: "SettlementDate", width: "150px", title: "Settlement Date", format: "{0:d/MM/yyyy}", filterable: { ui: "datetimepicker" } }
]
I want to allow batch editing of the grid.

Any suggestions or direction from someone who overcame a similar problem would be appreciated.

Thanks, Alison

1 Answer, 1 is accepted

Sort by
0
Accepted
Alexander Valchev
Telerik team
answered on 25 Feb 2013, 02:44 PM
Hi Alison,

By default the grid will display empty string if the field has a null value. If in your case the data is not null, you may use a template with JavaScript expression to display a value according to your requirements. For example:
{ field: "date", template: "#= <condition> ? kendo.toString(date, 'd/MM/yyyy') : " " #" }

Even though that the template will do the job, I recommend you to revise your server side implementation and return null instead of "01/01/0001". In this way you would be able to get the described behaviour out of the box.

Regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Alison
Top achievements
Rank 1
commented on 25 Feb 2013, 10:57 PM

Thanks Alexander

I managed to get the JSON to pass through a null.

So, the .asmx file ended up...

Public Class cDetail
    Public SettlementDate As DateTime?
End Class
<WebMethod> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False)> _
Public Function getDetail() As cDetail()
    Dim items As New List(Of cDetail)
    .... omitted
    Using oConn As New SqlConnection(ConfigurationManager.ConnectionStrings("conn").ToString())
        .... omitted
        Dim item As New cDetail()
        .... omitted
        Dim dteTemp As Date
        If (Date.TryParse(dr.Item("SettlementDate").ToString(), dteTemp) = True) Then
            item.SettlementDate = dteTemp
        End If
        .... omitted
    End Using
 
    Return items.ToArray()
End Function

I guess there's a lesson in there to not keep trying to get something to work last thing on a Friday - you tend to miss the obvious :).

Regards, Alison

Victor
Top achievements
Rank 1
commented on 09 Mar 2017, 04:09 AM

Is there a function that can encapsulate this logic "#= <condition> ? kendo.toString(date, 'd/MM/yyyy') : " " #"?  I have multiple values that need to check if it is null and it just seems that this logic can be called once and reused. 
Dimiter Topalov
Telerik team
commented on 09 Mar 2017, 03:49 PM

Hi Victor,

The columns.template option can be set to a function that takes the dataItem as an argument and returns the string to be rendered in the respective Grid cell, e.g.:

http://dojo.telerik.com/EfIcU

I hope this helps.

Regards,
Dimiter Topalov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Victor
Top achievements
Rank 1
commented on 09 Mar 2017, 04:41 PM

Thanks for the example.

How can I get this same function to work in a Custom Template in an MVC Kendo Grid?  Here is a snippet of code.

 

<div class='col-lg-4 col-md-4 col-sm-4'>
<label class="tab-strip-label">Lot</label>
<span class="tab-strip-span">#= LotNumber ==null ? "" : LotNumber #</span>
</div>

I want to do something like this and get an empty string.

<div class='col-lg-4 col-md-4 col-sm-4'>
<label class="tab-strip-label">Lot</label>
<span class="tab-strip-span">#= ToString(LotNumber) #</span>
</div>

Boyan Dimitrov
Telerik team
commented on 13 Mar 2017, 01:43 PM

Hello Victor,

I believe that what you are looking for is How to Apply Conditional Logic in Column Client Templates?

Regards,
Boyan Dimitrov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Victor
Top achievements
Rank 1
commented on 16 Mar 2017, 04:26 AM

I am looking to use a function in javascript similar to this c# method.

public string GetValue(string e)
{
            return (e == null) ? "" : e;
}

And in my client template I want to call it like this.

<span class="tab-strip-span">#= GetValue(LotNumber) #</span>

Instead of like this

<span class="tab-strip-span">#= LotNumber ==null ? "" : LotNumber #</span>

 

 

Boyan Dimitrov
Telerik team
commented on 17 Mar 2017, 03:57 PM

Hello Victor,

Please refer to the Handle External Templates and Expressions and specifically take a look at the second example. 

Regards,
Boyan Dimitrov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Alison
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Share this question
or