Organizing Dates in a Kendo Grid: How to Filter and Sort a DateTime Field

I am working with a datetime column in my Kendo Grid that displays values like this: 2014-04-11 12:43:41.720

To simplify the display, I have changed the template to show only short dates: 04/11/2014

The reason for not sending preformatted data to the columns is so that the time is considered when sorting. However, I face an issue when filtering using an "equal to" filter. If I select 4/11/2014 from the datepicker, no results are shown because the default time is 12:00:00.00.

Is there a way to filter based on the text rather than the value? Alternatively, can I send preformatted dates to the grid and use a different field for sorting?

This is a snippet of the JavaScript code for my column:

columns: [
    { 
      field: "CREATEDATE",
      title: "Created",
      width: 78,
      template: '#= kendo.toString(kendo.parseDate(CREATEDATE, "yyyy-MM-dd"), "MM/dd/yyyy") #',
      filterable: true,
      attributes: { style: "text-align:center;" }
    }
]

Answer №1

Check out the detailed guide provided by Telerik forum on how to implement Grid filters with date and time; you can download it here.

If you're looking for a ready-made solution, they also offer the code snippet needed to customize Kendo filter operation in their example. This includes using JavaScript/jQuery to override the default filter behavior and create your own rules.

The core concept is to construct a new date object based on specific parts of the date that are relevant to your needs, such as day, month, and year.

<html>
<head>
    <title>Dynamically alter date format</title>
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script src="http://cdn.kendostatic.com/2012.1.515/js/kendo.all.min.js"></script>
    <link href="http://cdn.kendostatic.com/2012.1.515/styles/kendo.common.min.css" rel="stylesheet" />
    <link href="http://cdn.kendostatic.com/2012.1.515/styles/kendo.default.min.css" rel="stylesheet" />
</head>
<body>

    <input id="dropDownList"></input>
    <br />
    <div id="grid"></div>

    <script>
        // JavaScript logic here
    </script>
</body>
</html>

Answer №2

After receiving a helpful solution from jwatts regarding filtering, I decided to share an additional answer for the benefit of others who may encounter a similar issue. In the process, I made some adjustments to my code to resolve a secondary question:

"Can preformatted dates be sent to the grid for sorting using a different field?"

In the end, I opted to utilize preformatted dates in my grid as column values for easier filtering without having to deal with timestamps. To tackle the sorting problem, I modified my controller logic during rebind by checking the DataSourceRequest for any sort parameters. If the user was sorting based on the preformatted date column, I changed it to use the complete datetime column (which was hidden).

if (request.Sorts != null)
{
    foreach (var sort in request.Sorts)
    {
        if (sort.Member.Equals("CREATEDATE_FORMATTED", System.StringComparison.OrdinalIgnoreCase))
        {
            sort.Member = "CREATEDATE";
        }
    }
}

Answer №3

Although this post may be old and already resolved, I came across it while looking for a solution to a similar question. I found an easier way to address the second part of your query: "can I have the sort use a different field." The KendoUI data grid provides a configuration option that allows customization for this purpose.

For instance, if you want to sort a column based on both user name and date, but only by date, you can do so using the following approach:

 gridColumns: [
                { field: 'title', title: 'Title' },
                { field: 'changedByDetail', title: 'Changed',
                    sortable: {
                        compare: function (a, b, desc){
                            if (a.changedDateTime < b.changedDateTime) {
                                return -1;
                            }
                            else if (a.changedDateTime > b.changedDateTime) {
                                return 1;
                            }
                            return 0;
                        }
                    }
                }
            ]

In this scenario, the 'changedDateTime' field contains the datetime values in the datasource. If not, you could utilize a string parsing function to extract the datetime information, convert it to a date format, and then compare the dates accordingly.

I hope this explanation proves helpful to someone!

Answer №4

If you're looking for a way to display time that is both filterable and sortable, here's a solution I've implemented in my KendoUI jquery grid. This method allows you to filter by minutes:

{ field: "CREATEDATE", 
  title: "Date Created",
  format: "{0:dd/MM/yy hh:mm tt}",
  width: "150px",
  template: "#= kendo.toString(kendo.parseDate(CREATEDATE, 'yyyy-MM-dd hh:mm tt'), 'dd/MM/yyyy hh:mm tt') #" },

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The hover effect is not altering the color

$(document).ready(function() { $(window).scroll(function() { var scroll = $(window).scrollTop(); if (scroll > 200) { $(".nav-bg").css({ "background": "#fff", ...

Exporting an indexed geometry with a draw range using GLTFExporter in Three.js: What's the best approach?

I am encountering a specific issue where I need to export an indexed geometry with a draw range. Unfortunately, the GLTFExporter does not support this feature, as indicated by the comment in the code: // @TODO Indexed buffer geometry with drawRange not su ...

Retrieving information through a loop in Next.js

My goal is to utilize the data retrieved from one endpoint to make a call to another endpoint, filtered by ID. I intend to fetch both calls using getServerSideProps and then pass the data to a different component. The initial call will result in an array ...

Display JSON information in a table using AngularJS

As I delve back into an old project, I've encountered a hurdle. My goal is to display some data in a table, but I seem to have forgotten the intricacies of working with JSON objects and Angular. The API response I'm receiving looks something lik ...

A guide on customizing bar colors in a jqPlot stacked bar graph

Is there a way to customize the colors for individual bars in a jqPlot stacked bar chart? I've searched through examples, but they all seem to use the default colors. How can I explicitly set different colors for each bar? Here is the current code sn ...

Tips for accurately measuring the height of a single table cell

I am facing an issue with a table that I have set up. Here is the code: <table> <tr> <td id='tdleftcontent' style='border:1px solid red;'> <asp:Label ID='lbl' runat="server"></asp:Label> < ...

Difficulty with SailsJS Transmitting Information to View Template

I've been trying to establish a connection for hours but haven't had any luck. All I want to do is transfer some data from a controller to a view template. When I navigate the route without specifying a view template, the browser displays the da ...

Changing the time zone of a browser using JavaScript or jQuery

After researching numerous discussions on the topic, it appears that changing the browser's timezone programmatically is not possible. Although the moment-timezone.js library allows us to set timezone for its objects, it does not affect the browser&ap ...

"Obtaining a URL that begins with using jQuery: A Step-by-

How can I extract the full URL that starts with http://sin1.g.adnxs.com Here is the code snippet: <div id="testingurl"> <div class="ads98E6LYS20621080"> <!-- This script is dynamically generated --> <iframe src="http://testing ...

Is there a way to transform integers into their matching strings within Vue markup?

I have a database table where I retrieve data. The "status" field is used to manage the information in this table. If the status is equal to 1, it indicates "Active." If the status is equal to 2, it signifies "Completed." If the status is equal to 0, it r ...

Display Content in a DIV When Form Field is Unfocused

After hours of searching, I still haven't found a solution! I am trying to create a form field (text) where users can type in text. I want the text they enter to appear in a div on the screen either as they type or after they finish typing. Maybe thi ...

Understanding the Document.ready function?

Recently, I came across some websites that follow this specific pattern: <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script> $(function (){...do some stuff with p ...

Control the playback of individual HTML5 audio elements using jQuery for seamless user experience

How can I modify the code to ensure that only one HTML5 audio element plays at a time? Currently, all tracks are able to play simultaneously. I am using jQuery for play and pause functionalities. If one track is playing and another is clicked on, how can ...

Finding the position of a specific element in an array by searching through multiple object keys

Assuming I have a single key (for example, if I have the object.name = 'Sam') and I want to find the index using: var index = array.map(function(el) {return el.name}).indexOf('Sam'); I can retrieve the index of the array element with ...

Obtain a string value from a JavaScript object

My dilemma involves a specific Javascript object. { A: 1, B: 2, C: 2, D: 1, E: 1, F: 4, G: 6, H: 2 }, The goal is to extract a four-letter string based on the key with the highest value, but there are limitations. The stri ...

Extensive application featuring a complex form built with react-redux

Recently, I've been tasked with revamping a module at my company using react. This module consists of a single page that is made up of 4-5 different forms. The selections made in each form determine the appearance of the following form step. While th ...

What is the best way to implement a custom NgbDateParserFormatter from angular-bootstrap in Angular 8?

Currently, I am working on customizing the appearance of dates in a form using Angular-Bootstrap's datepicker with NgbDateParserFormatter. The details can be found at here. My goal is to display the date in the format of year-month-day in the form fi ...

Create an immediate impact by injecting CSS

I currently have the following code set up: style.js: function applyStyle(str) { var element = document.createElement('style'); element.innerHTML = str; document.body.appendChild(element); } var style = 'body {' style ...

A guide on creating a clickable polygon in Vue.js using Konva

Is there a way to create an interactive polygon in Vue Konva where I can set each corner with a click? I haven't been able to find any resources other than the v-polygon which only creates predefined polygons. ...

In React conditional return, it is anticipated that there will be a property assignment

What is the optimal way to organize a conditional block that relies on the loggedIn status? I am encountering an issue with a Parsing error and unexpected token. Can someone help me identify what mistake I am making and suggest a more efficient approach? ...