Laravel-datatables: ์„œ๋น„์Šค๋กœ์„œ์˜ Datatables๋Š” ํ‘œ์‹œ, ํŽธ์ง‘ ๋ฐ ์‚ญ์ œ์— ์—ด์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2016๋…„ 03์›” 20์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: yajra/laravel-datatables

๋ฌธ์ œ ๋˜๋Š” ๊ธฐ๋Šฅ ์š”์ฒญ ์š”์•ฝ

Datatables๋ฅผ ์„œ๋น„์Šค๋กœ ์‚ฌ์šฉํ•  ๊ณ„ํš์ด๋ฉฐ ํ‘œ์‹œ, ํŽธ์ง‘ ๋ฐ ์‚ญ์ œ ์—ด์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์„œ(http://datatables.yajrabox.com/eloquent/add-edit-remove-column)์—์„œ ์˜ˆ์ œ๋ฅผ ์ฐพ์•˜์ง€๋งŒ 'Datatables as Service'์— ๋Œ€ํ•œ ์˜ˆ์ œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์‹œ์Šคํ…œ ์„ธ๋ถ€์ •๋ณด

  • ๋ผ๋ผ๋ฒจ ๋ฒ„์ „ : 5.1
  • Laravel-Datatables ๋ฒ„์ „: "yajra/laravel-datatables-oracle": "~6.0"

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

๋‹ค๋ฅธ ๋ฌธ์ œ์—์„œ ์†”๋ฃจ์…˜์„ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค.

public function ajax() {
        $query = $this->query();

        return $this->datatables
                        ->eloquent($this->query())
                        ->addColumn('action', function ($query) {
                            return '<a href="' . route("admin.districts.edit", $query->id) . '" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>';
                        })
                        ->make(true);
    }

๋ชจ๋“  3 ๋Œ“๊ธ€

๋‹ค๋ฅธ ๋ฌธ์ œ์—์„œ ์†”๋ฃจ์…˜์„ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค.

public function ajax() {
        $query = $this->query();

        return $this->datatables
                        ->eloquent($this->query())
                        ->addColumn('action', function ($query) {
                            return '<a href="' . route("admin.districts.edit", $query->id) . '" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>';
                        })
                        ->make(true);
    }

์•ˆ๋…•ํ•˜์„ธ์š” Yajra, ๋‹ค๋ฅธ ์—ด์— ์‚ญ์ œ ๋ฐ ํŽธ์ง‘ ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜์ง€๋งŒ ํ•˜๋‚˜์˜ ์—ด๋งŒ ์‚ฌ์šฉํ•˜๋Š” ์ž‘์—…์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚ด๊ฐ€ ๋ญ˜ ๋ณด์—ฌ?? ๋„์™€์ฃผ์„ธ์š”

๋‹ค๋ฅธ ๋ฌธ์ œ์—์„œ ์†”๋ฃจ์…˜์„ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค.

public function ajax() {
        $query = $this->query();

        return $this->datatables
                        ->eloquent($this->query())
                        ->addColumn('action', function ($query) {
                            return '<a href="' . route("admin.districts.edit", $query->id) . '" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>';
                        })
                        ->make(true);
    }

์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์›์‹œ HTML์„ ์ธ์‡„ํ•˜๋Š” ์‚ฌ๋žŒ์„ ์œ„ํ•ด HTML์ด ์ด์Šค์ผ€์ดํ”„๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด rawColumns('action')๋„ ์ถ”๊ฐ€ํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ:

public function ajax() {
        $query = $this->query();

        return $this->datatables
                        ->eloquent($this->query())
                        ->addColumn('action', function ($query) {
                            return '<a href="' . route("admin.districts.edit", $query->id) . '" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>';
                        })
                        ->rawColumns(['action'])
                        ->make(true);
    }
์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰