如何在sortBy获取的集合中使用分页以及运算符在哪里?

时间:2018-03-16 08:10:15

标签: sorting filter pagination laravel-5.5

我需要为我的收藏分页,我必须应用大量过滤器。但我无法做到这一点。我是laravel的新手所以请告诉我正确的方法。 我的控制器:

            //For me it's the best way to look for the value of a spezific column
            int seekValue = 5;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                var columnValue = Convert.ToInt32(row.Cells["ColumnName"].Value);
                if (columnValue == seekValue)
                {
                    dataGridView1.CurrentCell = row.Cells[0];
                }
            }

1 个答案:

答案 0 :(得分:1)

我遇到过这个问题,为了解决这个问题,我创造了一个名为PaginateCollection的特性:

/*
 * Paginate the Laravel Collection before and/or after filtering.
 *
 */
trait PaginateCollection
{
    /**
     * Paginate the collection.
     *
     * @param   \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Collection  $collection
     * @param   integer  $perPage
     * @param   integer  $currentPage
     * @return  \Illuminate\Pagination\LengthAwarePaginator
     */
    public function paginate($collection, $perPage = 10, $currentPage = 1)
    {
        $offSet = ($currentPage * $perPage) - $perPage;

        $otherParams = [
            'path'  => request()->url(),
            'query' => request()->query()
        ];

        return new LengthAwarePaginator(
            $collection->forPage(Paginator::resolveCurrentPage() , $perPage),
            $collection->count(),
            $perPage,
            Paginator::resolveCurrentPage(),
            $otherParams
        );
    }
}

也许,这应该会帮助你。