hocvietcode.com
  • Trang chủ
  • Học lập trình
    • Lập trình C/C++
    • Lập trình HTML
    • Lập trình Javascript
      • Javascript cơ bản
      • ReactJS framework
      • AngularJS framework
      • Typescript cơ bản
      • Angular
    • Lập trình Mobile
      • Lập Trình Dart Cơ Bản
        • Dart Flutter Framework
    • Cơ sở dữ liệu
      • MySQL – MariaDB
      • Micrsoft SQL Server
      • Extensible Markup Language (XML)
      • JSON
    • Lập trình PHP
      • Lập trình PHP cơ bản
      • Laravel Framework
    • Lập trình Java
      • Java Cơ bản
    • Cấu trúc dữ liệu và giải thuật
    • Lập Trình C# Cơ Bản
    • Machine Learning
  • WORDPRESS
    • WordPress cơ bản
    • WordPress nâng cao
    • Chia sẻ WordPress
  • Kiến thức hệ thống
    • Microsoft Azure
    • Docker
    • Linux
  • Chia sẻ IT
    • Tin học văn phòng
      • Microsoft Word
      • Microsoft Excel
    • Marketing
      • Google Adwords
      • Facebook Ads
      • Kiến thức khác
    • Chia sẻ phần mềm
    • Review công nghệ
    • Công cụ – tiện ích
      • Kiểm tra bàn phím online
      • Kiểm tra webcam online
Đăng nhập
  • Đăng nhập / Đăng ký

Please enter key search to display results.

Home
  • Lập trình laravel
Hướng dẫn xử lý sắp xếp files theo thời gian cho thư viện laravel file manager

Hướng dẫn xử lý sắp xếp files theo thời gian cho thư viện laravel file manager

  • 10-04-2022
  • Toanngo92
  • 0 Comments

Trong dự án gần đây, mình cần tải các tệp theo thứ tự theo “time DESC” ( sắp xếp theo thời gian mới nhất) khi iframe của laravel-filemanager được gọi. (Mặc định, các files trong laravel file manager sắp xếp theo tên alphabet). Vấn đề sẽ xảy ra khi số lượng file quá lớn, người dùng không thể tìm được file mình vừa upload.

Khi đọc docs, không thấy options tùy chỉnh trong tình huống này, nên phải tốn thêm chút thời gian tìm hiểu trên stack overflow để giải quyết vấn đề, nên hôm nay viết lại bài này để anh em đỡ tốn thời gian research, mặc dù theo góc nhìn của mình, cách này là cách giải quyết xấu vì phải sửa trực tiếp code trong thư mục vendor, sẽ gặp vấn đề khi update, nên hãy comment vào dự án để anh em code sau hiểu luồng còn maintain.

Bước 1: truy cập file vendor/unisharp/laravel-filemanager/public/js/script.js

Sửa biến sau để thay đổi tham số request khi gọi lên server lấy files:

var sort_type = 'alphabetic';

Sửa thành:

var sort_type = 'time';

Bước 2: truy cập file vendor/unisharp/laravel-filemanager/src/Controllers/ItemsController.php

Comment hoặc xóa hàm getItems() cũ của file ItemsController.php

/*public function getItems()
{

    $currentPage = self::getCurrentPageFromRequest();

    $perPage = $this->helper->getPaginationPerPage();

    $items = array_merge($this->lfm->folders(), $this->lfm->files());
    return [
        'items' => array_map(function ($item) {
            return $item->fill()->attributes;
        }, array_slice($items, ($currentPage - 1) * $perPage, $perPage)),
        'paginator' => [
            'current_page' => $currentPage,
            'total' => count($items),
            'per_page' => $perPage,
        ],
        'display' => $this->helper->getDisplayMode(),
        'working_dir' => $this->lfm->path('working_dir'),
    ];
}*/

Sửa mã nguồn thành:

use Illuminate\Http\Request; // định nghĩa kiểu dữ liệu request để hứng tham số từ request

// sửa hàm getItems trong Controller
public function getItems(Request $request)
{

    /* dd($request) => nếu dump biến này ra, chúng ta sẽ thấy parameter url được thay đổi từ  alphabetic thành time nếu sửa đúng */
    $currentPage = self::getCurrentPageFromRequest();

    $perPage = $this->helper->getPaginationPerPage();

    $files = $this->lfm->files();
    if($request->sort_type=='time'){
        $files = array_reverse($files);
    }

    $items = array_merge($this->lfm->folders(), $files);
    return [
        'items' => array_map(function ($item) {
            return $item->fill()->attributes;
        }, array_slice($items, ($currentPage - 1) * $perPage, $perPage)),
        'paginator' => [
            'current_page' => $currentPage,
            'total' => count($items),
            'per_page' => $perPage,
        ],
        'display' => $this->helper->getDisplayMode(),
        'working_dir' => $this->lfm->path('working_dir'),
    ];
}

Vậy là ok, chúc bạn thành công!

Tham khảo chi tiết tại link: https://stackoverflow.com/questions/47003766/laravel-filemanager-sort-by-time-default

Bài viết liên quan:

Khái niệm và sử dụng Route trong Laravel
Giới thiệu tổng quan về Laravel Framework
Hướng dẫn cài đặt laravel chi tiết qua video
Xử lý lỗi “The stream or file “laravel.log” could not be opened in append mode: failed to open stream: Permission denied ” trên các môi trường khác nhau
Cài đặt và bật extension fileinfo trong PHP 7 linux (centos,ubuntu)

THÊM BÌNH LUẬN Cancel reply

Dịch vụ thiết kế Wesbite

NỘI DUNG MỚI CẬP NHẬT

2. PHÂN TÍCH VÀ ĐẶC TẢ HỆ THỐNG

1. TỔNG QUAN KIẾN THỨC THỰC HÀNH TRIỂN KHAI DỰ ÁN CÔNG NGHỆ THÔNG TIN

Hướng dẫn tự cài đặt n8n comunity trên CyberPanel, trỏ tên miền

Mẫu prompt tạo mô tả chi tiết bối cảnh

Một số cải tiến trong ASP.NET Core, Razor Page, Model Binding, Gabbage collection

Giới thiệu

hocvietcode.com là website chia sẻ và cập nhật tin tức công nghệ, chia sẻ kiến thức, kỹ năng. Chúng tôi rất cảm ơn và mong muốn nhận được nhiều phản hồi để có thể phục vụ quý bạn đọc tốt hơn !

Liên hệ quảng cáo: [email protected]

Kết nối với HỌC VIẾT CODE

© hocvietcode.com - Tech888 Co .Ltd since 2019

Đăng nhập

Trở thành một phần của cộng đồng của chúng tôi!
Registration complete. Please check your email.
Đăng nhập bằng google
Đăng kýBạn quên mật khẩu?

Create an account

Welcome! Register for an account
The user name or email address is not correct.
Registration confirmation will be emailed to you.
Log in Lost your password?

Reset password

Recover your password
Password reset email has been sent.
The email could not be sent. Possible reason: your host may have disabled the mail function.
A password will be e-mailed to you.
Log in Register
×