0

I have this array:

Array
(
    [0] => Array
        (
            [start_break] => 2019-05-23 09:01:23
            [end_break] => 2019-05-23 09:04:36
            [time_diff] => 0.05
        )

    [1] => Array
        (
            [start_break] => 2019-05-23 12:05:33
            [end_break] => 2019-05-23 12:07:19
            [time_diff] => 0.03
        )

)

I would like to pass it via AJAX and create a table in a modal. So far, I have this JS code:

    $.ajax({
        type: 'GET',
        url: 'get-breaks',
        data: {
            t_date: $t_date,
            emp_id: $emp_id
        }
        success: function (data) {
            //append to table
        },
        error: function (data) {
        }
   });

And this HTML table:

<table id="breaks-table">
   <tr>
     <th>Start Break</th>
     <th>End Break</th>
     <th>Time Difference</th>
   </tr>
</table>

I would like to append the array contents to the table body. I hope someone can help.

1
  • Can you convert array to json and pass like post parameter and use in your AJAX. Commented Jun 19, 2019 at 12:35

1 Answer 1

1

i assume successfully get array from controller

after get try like that

$.ajax({
        type: 'GET',
        url: 'get-breaks',
        data: {
            t_date: $t_date,
            emp_id: $emp_id
        }
        success: function (data) {
            //append to table
            if(data.dateArray) {
              data.dateArray.forEach(function(item,index) {
                      $("table#breaks-table").append(createTr(item));
              });
            }
        },
        error: function (data) {
        }
   });



function createTr(item) {
    var tr = '<tr>' +
           '<td class="className">' + item.start_break + '</td>' +
           '<td>' + item.end_break + '</td>' +
           '<td>' + item.time_diff + '</td>' +
        '</tr>';

    return tr;
 } 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.