Search code examples
javascriptphpjquerydatatablesweb-frontend

How do i search Boolean value which is displayed in string in jQuery datatable?


I have one field user_role which is boolean in Database. 1 = Manager and 2 = Normal user. I am displaying in a database like if user_role = 1 then it display Manager and so on. but the problem is when I search "Manager", this is not able to search in the user_role field. but when I search for "1", this will display all record in the manager field. here is my code.

$(function () {
     var url = "{{ asset('managers') }}";
     var table = $('#managers-table').DataTable({
     bProcessing: true,
     ordering: true,
     serverSide: true,
     paging: true,
     bRetrieve: true,
     autoWidth: false,
     ajax: {
        url: url
            },
            aaSorting: [[1, 'desc']],
            columns: [
                {data: 'user_id'},
                {data: 'email'},
                {data: 'username'},
                {
                    "data": 'user_role',
                    "render": function (data) {
                        return data == 1 ? "<span>Manager</span>" : "<span>Normal user</span>";
                    }
                },
               ]

        });
    });

and here us my php code.

/**
 * Display a listing of the Manager.
 *
 * @param Request $request
 * @return Response
 */
public function index(Request $request)
{
    $this->managerRepository->pushCriteria(new RequestCriteria($request));
    if ($request->ajax()) {
        return Datatables::of(Manager::IsDelete()->IsTestData()->get())->make(true);
    }
    return view('managers.index');
}

How do I search user_role field? please help if anyone is aware of this.


Solution

  • I got the solution. change the query in PHP code

     public function index(Request $request)
      {
         $this->managerRepository->pushCriteria(new RequestCriteria($request));
         if ($request->ajax()) {
           return Datatables::of(Manager::select(DB::raw("user_id, email, username,is_active,(CASE WHEN (user_role = 1) THEN 'Manager' ELSE 'Normal User' END) as user_role"))->IsDelete()->IsTestData()->get())->make(true);
         }
         return view('managers.index');
      }