Dynamically format all date columns

Dynamically format all date columns

gsandersgsanders Posts: 6Questions: 2Answers: 0

I am building a reporting screen that will dynamically build small reports and displays them using Datatables. I would like to have an easy way to automatically render all date columns in the same format. Since the columns will change based upon the report, simply using a column render is not feasible. CAn I use a type detect plugin for this?

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Without seeing your data it's hard to suggest a solution, but this example may help. If not, we're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • gsandersgsanders Posts: 6Questions: 2Answers: 0
    edited December 2020

    The test page is not on a publicly available server. I'll try and describe the issue a little more fully. The page is a screen that will allow users to pick from a variety of reports , I intend to then allow them to use the Searchbuilder plugin to pull the subset of data they desire. Each report will have a completely unique set of columns. For example , one report may return the following columns
    UserName (string), TaskCompleted (Date), Title(string), EstimatedHoursRequired, HoursLogged(float)

    And another might be ,
    ProjectID (integer) , Title (string) , System(String), Priority(integer) , DateEntered(date) ,DateClosed(date) , Status(string)

    For the most part the dates will all be date objects in the JSON, I want to format them as M/D/Y . I wanted to avoid using column renders because that requires me to deploy new javascript every time a new report is added to the page.
    Does that help?

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    There's a couple of ways to convert dates - see examples here and here - using Moment.js, as it's excellent for all time/date based operations. Hopefully those will get you going,

    Colin

  • gsandersgsanders Posts: 6Questions: 2Answers: 0

    Unfortunately both of those examples use column renders which require the initialiasation to be aware of which columns are dates and that changes with every report. I believe I've found a work-around by doing a quick iteration through the first row of data to find which columns are dates and building a columDef object . Thanks for your help.

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    At the moment there isn't a way to set an output formatter based on type (although it is something I've actually considering). The approach you've taken is one way to do it - the other is to use an columns.render function which will check if the string given to it is an ISO time stamp. If so then render it - if not then just return it. That rendering function would then be applied to all columns.

    Allan

  • gsandersgsanders Posts: 6Questions: 2Answers: 0

    Thanks Allan. I may try a render function for all columns as that would let me check a few other things at the same time.

This discussion has been closed.