lundi 23 novembre 2015

how to add foreach for json with laravel?

I want to display my data using ajax. I tried it using php foreach and it works. but I want to convert it to ajax please advice here is my json output from my controller

{
    "id": 35,
    "date": "2015-11-17",
    "day": "Tuesday",
    "shifts": [
      {
        "user_id": 17,
        "time_sheet_id": 35,
        "week_id": 5,
        "shift_no": 1,
        "shift_start_time": "08:00:00",
        "shift_end_time": "10:00:00",
        "run_type": "S"
      },
      {
        "user_id": 17,
        "time_sheet_id": 35,
        "week_id": 5,
        "shift_no": 2,
        "shift_start_time": "10:00:00",
        "shift_end_time": "13:00:00",
        "run_type": "S"
      },
      {
        "user_id": 17,
        "time_sheet_id": 35,
        "week_id": 5,
        "shift_no": 3,
        "shift_start_time": "13:00:00",
        "shift_end_time": "15:00:00",
        "run_type": "S"
      },
      {
        "user_id": 17,
        "time_sheet_id": 35,
        "week_id": 5,
        "shift_no": 4,
        "shift_start_time": "15:00:00",
        "shift_end_time": "18:00:00",
        "run_type": "S"
      },

public function getWeek(Request $request) {

//get matching data for shifts :: user,shift time, hours
$user_id = $request->input('user_id');
$week = $request->input('week_starting_date');

$dataWeek = Timesheets::select('id','date','day')->where('week_id', '=', 5)->where('user_id', '=', 17)->With('shifts')->get();

return $dataWeek;

}

and below I mentioned the ajax code I tried

  $(document).ready(function () {
            $("#week_starting_date, #user_id").change(function () {
                var dd = $('#week_starting_date :selected').text();
                var week_starting_date = $('#week_starting_date option:selected').val();
                var user_id = $('#user_id option:selected').val();
                var date = $('#timesheet_date').val();

                    $.ajax({
                        url: "{{url ('getWeek')}}",
                        type: "GET",
                        dataType: "HTML",
                        async: false,
                        data: {week_starting_date: week_starting_date, user_id: user_id},
                        success: function (data) {
                            var json_obj = $.parseJSON(data);//parse JSON


                            var output = "<tr>";

                            for (var i in json_obj) {


                                var url = "{{url('edit-shift')}}/" + json_obj[i].id;
                                var del_url = "{{url('destroy-shift')}}/" + json_obj[i].id;



                                output +=

                                        "<td>" + json_obj[i].id + "</td>" +
                                        "<td>" + json_obj[i].date + "</td>" +
                                        "<td>" + json_obj[i].day + "</td>" +


                               "<td><a data-toggle='modal'  data-target='#myModal3' href='" + url + "' ><span class='glyphicon glyphicon-pencil '></span></a><a href='" + del_url + "' ><button type='submit' id='delete-btn' onclick='return confirm('This action cannot be undone . Are you sure you want to delete this account ?')'><span class='glyphicon glyphicon-trash'></span></button></a></td>";


                                output += "</tr>";



                            }

                            $('#span2').html(output);


                        }
                    });

            });

        });

this is I tried

here attached my current view and the view which I want to create. kindly please advice to proceed

current display enter image description here

expected view enter image description here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire