【问题标题】:Need help in order column by date按日期排序需要帮助
【发布时间】:2016-07-24 22:19:59
【问题描述】:

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

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

{!! 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();

我在呈现表格之前按“最后一次”对数据进行排序,这是最后一次更新的日期列。

“mm/dd/yyyy”和“yyyy/mm/dd”格式下单成功,但“dd/mm/yyyy”下单失败。

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

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

谁能帮我在所有日期格式中正确实现排序。 TIA

【问题讨论】:

  • yyyy/mm/dd 是唯一的格式,就数据库而言

标签: mysql sorting date laravel datatables


【解决方案1】:

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

改变

return UTC::usertimezone($TicketDatarow-&gt;updated_at);

return '&lt;span style="display:none"&gt;'.$TicketDatarow-&gt;updated_at.'&lt;/span&gt;'.UTC::usertimezone($TicketDatarow-&gt;updated_at);

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

【讨论】:

  • 感谢您的帮助,它适用于各种格式。
猜你喜欢
  • 2011-07-22
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多