簡體   English   中英

Laravel為數據表使用數據

[英]Laravel working with data for datatables

我有一個UsesController,當前看起來如下所示,我試圖找出什么是對我的數據進行處理的最佳方法。 截至目前,我只是從數據庫中檢索所有用戶,並將其傳遞到視圖,然后在foreach循環中遍歷數據,從而在HTML表中創建新的表行。 然后在我的javascript中將該表初始化為數據庫表。 走這條路線是否有任何弊端,或者我應該將其轉變為各種API或哪種建議對我有幫助。

<?php

use MyApp\Services\UserCreatorService;

class UsersController extends BaseController {

    protected $userCreator;

    public function __construct(UserCreatorService $userCreator)
    {
        parent::__construct();
        $this->userCreator = $userCreator;
        $this->beforeFilter('role:Administrator.Owner');
    }

    /**
    * Display a listing of users
    *
    * @return Response
    */
    public function index()
    {
        // Retrieve all users from database with roles and statuses
        $users = User::with('role')->with('status')->get();

        // Return a view to display all users by passing users variable to view.
        return View::make('users.index', compact('users'));
    }
}

<table class="table table-striped table-bordered responsive resourceTable">
    <thead class="">
        <tr>
            <th class="center">ID</th>
            <th>Name</th>
            <th>Email Address</th>
            <th>Username</th>
            <th>Role</th>
            <th>Status</th>
            <th class="nosortable center">Actions</th>
        </tr>
    </thead>

    <tbody>
        @foreach($users as $user)
            <tr>
                <td class="center">{{ $user->id }}</td>
                <td>{{ $user->getFullName() }}</td>
                <td>{{ $user->email_address }}</td>
                <td>{{ $user->username }}</td>
                <td>{{ $user->role->role_name }}</td>
                <td>{{ $user->status->status_name }}</td>
                <td class="center">
                    <!-- TODO: Figure out how to make a function with actions td.-->
                    @if ( $user->role['id'] < $currentUser->role['id'] )
                        <a data-original-title="Edit" href="{{ route('users.edit', $user->id) }}" data-toggle="tooltip" title="" class="tooltips"><i class="fa fa-pencil"></i></a>
                        <a data-original-title="Delete" href="{{ route('users.destroy', $user->id) }}" data-toggle="tooltip" title="" class="tooltips ajax-delete"><i class="fa fa-trash-o"></i></a>
                    @endif
                    <a data-original-title="Profile" href="{{ route('users.show', $user->id) }}" data-toggle="tooltip" title="" class="tooltips"><i class="fa fa-eye"></i></a>
                </td>
            </tr>
        @endforeach
    </tbody>
</table>

// Set defaults for all resource tables.
$.extend( $.fn.dataTable.defaults, {
    "aoColumnDefs": [
        { "bSortable": false, "aTargets": [ "nosortable" ] }
    ],
    "pagingType": "full_numbers"
});

$(document).ready(function() {

    //we define the table in a global variable so we can later manipulate it...
    resourceTable = $('.resourceTable').dataTable();

    var $addButton = $('<button class="btn btn-primary myActionButtons" id="addNew">Add New</button>');

    /* TODO: Find out if this is the best place for a add new button globally on table pages. */
    $('.dataTables_filter').parent().append($addButton);
});

$(document).on('click', '#addNew', function(e) {
    e.preventDefault();
    window.location.replace(window.location.href + '/create');
});

好吧,您的方法沒有錯。 但實際上,我建議您將大數據分頁。 它將大大提高性能和速度。

關於分頁,請使用Laravel代替js或jQuery。 您可以嘗試一下自己,並會發現通過Laravel分頁可以提高性能。

對於大量數據API是更好的解決方案。 傑弗里(Jeffry)解釋了為什么不好的做法使用$users = User::with('role')->with('status')->get(); 檢查此鏈接它會幫助您LINK

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM