Laravel Datatables 1 => active / 0 =>不活跃

时间:2015-08-27 20:40:43

标签: laravel datatables

我正在使用Laravel Framework和yajrabox数据表。

我有一个包含2列的用户表,状态列和帐户列类型。这两个都包含在内。

如何将这些整数更改为字符串?如何对cntroller中的$ users变量的值执行db请求?

还可以将2列连成1吗?那么first_name和last_name分为1列?

我目前的设置:

userlist.blade.php

@extends('app')
@section('content')
<div class='portlet light bordered '>
    <div class='portlet-title'>
        <div class='caption font-red-sunglo'>
            Ledenbeheer
        </div>
        <div class="actions">
            <a id='create' href="javascript:;" class="btn btn-circle red-sunglo">
                Nieuw lid </a>
            <a id='excel_import' href="javascript:;" class="btn btn-circle btn-default">
                <i class="fa fa-fax"></i>Excel import </a>
        </div>
    </div>
    <div class='portlet-body'>
        <table class="table table-bordered table-striped table-hover" id="users-table">
            <thead>
            <tr>
                <th>Naam</th>
                <th>Email</th>
                <th>Status</th>
                <th>Account type</th>
                <th>Created At</th>
                <th>Updated At</th>
            </tr>
            </thead>
        </table>
        </div>
    </div>
@stop
@push('scripts')
<script>
$(function() {
    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '{!! route('configuratie_leden.data') !!}',

    });
});
 </script>
 @endpush

UserController.php

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use yajra\Datatables\Facades\Datatables;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
//
public function getIndex(){

    $users =  User::all();

    return view('users.userlist');
}

public function anyData(){

    $users = User::select(['first_name','email','status','account_type','created_at','updated_at']);

    return Datatables::of($users)->make();
}

}

路线:

// Routes module configuratie
Route::controller('configuratie_leden','UserController',
[
   'getIndex' => 'configuratie_leden',
    'anyData' => 'configuratie_leden.data'
]);

1 个答案:

答案 0 :(得分:1)

Javascript解决方案:

columns: [
    { 
        data: 'status', 
        name: 'status',
        render: function ( data, type, full, meta ) {
           return data ? "active" : "not active" ;
        }
    }
]

这就是你可以将int更改为string的方法。当数据为1时,它将打印“活动”。

  

还可以将2列连成1吗?那么first_name和last_name分为1列?

是的。您可以在查询中执行此操作。

$users = User::select([
        DB::raw("CONCAT(users.name,'-',users.surname) as name_surname"),
        'email',
        'created_at',
        'updated_at'
    ]);

示例:http://datatables.yajrabox.com/eloquent/post-column-search