How does Editor calculate value based on other data in DataTable?

How does Editor calculate value based on other data in DataTable?

saligiayyssaligiayys Posts: 12Questions: 7Answers: 0

Hi team,

I have a DataTable with start-time, end-time and worked hours.
How should I use Editor to enter or edit start-time and end-time to get the value of worked hours then render these three data in the table? Cold you please advise?
Thank you very much!

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,448Questions: 78Answers: 353
    edited November 21 Answer ✓

    Just take a look at the examples. You'll find lots of similar use cases there as well.
    https://editor.datatables.net/examples/index

    In your case you could send start and end time to the server. Calculate the worked hours on the server, have Editor do the database insert or update and have Editor reload the updated values including hours worked to the client. That would all happen automatically.

    You could also do the calculation on the client side but I wouldn't do it if you need to save the values in a database anyway.

    On the server side your Editor fields could look like this - assuming your wire format is the usual SQL-datetime format.

    Editor::inst( $db, 'yourTable' )
        ->field(
            Field::inst( 'start_time' ),
            Field::inst( 'end_time' )
                ->validator( function ( $val, $data, $opts ) {
                    //wire format e.g. '2016-11-30 03:55:06'
                    if ( $val <= $data["start_time"] ) {
                        return "end time must be later than start time.";
                    }
                    return true;
                } ),
            Field::inst( 'hours_worked' )
                ->setFormatter( function ( $val, $data, $opts ) {
                    //wire format e.g. '2016-11-30 03:55:06'
                    $datetime1 = new DateTime($data['start_time']);//start time
                    $datetime2 = new DateTime($data['end_time']);//end time
                    $interval = $datetime1->diff($datetime2);
                    //00 years 0 months 0 days 08 hours 0 minutes 0 seconds');//00 years 0 months 0 days 08 hours 0 minutes 0 seconds
                    return $interval->format('%Y years %m months %d days %H hours %i minutes %s seconds');
                    //you would need to transform the above into what you require in hours and minutes for example
                } )
        )
    
  • saligiayyssaligiayys Posts: 12Questions: 7Answers: 0

    Thank you very much! I will read all the documents again.

Sign In or Register to comment.