1

所以在这个应用程序Drawing belongsTo Customer中。我有数据表

            <table id='drawing-table' class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>Drawing number</th>
                        <th>Customer</th>
                    </tr>
                </thead>
            </table>

这表示$darwing->number$customer->title。加载信息我use yajra\Datatables\Datatables;

使用此 JS 方法加载数据:

$(function () {
    $('#drawing-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '{{route('drawings.datatable')}}',
        columns: [
            { data: 'number', name: 'number' },
            { data: 'customer.title', name: 'customer' },
        ]
    });
});

而这个 Laravel 方法:

public function datatable()
{
    $drawings = Drawing::select(array('drawings.id','drawings.number'));

    return Datatables::of(Drawing::with('customer')->select('*'))->make(true);
}

问题

  1. 如何使数据表搜索窗口可以使用$customer->title
  2. 如何将图纸编号和客户名称显示为链接?
4

3 回答 3

6
    public function datatable()
    {

        //reference customer table
        $drawings = DB::table('customers')
            // join it with drawing table
            ->join('drawings', 'drawings.customer_id', '=', 'customers.id')
            //select columns for new virtual table. ID columns must be renamed, because they have the same title
            ->select(['drawings.id AS drawing_id', 'drawings.number', 'customers.title', 'customers.id AS customer_id']);

        // feed new virtual table to datatables and let it preform rest of the query (like, limit, skip, order etc.)
        return Datatables::of($drawings)
            ->editColumn('title', function($drawings) {
                return '<a href="'.route('customers.show', $drawings->customer_id).'">' . $drawings->title . '</a>';
            })  
            ->editColumn('number', function($drawings) {
                return '<a href="'.route('drawings.show', $drawings->drawing_id).'">' . $drawings->number . '</a>';
            })  
            ->make(true);
    }

花了很多时间试图弄清楚,希望它能节省一些时间。 http://datatables.yajrabox.com/fluent/joins

于 2015-10-16T05:54:46.490 回答
0

我不太确定你的第一个问题。Datatables 搜索窗口将搜索所有内容。你想让它只针对 1 列吗?

要回答您的第二个问题,您可以编辑列输出。尝试这个

$drawings = Drawing::select(array('drawings.id','drawings.number'));

return Datatables::of(Drawing::with('customer')->select('*'))
        ->editColumn('customer', function($drawings) {
            return '<a href="#">' . $drawings->customer . '</a>';
        })              
        ->make(true);

编辑

为了实现您想要的搜索,您需要执行以下操作:

public function datatable(Request $request)
{
    $drawings = Drawing::select(array('drawings.id','drawings.number'));

    return Datatables::of(Drawing::with('customer')->select('*'))
            ->filter(function ($query) use ($request) {
                if ($request->has('name')) {
                    $query->where('customer.customer_name', 'like', "%{$request->get('name')}%");
                }
            })
            ->editColumn('customer', function($drawings) {
                return '<a href="#">' . $drawings->customer->customer_name . '</a>';
            })              
            ->make(true);
}

然后,在你的 JS

$(function () {
    $('#drawing-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: '{{route('drawings.datatable')}}',
            data: function (d) {
                d.name = $('input[name=name]').val();
            }
        },
        columns: [
            { data: 'number', name: 'number' },
            { data: 'customer.title', name: 'customer' },
        ]
    });
});

这是未经测试的,但应该达到你想要的。

于 2015-10-13T14:16:49.467 回答
0

您也可以使用与 yajra 的 elequent 关系这里是示例代码。

$sub_sectors = Sector::where('parent_id', '>', 0)->with('parent')->latest()->get();
$sub_sectors->each(function($sub_sectors){
    $sub_sectors->sector = $sub_sectors->parent->title['en'];
});

在此示例中,您可以使用每种方法获取扇区与子扇区,您可以获得扇区名称,现在您可以在 yajra 表中显示扇区

于 2020-06-24T05:25:26.267 回答