按日期排序需要帮助

时间:2016-04-05 06:16:57

标签: mysql sorting date laravel datatables

我正在尝试实现数据表,我希望记录按最后更新的记录排序。

我的刀片文件中的代码如下所示

{!! Datatable::table() ->addColumn( "", Lang::get('lang.subject'), Lang::get('lang.ticket_id'), Lang::get('lang.priority'), Lang::get('lang.from'), Lang::get('lang.last_replier'), Lang::get('lang.assigned_to'), Lang::get('lang.last_activity')) ->setUrl(route('get.inbox.ticket')) ->setOrder(array(7=>'desc')) ->setClass('table table-hover table-bordered table-striped') ->render();!!}

控制器功能

return \Datatable::collection(new Collection($tickets))
                ->addColumn('id', function ($ticket) {
                      return "<input type='checkbox' name='select_all[]' id='".$ticket->id."' onclick='someFunction(this.id)' class='selectval icheckbox_flat-blue' value='".$ticket->id."'></input>";
                })
                ->addColumn('subject', function ($ticket) {
                    $subject = DB::table('ticket_thread')->select('title')->where('ticket_id', '=', $ticket->id)->first();
                    if (isset($subject->title)) {
                        $string = $subject->title;
                        if (strlen($string) > 20) {
                            $stringCut = substr($string, 0, 30);
                            $string = substr($stringCut, 0, strrpos($stringCut, ' ')).' ...';
                        }
                    } else {
                        $string = '(no subject)';
                    }
                    //collabrations
                    $collaborators = DB::table('ticket_collaborator')->where('ticket_id', '=', $ticket->id)->get();
                    $collab = count($collaborators);
                    if ($collab > 0) {
                        $collabString = '&nbsp;<i class="fa fa-users"></i>';
                    } else {
                        $collabString = null;
                    }

                    $threads = Ticket_Thread::where('ticket_id', '=', $ticket->id)->first(); //
                    $count = Ticket_Thread::where('ticket_id', '=', $ticket->id)->count(); //Ticket_Thread::where('ticket_id', '=', $ticket->id)->get();

                    $attachment = Ticket_attachments::where('thread_id', '=', $threads->id)->get();
                    $attachCount = count($attachment);
                    if ($attachCount > 0) {
                        $attachString = '&nbsp;<i class="fa fa-paperclip"></i>';
                    } else {
                        $attachString = '';
                    }

                    return "<a href='".route('ticket.thread', [$ticket->id])."' title='".$subject->title."'>".$string."&nbsp;<span style='color:green'>(".$count.")<i class='fa fa-comment'></i></span></a>".$collabString.$attachString;
                })
                ->addColumn('ticket_number', function ($ticket) {
                    return "<a href='".route('ticket.thread', [$ticket->id])."' title='".$ticket->ticket_number."'>#".$ticket->ticket_number.'</a>';
                })
                ->addColumn('priority', function ($ticket) {
                    $priority = DB::table('ticket_priority')->select('priority_desc', 'priority_color')->where('priority_id', '=', $ticket->priority_id)->first();

                    return '<span class="btn btn-'.$priority->priority_color.' btn-xs">'.$priority->priority_desc.'</span>';
                })
                ->addColumn('from', function ($ticket) {
                    $from = DB::table('users')->select('user_name')->where('id', '=', $ticket->user_id)->first();

                    return "<span style='color:#508983'>".$from->user_name.'</span>';
                })
                ->addColumn('Last Replier', function ($ticket) {
                    $TicketData = Ticket_Thread::where('ticket_id', '=', $ticket->id)->max('id');
                    $TicketDatarow = Ticket_Thread::where('id', '=', $TicketData)->first();
                    $LastResponse = User::where('id', '=', $TicketDatarow->user_id)->first();
                    if ($LastResponse->role == 'user') {
                        $rep = '#F39C12';
                        $username = $LastResponse->user_name;
                    } else {
                        $rep = '#000';
                        $username = $LastResponse->first_name.' '.$LastResponse->last_name;
                        if ($LastResponse->first_name == null || $LastResponse->last_name == null) {
                            $username = $LastResponse->user_name;
                        }
                    }

                    return "<span style='color:".$rep."'>".$username.'</span>';
                })
                ->addColumn('assigned_to', function ($ticket) {
                    if ($ticket->assigned_to == null) {
                        return "<span style='color:red'>Unassigned</span>";
                    } else {
                        $assign = DB::table('users')->where('id', '=', $ticket->assigned_to)->first();

                        return "<span style='color:green'>".$assign->first_name.' '.$assign->last_name.'</span>';
                    }
                })
                ->addColumn('Last', function ($ticket) {
                    $TicketData = Ticket_Thread::where('ticket_id', '=', $ticket->id)->max('id');
                    $TicketDatarow = Ticket_Thread::select('updated_at')->where('id', '=', $TicketData)->first();

                    return UTC::usertimezone($TicketDatarow->updated_at);
                })
                ->searchColumns('subject', 'from', 'assigned_to', 'ticket_number', 'priority')
                ->orderColumns('subject', 'from', 'assigned_to', 'Last Replier', 'ticket_number', 'priority', 'Last')
                ->make();

我在使用'Last'渲染表之前对数据进行排序,这是最后一次更新_日期列。

订购以“mm / dd / yyyy”和“yyyy / mm / dd”格式成功,但在“dd / mm / yyyy”订购失败。

以''mm-dd-yyyy'格式排序时,它可以正常工作 Sorting "mm-dd-yy"

但是在dd-mm-yyyy中排序它排序不正确 sorting "dd-mm-yy"

任何人都可以帮助我在所有日期格式中正确实现排序。 TIA

1 个答案:

答案 0 :(得分:0)

我建议你使用隐藏的跨度。数据表排序通过查看纯文本来进行。

更改

your string

return UTC::usertimezone($TicketDatarow->updated_at);

看起来会一样,但排序会更好。