Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Письменов Дмитрий Иванович
/
yourroomads
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 6ea4d090
authored
Feb 27, 2021
by
Jonathan Reinink
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify pagination
1 parent
13ce8038
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
26 additions
and
88 deletions
app/Http/Controllers/ContactsController.php
app/Http/Controllers/OrganizationsController.php
app/Providers/AppServiceProvider.php
resources/js/Pages/Contacts/Index.vue
resources/js/Pages/Organizations/Index.vue
resources/js/Shared/Pagination.vue
app/Http/Controllers/ContactsController.php
View file @
6ea4d09
...
...
@@ -20,7 +20,7 @@ class ContactsController extends Controller
->
orderByName
()
->
filter
(
Request
::
only
(
'search'
,
'trashed'
))
->
paginate
()
->
t
ransform
(
function
(
$contact
)
{
->
t
hrough
(
function
(
$contact
)
{
return
[
'id'
=>
$contact
->
id
,
'name'
=>
$contact
->
name
,
...
...
app/Http/Controllers/OrganizationsController.php
View file @
6ea4d09
...
...
@@ -18,7 +18,15 @@ class OrganizationsController extends Controller
->
orderBy
(
'name'
)
->
filter
(
Request
::
only
(
'search'
,
'trashed'
))
->
paginate
()
->
only
(
'id'
,
'name'
,
'phone'
,
'city'
,
'deleted_at'
),
->
through
(
function
(
$organization
)
{
return
[
'id'
=>
$organization
->
id
,
'name'
=>
$organization
->
name
,
'phone'
=>
$organization
->
phone
,
'city'
=>
$organization
->
city
,
'deleted_at'
=>
$organization
->
deleted_at
,
];
}),
]);
}
...
...
app/Providers/AppServiceProvider.php
View file @
6ea4d09
...
...
@@ -2,10 +2,6 @@
namespace
App\Providers
;
use
Illuminate\Pagination\LengthAwarePaginator
;
use
Illuminate\Pagination\UrlWindow
;
use
Illuminate\Support\Collection
;
use
Illuminate\Support\Facades\Request
;
use
Illuminate\Support\Facades\Storage
;
use
Illuminate\Support\ServiceProvider
;
use
League\Glide\Server
;
...
...
@@ -19,12 +15,6 @@ class AppServiceProvider extends ServiceProvider
*/
public
function
register
()
{
$this
->
registerGlide
();
$this
->
registerLengthAwarePaginator
();
}
protected
function
registerGlide
()
{
$this
->
app
->
bind
(
Server
::
class
,
function
(
$app
)
{
return
Server
::
create
([
'source'
=>
Storage
::
getDriver
(),
...
...
@@ -35,75 +25,13 @@ class AppServiceProvider extends ServiceProvider
});
}
protected
function
registerLengthAwarePaginator
()
/**
* Bootstrap any application services.
*
* @return void
*/
public
function
boot
()
{
$this
->
app
->
bind
(
LengthAwarePaginator
::
class
,
function
(
$app
,
$values
)
{
return
new
class
(...
array_values
(
$values
))
extends
LengthAwarePaginator
{
public
function
only
(
...
$attributes
)
{
return
$this
->
transform
(
function
(
$item
)
use
(
$attributes
)
{
return
$item
->
only
(
$attributes
);
});
}
public
function
transform
(
$callback
)
{
$this
->
items
->
transform
(
$callback
);
return
$this
;
}
public
function
toArray
()
{
return
[
'data'
=>
$this
->
items
->
toArray
(),
'links'
=>
$this
->
links
(),
];
}
public
function
links
(
$view
=
null
,
$data
=
[])
{
$this
->
appends
(
Request
::
all
());
$window
=
UrlWindow
::
make
(
$this
);
$elements
=
array_filter
([
$window
[
'first'
],
is_array
(
$window
[
'slider'
])
?
'...'
:
null
,
$window
[
'slider'
],
is_array
(
$window
[
'last'
])
?
'...'
:
null
,
$window
[
'last'
],
]);
return
Collection
::
make
(
$elements
)
->
flatMap
(
function
(
$item
)
{
if
(
is_array
(
$item
))
{
return
Collection
::
make
(
$item
)
->
map
(
function
(
$url
,
$page
)
{
return
[
'url'
=>
$url
,
'label'
=>
$page
,
'active'
=>
$this
->
currentPage
()
===
$page
,
];
});
}
else
{
return
[
[
'url'
=>
null
,
'label'
=>
'...'
,
'active'
=>
false
,
],
];
}
})
->
prepend
([
'url'
=>
$this
->
previousPageUrl
(),
'label'
=>
'Previous'
,
'active'
=>
false
,
])
->
push
([
'url'
=>
$this
->
nextPageUrl
(),
'label'
=>
'Next'
,
'active'
=>
false
,
]);
}
};
});
//
}
}
resources/js/Pages/Contacts/Index.vue
View file @
6ea4d09
...
...
@@ -58,7 +58,7 @@
</tr>
</table>
</div>
<pagination
:links=
"contacts.links"
/>
<pagination
class=
"mt-6"
:links=
"contacts.links"
/>
</div>
</
template
>
...
...
resources/js/Pages/Organizations/Index.vue
View file @
6ea4d09
...
...
@@ -50,7 +50,7 @@
</tr>
</table>
</div>
<pagination
:links=
"organizations.links"
/>
<pagination
class=
"mt-6"
:links=
"organizations.links"
/>
</div>
</
template
>
...
...
resources/js/Shared/Pagination.vue
View file @
6ea4d09
<
template
>
<div
class=
"mt-6 -mb-1 flex flex-wrap"
>
<template
v-for=
"(link, key) in links"
>
<div
v-if=
"link.url === null"
:key=
"key"
class=
"mr-1 mb-1 px-4 py-3 text-sm border rounded text-gray-400"
:class=
"
{ 'ml-auto': link.label === 'Next' }">
{{
link
.
label
}}
</div>
<inertia-link
v-else
:key=
"key"
class=
"mr-1 mb-1 px-4 py-3 text-sm border rounded hover:bg-white focus:border-indigo-500 focus:text-indigo-500"
:class=
"
{ 'bg-white': link.active, 'ml-auto': link.label === 'Next' }" :href="link.url">
{{
link
.
label
}}
</inertia-link>
</
template
>
<div
v-if=
"links.length > 3"
>
<div
class=
"flex flex-wrap -mb-1"
>
<template
v-for=
"(link, key) in links"
>
<div
v-if=
"link.url === null"
:key=
"key"
class=
"mr-1 mb-1 px-4 py-3 text-sm border rounded text-gray-400"
v-html=
"link.label"
/>
<inertia-link
v-else
:key=
"key"
class=
"mr-1 mb-1 px-4 py-3 text-sm border rounded hover:bg-white focus:border-indigo-500 focus:text-indigo-500"
:class=
"
{ 'bg-white': link.active }" :href="link.url" v-html="link.label" />
</
template
>
</div>
</div>
</template>
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment