์ฒซ์งธ, ์ด ๋๋ผ์ด ํจํค์ง์ ๋ํ ํ๋ฅญํ ์ผ์ ๋๋ค.
์์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
๋ฐ๋ชจ ํ์ผ์ ์ ์ฌ์ฉ์ ์ ์ ์ด์ ์ถ๊ฐํ์ต๋๋ค: .../eloquent/add-edit-remove-column, ํด๋น ์ด์ ๊ฒ์/ํํฐ๋งํ๊ณ ์ถ์ง๋ง ๋ชจ๋ ๊ฒ์ ์ ๋ ฅํ ๋ ๋ธ๋ผ์ฐ์ ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
์ฌ์ฉ์ ์ ์ ์ด์์ ๊ฒ์/ํํฐ๋งํ๊ณ ๊ทธ ํ์ custom_column์ ํฌํจํ์ฌ ํ ์ด๋ธ์ ๋ชจ๋ ์ด์ ๋ํด ๊ฒ์ ์ ๋ ฅ ํ ์คํธ๋ฅผ ์ถ๊ฐํ๋ ์๋ฃจ์ ์ด ์์ต๋๊น?
FireBug ์ฝ์ ์ค๋ฅ:
<span class="exception_message">SQLSTATE[42S22]: Column not found: 1054 Champ
'new_column' inconnu dans where clause (SQL: select count(*) as aggregate from (select '
;1' as row_count from `users` where (LOWER(`id`) LIKE %b% or LOWER(`name`) LIKE %b% or LOWER(`email
`) LIKE %b% or LOWER(`created_at`) LIKE %b% or LOWER(`updated_at`) LIKE %b% or LOWER(`new_column`) LIKE
%b%)) count_row_table)</span>
์ปจํธ๋กค๋ฌ์ ์ฝ๋:
public function getAddEditRemoveColumnData()
{
$users = User::select(['id', 'name', 'email', 'password', 'created_at', 'updated_at']);
return Datatables::of($users)
->addColumn('new_column' , 'ABC')
->make(true)
}
์๋ฐ์คํฌ๋ฆฝํธ์ ์ฝ๋:
$('#users-table').DataTable({
processing: true,
serverSide: true,
ajax: '{{ url("eloquent/add-edit-remove-column-data") }}',
columns: [
{data: 'id', name: 'id'},
{data: 'name', name: 'name'},
{data: 'email', name: 'email'},
{data: 'created_at', name: 'created_at'},
{data: 'updated_at', name: 'updated_at'},
{data: 'new_column', custom: 'new_column', orderable: true, searchable: true}
]
});
@marioene , ๋ฐ์ดํฐ๊ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ฐ์ ธ์จ ๊ฒ์ด ์๋๋ฉด addColumn
์์ ๊ฒ์ํ ์ ์์ต๋๋ค. ์ด์ ๋ํ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ์ด select ๋ฌธ์ ์ด์ ์ถ๊ฐํ๋ ๊ฒ์
๋๋ค. addColumn
๋ฅผ ์ฌ์ฉํ ํ์๊ฐ ์์ต๋๋ค.
$users = User::select([
'id', 'name', 'email', 'password', 'created_at', 'updated_at',
DB::raw("'ABC' as new_column")
]);
์ฐธ๊ณ : ์๋ํ๋ ค๋ฉด ์ฌ์ฉ์ ์ ์
filterColumn
๋ฅผ ์์ฑํด์ผ ํ ์๋ ์์ต๋๋ค. ์์ฑ๋ sql์ด ์๋ํ๊ณ ํจํค์ง์์๋ ์๋ํ๋์ง ํ์ธํ์ญ์์ค.
๋นํ์ฑ์ผ๋ก ์ธํ ์ข ๋ฃ ๋ฐ ์์ ์ ์๋ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํด์ผ ํฉ๋๋ค. ๊ฐ์ฌ ํด์!
@yajra 'select'(์ปจํธ๋กค๋ฌ์์)๋ฅผ ์ฌ์ฉํ์ฌ ๊ณ์ฐ๋ ์ด์ ๋จผ์ ์ถ๊ฐํ๋ผ๋ ๊ทํ์ ์ ์์ ๋ฐ๋์ต๋๋ค.
$staffData = Staff::select('id', 'staff_id', \DB::raw('concat(first_name, " ", middle_name, " ", last_name) as full_name'), 'department_name', 'status');
๊ทธ๋ฐ ๋ค์ ์ฌ์ฉ์ ์ ์ ํํฐ๋ฅผ ์ถ๊ฐํ์ต๋๋ค(์ปจํธ๋กค๋ฌ์์)
$dt = Datatables::of($staffData);
return $dt->filterColumn('full_name', function($query, $keyword) {
$query->whereRaw("CONCAT(first_name, ' ', middle_name, ' ', last_name) like ?", ["%{$keyword}%"]);
})->make(true);
ํ์ด์ง๋ฅผ ์ฒ์ ๋ก๋ํ ๋ ์ ์ญ ๊ฒ์ ์ ๋ ฅ ํ ์คํธ ์์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒ์ํ ๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ฌ ๋ฐ์ดํฐ ํ ์ด๋ธ์ด ์ ๋๋ก ๋ ๋๋ง๋ฉ๋๋ค.
Ajax error. http://datatables.net/tn/7
์ ๊ฐ ์ค์๋ฅผ ํ ์ ์๋ ๋ถ๋ถ์ ์๋ ค์ฃผ์ธ์.
@AshishGupta001 ํฌ๋กฌ ๊ฐ๋ฐ ๋๊ตฌ ๋๋ ๋ฐฉํ๋ฒ์ ์ฌ์ฉํ์ฌ ์์ฝ์ค ์์ฒญ์ ๊ฒ์ฌํด ๋ณด์ญ์์ค. ๋ฌธ์ ์ ์์ธ์ ๋ํ ํํธ๋ฅผ ์ ๊ณตํฉ๋๋ค. ์๋ง๋ SQL ์์ธ ๋ฌธ์ ์ผ ๊ฒ์ ๋๋ค.
@yajara ๋น ๋ฅธ ๋ต๋ณ ๊ฐ์ฌํฉ๋๋ค.
'๊ณ์ฐ๋' ์ด(์ ๊ฒฝ์ฐ์๋ 'full_name')์ ๋ํ ์ ์ญ ๊ฒ์์ ๊ตฌํํ๋ ๋์ ๋ค์์ ์ฐธ์กฐํ์ต๋๋ค.
1) 168ํธ : https://github.com/yajra/laravel-datatables/issues/168
2) ์: https://datatables.yajrabox.com/eloquent/post-column-search
๋ด ๋ฐ์ดํฐ ํ ์ด๋ธ์ ๋ํ ์ฌ์ฉ์ ์ง์ 'filterColumn()'์ ์ถ๊ฐํ๊ธฐ ์ ์ ํ ์ด๋ธ์ ๋๋ฝ๋ ์ด 'full_name'์ ๋ํด ๋ถํํ๋ SQL ์์ธ๊ฐ ๋ฐ์ํ์ต๋๋ค. ๊ทธ๋ฐ ๋ค์ ์๋์ ๊ฐ์ด '๊ณ์ฐ๋' ์ด(๋ด ๊ฒฝ์ฐ์๋ 'full_name')์ ๋ํ ์ฌ์ฉ์ ์ง์ ํํฐ๋ฅผ ์ถ๊ฐํ๊ธฐ ์ํด ์๋ ์ฝ๋๋ฅผ ์ถ๊ฐํ์ต๋๋ค.
$dt->filterColumn('full_name', function($query, $keyword) {
$query->whereRaw("CONCAT(staff.first_name, ' ', staff.middle_name, ' ', staff.last_name) like ?", ["%{$keyword}%"]);
});
return $dt->make(true);
์ด์ ๋ก๊ทธ์ ๋ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
mb_strtolower() expects parameter 1 to be string, object given
ํ์ผ ......./vendor/laravel/framework/src/Illuminate/Support/Str.php#162
์ด ๋ฌธ์ ๋ฅผ ๋๋ฒ๊ทธํ๋ ๋ฐ ๋์์ ์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค.
@AshishGupta001 ์ด๋ค ๋ฒ์ ์ ์ฌ์ฉํ๊ณ ์์ต๋๊น? ๊ทํ์ ์ฝ๋๋ ๋์๊ฒ ๊ด์ฐฎ์ ๋ณด์ ๋๋ค.
@yajra ํ์ธ
PHP - 5.5.38-3
๋ผ๋ผ๋ฒจ - 5.0.34
Datatables - yajra / laravel-datatables ์ค๋ผํด (composer.json ๊ฐ์ด) ^ 6.3
Helper ํด๋์ค์ 'getOrMethod'์ ์ ๋ ฅ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ๋ "clouser"๋ฅผ ๋ฎ์ถ๊ธฐ ์ํด ๋ณํ์ ์๋ํ ๋ ์์ธ๊ฐ ๋ฐ์ํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
public static function getOrMethod($method) {
if (! Str::contains(Str::lower($method), 'or')) {
return 'or' . ucfirst($method);
}
return $method;
}
'$method'๋ ์๋์ ๊ฐ์ด function($query, $keyword)
์ ๋ฌ๋ ํด๋ก์ ์
๋๋ค.
Closure {#864
reflection: """
Closure [ <user> public method App\Http\Controllers\{closure} ] {\n
@@ /var/www/html/XYZ/app/Http/Controllers/StaffHistoryController.php 310 - 313\n
\n
- Parameters [2] {\n
Parameter #0 [ <required> $query ]\n
Parameter #1 [ <required> $keyword ]\n
}\n
}\n
"""
}
๋ด๊ฐ ์ง๋ฉดํ ๋ฌธ์ ๋ฅผ ํ์ ํ ์๋ ์์์ง๋ง ์ด ํน์ ์ฌ์ฉ ์ฌ๋ก๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ์ฐพ์ ์ ์์์ต๋๋ค. ์ฌ๊ธฐ์์ ํํธ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
๋ณด๊ธฐ: ์คํฌ๋ฆฝํธ ํ๊ทธ ๋ด์ ๋ณด๊ธฐ์ ๋ค์์ด ์์ด์ผ ํฉ๋๋ค .
{ data: 'full_name', name: 'full_name', searchable: false },
{ data: 'first_name', name: 'first_name', searchable: true, visible: false },
{ data: 'middle_name', name: 'middle_name', searchable: true, visible: false },
{ data: 'last_name', name: 'last_name', searchable: true, visible: false }
์ปจํธ๋กค๋ฌ: ๋๋จธ์ง ์ด ๊ฐ๊ณผ ํจ๊ป SQL ์ฟผ๋ฆฌ์์ first_name ๋ฑ์ ๊ฐ์ ธ์ต๋๋ค.
@AshishGupta001 ์์
@AshishGupta001 ์ข์ ์๋ฃจ์ ์ ๋๋ค!
@yajra ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ฐ์ ธ์จ ๋ฐ์ดํฐ์ด์ง๋ง JS์์ ์ด ์ด์ ์ถ๊ฐํ์ง ์๋ addColumn์ ์ฌ์ฉํ์ฌ ์ถ๊ฐ๋ ์ด์ ๊ฒ์ํ ์ ์์ต๋๊น?
์๋๋ฉด JS์ ์ถ๊ฐํ๊ณ ๋ณด์ด์ง ์์ง๋ง ๊ฒ์ ๊ฐ๋ฅํ๊ฒ ๋ง๋ค์ด์ผ ํฉ๋๊น?
๋ด๊ฐ ์๊ฐํ ์ ์๋ ๋ช ๊ฐ์ง ๋ฐฉ๋ฒ:
{data: 'added_column', name: 'actual_column_name'}
๋น ๋ฅธ ๋ต๋ณ ๊ฐ์ฌํฉ๋๋ค @yajra
์ด๋ฏธ ๋ ๋ฒ์งธ ์๋ฃจ์ ์ ์๋ํ๊ณ ์์์ง๋ง ์ด ์ถ๊ฐ๋ ์ด์ด ์ฟผ๋ฆฌ์์ ๊ณ ๋ ค๋์ง ์๋ ๊ฒ์ผ๋ก ๋ํ๋ฌ์ต๋๋ค.
์ด๊ฒฌ์๋ ์ฌ๋ ?
ํด๋น ์ด์ searchable: false
๋ฅผ ์ถ๊ฐํ์ง ์์๋์ง ํ์ธํ์ญ์์ค.
์ด๊ฒ์ด ๋ด๊ฐ ->addColumn('company_name', function($deal) {
return $deal->company_name;
})
์ด์ ์ถ๊ฐํ ๋ฐฉ๋ฒ์
๋๋ค.
๊ทธ๋ฆฌ๊ณ $dataTable->filterColumn('company_name', 'where', "like", ["%$keyword%"]);
์ ๊ฐ์ ๊ธ๋ก๋ฒ ๊ฒ์์ด
๋ฐฑ์๋์์ searchable: false
๋ฅผ ์ ๋ฌํ ์ ์์ต๋๊น?
searchable: false
๋ ํด๋ผ์ด์ธํธ ์ธก js์ ์์ต๋๋ค.
BTW, ์ด๋ค ๋ฒ์ ์ ์ฌ์ฉํ๊ณ ์์ต๋๊น?
๊ทธ๋. ๋ฐ๋ผ์ ์ฐ๋ฆฌ๋ ์ฌ์ ํ ์ด ๋ฐฉ๋ฒ์ผ๋ก ์ธ๊ธํ ์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
v6.17.0
@AshishGupta001 ๊ณผ ๋์ผํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๋ถํํ๋ ํด๋ผ์ด์ธํธ ์ธก ํธ๋ฆญ์ ์ด์ ์ ๋ ฌํ ์ ์๊ธฐ ๋๋ฌธ์ ์๋ฆฌ์ง ์์ต๋๋ค.
filterColumn ๋์์ ํ์ํ์ง๋ง ์๋์ํค์ง ๋ชปํ์ต๋๋ค.
"yajra/laravel-datatables-oracle" ์ฌ์ฉ: "~9.0"
์ปจํธ๋กค๋ฌ ๋ฐฉ๋ฒ:
public function getAccounts(Request $request)
{
if ($request->ajax()) {
$accounts = Account::with('current_marketer')->select([
'*',
DB::raw("CONCAT(accounts.lastname,' ',accounts.firstname) as fullname"),
]);
return Datatables::of(Account::query()->with('current_marketer'))
->addColumn('marketer', function(Account $account){
return $account->current_marketer->name;
})
->addColumn('fullname', function($account){
return '<a href="'.route('account.clients.show', $account->account_number).'" class="hover:underline" target="_blank">'. $account->fullname.'</a>';
})
->filterColumn('fullname', function($query, $keyword) {
$sql = "CONCAT(accounts.lastname,' ',accounts.firstname) like ?";
$$query->whereRaw($sql, ["%{$keyword}%"]);
})
->rawColumns(['fullname'])
->editColumn('created_on', function(Account $account) {
return Carbon::parse($account->created_on)->format('jS F, Y');
})
->toJson();
}
return view('datatables.accounts.index');
}
๋ธ๋ ์ด๋ js:
$('#accounts-table').DataTable({
"processing": true,
"serverSide": true,
"ajax": '{{ route('accounts.get') }}',
"columns": [
{ data: "id" },
{ data: "account_number" },
{ data: 'fullname'},
{ data: "marketer" },
{ data: "created_on" },
]
});
์์ธ: ๊ฒ์ ๋๋ ์ ๋ ฌ ์๋ ์:
๊ทธ ์์ธ์ ๋ฌธ์ ๊ฐ ๋ด ์๋ชป์์ด ๋ฐํ์ก๊ธฐ ๋๋ฌธ์
๋๋ค. ๋ด filterColumn ๋ฉ์๋์ ์ด์ค '$$' ๊ธฐํธ๊ฐ ์์ต๋๋ค.
๊ทธ๋๋ ์ ๋ ฌ ๋ฌธ์ ๋ ํด๊ฒฐ๋์ง ์์์ต๋๋ค. ์์ง ์์์ผ ํ ์ฌํญ:
@seewhy17 ๋ณต์กํ ์ ๋ ฌ์ ์ํด orderColumn ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ๊ทํ์ ๊ฒฝ์ฐ fullname
์ฃผ๋ฌธํ๋ ๊ฒ์ ์ด๋ฏธ ๊ธฐ๋ณธ์ ์ผ๋ก ์๋ํฉ๋๋ค.
@seewhy17 ๋ณต์กํ ์ ๋ ฌ์ ์ํด orderColumn ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ๊ทํ์ ๊ฒฝ์ฐ
fullname
์ฃผ๋ฌธํ๋ ๊ฒ์ ์ด๋ฏธ ๊ธฐ๋ณธ์ ์ผ๋ก ์๋ํฉ๋๋ค.
๊ทธ๋ ์ง ์์์ต๋๋ค. filterColumn ๋ฉ์๋๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ๋์ผํ ์์
์ ์ํํ๋ ๋ชจ๋ธ์ ํ๊ฐ์์ ์ถฉ๋ํ๋ ๊ฒ ๊ฐ์ต๋๋ค... ์ ๋ง ๋ชจ๋ฅด๊ฒ ์ต๋๋ค.
js์ name: lastname
๋ฅผ ์ถ๊ฐํ์ฌ ์ผ์ข
์ ์์
์ ์ํํ์ผ๋ฏ๋ก ์ฑ์ผ๋ก ํํฐ๋งํ๊ณ filterColumn ๋ฉ์๋๋ ์ค๋ณต๋ฉ๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
@marioene , ๋ฐ์ดํฐ๊ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ฐ์ ธ์จ ๊ฒ์ด ์๋๋ฉด
addColumn
์์ ๊ฒ์ํ ์ ์์ต๋๋ค. ์ด์ ๋ํ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ์ด select ๋ฌธ์ ์ด์ ์ถ๊ฐํ๋ ๊ฒ์ ๋๋ค.addColumn
๋ฅผ ์ฌ์ฉํ ํ์๊ฐ ์์ต๋๋ค.