How to return datetime to use in a function?

How to return datetime to use in a function?

BradleyO07BradleyO07 Posts: 13Questions: 10Answers: 0

I am working on a reporting project for forklifts at my company. The reports from the forklift are done by the operators and are uploaded automatically to our database. I created a table to display these reports that will show a pass or fail. Currently what I am doing is displaying every report one by one. I would like for the report to show just the time, operator and asset. Then when you click a link, within the table, it will display all the reports for that timeframe. Currently when I click on the link I get an error code that just returns "viewReport(2023-07-22 07:57:58)" with an error that says "Uncaught SyntaxError: missing ) after argument list". This is how I currently have my columns set up within my datatable.

columns: [
      { data: null, render: function (data, type, row){
        return "<a class=\"default-link\" onClick=\"viewReport(" + data.datetime + ")\">" + data.datetime;
      } },
      { data: "operator" },
      { data: "asset" },
      { data: "inspection" },
      { data: "status",
        render: function(data,type){
          if (type === 'display'){
              if(data === 'pass'){
                return "<span style='color:green'>\u2714</span>";
              } else if (data === 'fail'){
                return "<span style='color:red'>\u2718</span>";
              }
            return data;
          } 
          return data;
        } 
    },
    ],

What could I add to my code to allow my code to return the date/time and allow me to display all the reports with that date/time? As I said currently the code it return the full date and time but I believe it is trying to subtract the numbers instead of seeing them as a string.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974
    edited November 2023 Answer ✓

    viewReport(2023-07-22 07:57:58)

    You will need to place the datetime value inside quotes. Since you are using double quotes for the a attributes you will need to use single quotes to turn the datetime into a string, something like this:

    return "<a class=\"default-link\" onClick=\"viewReport('" + data.datetime + "')\">" + data.datetime;
    

    It is recommended to use delegated events instead of onclick() to create click events as shown in this example. I built a simple example, based on your code, to demonstrate.
    https://live.datatables.net/rapuyaxo/1/edit

    Kevin

Sign In or Register to comment.