yahhoo
yahhoo

Reputation: 11

laravel about How to obtain Additional Data by Yajra\DataTables (Additional Data Response)

The following site explains how to send data from the server, but not how to retrieve data on the front-end side. How can I retrieve the data on the front-end side?

https://yajrabox.com/docs/laravel-datatables/master/response-with

use DataTables;

Route::get('user-data', function() {
    $model = App\User::query();

    return DataTables::eloquent($model)
                ->with('posts', 100)
                ->with('comments', 20)
                ->toJson();
});

Example Response

    {
        "draw": 1,
        "recordsTotal": 10,
        "recordsFiltered": 3,
        "data": [{
          ----\(^o^)/------
        }],
        "posts": 100,   <----this (^o^)I want to get it on the blade side.
        "comments": 20  <----this (^o^)I want to get it on the blade side.
 }

BLADE SIDE (I want to use the data when describing the footer.)

"footerCallback": function(row, data, start, end, display) {

        \(^o^)/I want to get it here.

}

Other

var oTable = $('#example').DataTable({
    // searching: false,
    serverSide: true,
    ajax: {
        url: "{{ route(´ε` ) }}",
        data: function(d) {
            d.search = $('#search').val();
        },
        dataFilter: function(data) {
            return data;

           \(^o^)/I tried my best here.

                    var json = jQuery.parseJSON(data);
                    console.log(json.posts);

           \(^o^)/The posts data exists.
             
        }
    },
    columns: [{
    }],
    responsive: true,
    stateSave: true,
    lengthMenu: [
        [7, 10, 15, -1],
        [7, 10, 15, "All"]
    ],
    displayLength: 15,
    // scrollX: true,
    // scrollY: 200,
    "footerCallback": function(row, data, start, end, display) {

          \(^o^)/I want to get it here.

            console.log(row);
            console.log(data);
            console.log(start);
            console.log(end);
            console.log(display);

         (;´Д`) Not found..
    },
    "createdRow": function(row, data, key) {
    },

}); $('#example').on('click', 'button.myShow', function() {
    var tr = $(this).closest('tr');
    var row = oTable.row(tr);
    console.log(row.data()); });

$('#search_form').on('submit', function(e) {
    oTable.draw();
    e.preventDefault(); }); });

Upvotes: 1

Views: 1665

Answers (1)

edinzujo
edinzujo

Reputation: 11

This is how you catch the data. I think you can catch it inside footerCallback as well.

Example:

initComplete: function (data) {
    console.log(data.json.posts)
    console.log(data.json.comments)
}

Upvotes: 1

Related Questions