laravel-filemanager,按时间默认排序

时间:2017-10-29 18:31:48

标签: laravel-5.5 file-management

当调用laravel-filemanager的iframe时,我需要按“time DESC”加载文件顺序。 有可能吗?我阅读了代码并看到我们无法按时间顺序DESC和代码没有选项来配置默认的“sort_type”

https://github.com/UniSharp/laravel-filemanager

3 个答案:

答案 0 :(得分:1)

这不是一个好主意,但对我来说是有用的

我更改了vendor / unisharp / laravel-filemanager / public / js / script.js中的代码

var sort_type = 'alphabetic';

var sort_type = 'time';

如果要按降序对日期进行排序。更改代码

vendor / unisharp / laravel-filemanager / src / Controllers / 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'),
    ];
}

使用

use Illuminate\Http\Request;

public function getItems(Request $request)
{

    $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'),
    ];
}

答案 1 :(得分:0)

我更改了vendor / unisharp / laravel-filemanager / src / traits / LfmHelpers.php中的代码 并且它的工作

   public function sortFilesAndDirectories($arr_items, $sort_type)
    {
        if ($sort_type == 'time') {
            $key_to_sort = 'updated';
        } elseif ($sort_type == 'alphabetic') {
            $key_to_sort = 'name';
        } else {
            $key_to_sort = 'updated';
        }


           return strcmp($a->{$key_to_sort}, $b->{$key_to_sort});
        });

        return $arr_items;
    }

public function sortFilesAndDirectories($arr_items, $sort_type)
    {
        if ($sort_type == 'time') {
            $key_to_sort = 'updated';
        } elseif ($sort_type == 'alphabetic') {
            $key_to_sort = 'name';
        } else {
            $key_to_sort = 'updated';
        }

        uasort($arr_items, function ($a, $b) use ($key_to_sort) {
            if ( $a->$key_to_sort == $a->$key_to_sort )
                return 0;
            else if ( $a->$key_to_sort > $a->$key_to_sort)
                return -1;
            else
                return 1;
        });

        return $arr_items;
    }

答案 2 :(得分:0)

LFM 1.8:

此外,如果您不想更改LFM Src代码,也可以使用此方法。 首先使用此命令生成视图:

php artisan vendor:publish --tag=lfm_view

找到这个文件:

ROOT/resources/views/vendor/laravel-filemanager/grid-view.blade.php

并根据以下更改鳕鱼:

  @if((sizeof($files) > 0) || (sizeof($directories) > 0))

  <div class="row">


<!-- -----------------------------------Begin of added block -->

<?php
    $file_temp = [];
    if($files != null){
        foreach ($files as $key => $value) {
            $file_temp[$value['updated']] = $value;
        }
        krsort($file_temp);
        $file_temp1 = [];
        $i = 0;
        foreach ($file_temp as $key => $value) {
            $file_temp1[$i] = $value;
            $i+=1;
        }
        $files = $file_temp1;
    }
?>
<!-- ---------------------------------------End of added block -->


  @foreach($items as $item)
  ....

...

如您所见,已添加<?php ?>代码块。您可以根据需要使用krsort()ksort() 降序升序