Error when searching one of the columns in DataTable (serverSide: true)
Error when searching one of the columns in DataTable (serverSide: true)
Link to test case:
Local project
Debugger code (debug.datatables.net):
var n = document.createElement('script');
n.setAttribute('language', 'JavaScript');
n.setAttribute('src', 'https://debug.datatables.net/debug.js');
document.body.appendChild(n);
Error messages shown:
DataTables warning: table id=datauser - Exception Message:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.nama' in 'where clause' (SQL: select count(*) as aggregate from (select '1' asrow_count
fromusers
left jointb_operator
ontb_operator
.user_id
=users
.id
left jointb_mahasiswa
ontb_mahasiswa
.user_id
=users
.id
where (LOWER(users
.username
) LIKE %k% or LOWER(users
.nama
) LIKE %k% or LOWER(users
.role
) LIKE %k% or LOWER(users
.updated_at
) LIKE %k%) andusers
.deleted_at
is null) count_row_table)
Description of problem:
I got the field 'nama' by IFNULL two table columns into one (AS nama), but weirdly DataTable search searching nama in the table users... the ordering and paginating working fine though. How do I fix DataTable search so it can look up 'nama' instead of 'users.nama'?
Controller:
$table = User::select(\DB::RAW('users.id, users.username, IFNULL(tb_operator.nama_operator,tb_mahasiswa.nama_mahasiswa) AS nama, users.role, users.updated_at'))
->join('tb_operator', 'tb_operator.user_id', '=', 'users.id', 'left')
->join('tb_mahasiswa', 'tb_mahasiswa.user_id', '=', 'users.id', 'left');
DataTable:
var datauser = $('#datauser').DataTable({
processing: true,
serverSide: true,
ajax: '{{ action('UserC@data') }}',
columns: [
{ data: 'DT_RowIndex', orderable: false, searchable: false },
{ data: 'username', name: 'users.username' },
{
data: 'nama',
render: function (data) {
return data;
}
},
{
data: 'role',
name: 'users.role',
render: function (data) {
badge = 'danger';
if (data == 'mahasiswa') {
badge = 'info';
} else if(data == 'publik') {
badge = 'warning';
} else if(data == 'operator') {
badge = 'success';
}
return '<span class="badge badge-' + badge + '">' + data + '</span>';
}
},
{ data: 'last_update', name: 'users.updated_at'},
{
data: 'action',
name: 'action',
searchable: false,
orderable: false,
render: function (action) {
return '<div class="text-center">' + action + '</div>';
}
}
]
});
Answers
Hi,
I don't immediately recognise your server-side script - is that something you have written? Can you modify the SQL you are creating to match what DataTables is submitting?
Also you are using
columns.name
for all columns apart from thenama
column. Is there a reason for that? Is your server-side script using thename
property at all? If not, you could use that for the mapping.Allan
Yes, is that not a server-side script? we always use those controllers and they work.
Here is the whole function:
I can't unfortunately
Yes I use name on columns I want to search and order, it's already implemented by previous developer and it works fine
Yes I think, but the new field 'nama' might not be implemented there. I don't know what directory my project server-side script is located? Is the file truly located in the server computer or it is in my computer?
Welp I don't know what mapping looks like
It would be great if the SQL query I had in the controller can be implemented to columns.name too like what I do to columns.data, is there a way to do this?
We use the terminology "server-side script" to mean any code running on the server. I was asking who wrote the library code for it, since it doesn't appear to be using a library we provide, and therefore we can't really debug it.
Worth noting that we also use the term "server-side processing" which means that the server is doing all the filtering, paging and sorting. That appears to be what you are using here (enabled by the
serverSide
option). Do you have tens of thousands or more rows? If not, then you shouldn't need server-side processing.I have no idea . I don't know your project code, so that would be something you would need to lookup in your code base.
I would suggest debugging your script by getting it to output the SQL that it is generating. From there you will hopefully be able to see how it is generating that SQL and how you can modify that generation to suit your needs.
Allan