Showing dates in handsontable

Here is the relevant section of what I currently have.

For Handsontable options:

data: [[new Date(2013,5,26,17,00,00),24.7025,null,29.018950276,19.7746530531,null,null,null,null,null,null,55,110,165,220], ...

columns: [{type:'date', dateFormat: 'mm/dd/yy'},{type: 'numeric',format: '0,0.00'}, ...

The displayed date in the handsontable cell appears as:

Wed Jun 26 2013 17:00:00 GMT+0200 (Romance Daylight Time)

I am trying to change it to "dd-MM-yyyy HH:mm" format without success. The default date format persists despite attempts to modify it.

Answer №1

If you encounter this situation, a custom renderer can be utilized.

To address this issue, I have developed jquery.handsontable.exts.js and it is being imported alongside handsontable.js.

This specific snippet of code employs either the renderFormat or dateFormat property to accurately display dates.

    /**
    * Custom Handsontable date renderer
    * @param {Object} instance Handsontable instance
    * @param {Element} TD Table cell where the rendering occurs
    * @param {Number} row
    * @param {Number} col
    * @param {String|Number} prop Property name from the row object
    * @param value Value to render (ensure unsafe HTML characters are escaped before insertion into DOM!)
    * @param {Object} cellProperties Cell properties shared by renderer and editor
    */
    Handsontable.DateRenderer = function (instance, TD, row, col, prop, value, cellProperties) {
        if (value && typeof value === "object" && value.constructor === Date) {
            // It's a Date!
            var format = cellProperties.renderFormat || cellProperties.dateFormat || "";
            value = $.datepicker.formatDate(format, value); //value.format(format);
        }
        if (cellProperties.readOnly)
            Handsontable.TextRenderer(instance, TD, row, col, prop, value, cellProperties);
        else
            Handsontable.AutocompleteRenderer(instance, TD, row, col, prop, value, cellProperties);
    };

    Handsontable.DateCell.renderer = Handsontable.DateRenderer;
      Handsontable.cellLookup.renderer.date = Handsontable.DateRenderer;

Answer №2

After a lengthy search, I finally stumbled upon the solution in the Handsontable documentation. Despite my initial doubts, I decided to share it here...

hot = new Handsontable(container, {
    data: getDataFromSomewhere(),
    colHeaders: ['SomeText', 'SomeDate'],
    columns: [
      {
        // The first cell contains simple text with no special configurations
      },
      {
        type: 'date',
        dateFormat: 'MM/DD/YYYY',
        correctFormat: true,
        defaultDate: '01/01/1900'
      }
    ]
});

The downside is that you'll need to implement three additional libraries:

  • /dist/moment/moment.js
  • /dist/pikaday/pikaday.js
  • /dist/pikaday/css/pikaday.css

This setup also includes a daypicker by default (which wasn't my preferred choice... I needed to include HH:mm as well).

Answer №3

    display: [
        { info: 'Edited', transform: dateTransformer, lockEditing: true },
    ],

function dateTransformer(dataInstance, tableData, record, column, property, dataValue, cellOptions) {
    dataValue = typeof dataValue === "undefined" ? '' : new Date(dataValue).toLocaleString();
    return Handsontable.renderers.TextRenderer(dataInstance, tableData, record, column, property, dataValue, cellOptions);
}

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

Setting a variable prior to receiving feedback in Javascript

Uncertain of the appropriate query, but I will attempt to elucidate in my current operational code: var container, loader switch(section){ case 'A': container = $('#list .container') loader = $('#surveylist&apo ...

Selecting a Child Component in Vue.js: A Guide to Specifying the Correct Component

Within my application, I have a variety of components that are either generic or specific to certain brands. For example, I have two brand-specific components named Product_brand_A.vue and Product_brand_B.vue, both of which I want to display in a list form ...

What strategies can I use to handle components that rely on changes in the angular $state when launching a new window via $state.href?

I need to show a specific angular view in a new window without certain elements like the header or footer displayed. Currently, I am accomplishing this by checking for the current state name in the layout view and using $window.open with $state.href: In ...

Utilizing JavaScript to control the visibility of a div based on radio button selection without the need

I am facing an issue with my HTML code that is only partially working. I am attempting to display text inputs based on a radio button selection. At the moment, the code successfully hides the other options when a radio button is clicked, but upon the ini ...

transforming JSON key names by associating them with their corresponding JSON values and modifying the key names

My challenge lies in converting a JSON string from one format to another. The initial format is: [ { clientIDs: "WELL #6", analyteIDs: [ "7440-62-2", "7440-28-0" ] } ] ...

refresh polymer components or make an ajax request within a custom element

I've been spending days on this issue with no success! My application relies on cookies for session handling and includes multiple custom elements imported in the header. Some of these elements need to access information from the 'cookie session& ...

DreamFactory's REST API POST request for rest/user/session consistently encounters errors in Internet Explorer 9

In Firefox, Chrome, and Safari, the initial POST rest/user/session request works perfectly fine. However, in Internet Explorer 9, it consistently returns an error. When the dataType is specified as "json," IE9 encounters a 'no transport' error w ...

Is it possible to apply a delay function to JQuery's AjaxStart/Stop events?

As I was working on my single-page app, I thought about adding a spinner and gray background to provide feedback to users while processes are loading. I discovered JQuery's AjaxStart() and AjaxStop() functions, which allow me to display the spinner du ...

Having trouble dismissing Bootstrap alerts in TypeScript?

There seems to be an issue with the close button in a simple piece of code. The alert is displayed correctly, but clicking on the close button doesn't close the alert. Currently, I am using bootstrap 5.2.3. My code consists of a basic main file and an ...

Leveraging Javascript to generate universal HTML content for various Javascript files

Present Situation: I have a timesheet feature that enables users to input their leave, TOIL, and sick days along with the respective hours. Additionally, there is a table that dynamically adds a new row every time the plus button is clicked using the foll ...

How to utilize a parameter value as the name of an array in Jquery

I am encountering an issue with the following lines of code: $(document).ready(function () { getJsonDataToSelect("ajax/json/assi.hotel.json", '#assi_hotel', 'hotelName'); }); function getJsonDataToSelect(url, id, key){ ...

Struggling to design a functional mobile keyboard

I've been working on creating a mobile keyboard, but I'm facing some issues with the JavaScript part. The problem seems to be in my code as the keyboard isn't responding when I try to type. Here's a snippet of the JavaScript I'm us ...

React has identified a modification in the sequence of Hooks invoked, however, I am unable to locate any Hooks being invoked conditionally

I am currently utilizing: ReactJS 16.14.0 In my React functional component, I am relying on data stored in context for accurate rendering. Certain data requires additional processing before display, and some data needs to be fetched. However, I am encoun ...

Take two inputs, divide them, and then multiply the result by 100

I am currently troubleshooting the total project match function, as it is not working properly. I aim to have this function divide the first function by the second function and then multiply the result by 100. Here is a snippet of my code: matchContribu ...

Enhancing multiple documents in mongoDB with additional properties

I am working with a data structure that includes user information, decks, and cards. I want to update all the cards within the deck named "Planeten" by adding a new property. How can I achieve this using a mongoose query? { "_id": "5ebd08794bcc8d2fd893f ...

Would it be better to lock a variable stored in req.session, or opt for a massive global variable instead?

As I delve into creating a game using node.js, the premise is sending units on missions and waiting for their return. The challenge lies in ensuring that the same unit cannot be sent on two different missions simultaneously, nor can two sets of units be di ...

Steps to set up gapi-script authentication with next-auth in a Next.js application

I have encountered an issue while working on my open-source project that involves utilizing the Google Drive API. Can anyone guide me on how to set up gapi-script authentication using next-auth in a Next.js project? I have managed to implement authentica ...

Leveraging JSON data to dynamically create HTML elements with multiple class names and unique IDs, all achieved without relying on

Recently, I've been working on creating a virtual Rubik's cube using only JS and CSS on CodePen. Despite my limited experience of coding for less than 3 months, with just under a month focusing on JS, I have managed to develop two versions so far ...

JavaScript causing display issues with Unicode characters

convert.onclick = function() { for (var i = 0; i < before.value.length; i++) { after.value += "'" + before.value.charAt(i) + "', "; } } <textarea id="before" type="text" name="input" style="width:100%;">* ...

I'm perplexed as to why my JavaScript code isn't successfully adding data to my database

Recently, I delved into the world of NodeJS and Express. My goal was to utilize Node and Express along with MongoDB to establish a server and APIs for adding data to the database. Specifically, I aimed to write the code in ESmodules syntax for JavaScript. ...