Giter Site home page Giter Site logo

mgwidmann / scrivener_html Goto Github PK

View Code? Open in Web Editor NEW
123.0 9.0 204.0 188 KB

HTML view helpers for Scrivener

License: MIT License

Elixir 100.00%
scrivener pagination-links html custom-html elixir elixir-phoenix bootstrap foundation semantic materialize

scrivener_html's Introduction

Scrivener.Html Build Status

Helpers built to work with Scrivener's page struct to easily build HTML output for various CSS frameworks.

Setup

Add to mix.exs

  # add :scrivener_html to deps
  defp deps do
    [
      # ...
      {:scrivener_html, "~> 1.8"}
      # ...
    ]
  end

  # add :scrivener_html to applications list
  defp application do
    [
      # ...
      applications: [ ..., :scrivener_html, ... ]
      # ...
    ]
  end

For use with Phoenix.HTML, configure the :routes_helper module in config/config.exs like the following:

config :scrivener_html,
  routes_helper: MyApp.Router.Helpers,
  # If you use a single view style everywhere, you can configure it here. See View Styles below for more info.
  view_style: :bootstrap

Import to your view.

defmodule MyApp.UserView do
  use MyApp.Web, :view
  import Scrivener.HTML
end

Example Usage

Use in your template.

<%= for user <- @page do %>
   ...
<% end %>

<%= pagination_links @page %>

Where @page is a %Scrivener.Page{} struct returned from Repo.paginate/2. So the function in your controller is like:

#  params = %{"page" => _page}
def index(conn, params) do
  page = MyApp.User
          # Other query conditions can be done here
          |> MyApp.Repo.paginate(params)
  render conn, "index.html", page: page
end

Scopes and URL Parameters

If your resource has any url parameters to be supplied, you should provide them as the 3rd parameter. For example, given a scope like:

scope "/:locale", App do
  pipe_through [:browser]

  get "/page", PageController, :index, as: :pages
  get "/pages/:id", PageController, :show, as: :page
end

You would need to pass in the :locale parameter and :path option like so:

(this would generate links like "/en/page?page=1")

<%= pagination_links @conn, @page, ["en"], path: &pages_path/4 %>

With a nested resource, simply add it to the list:

(this would generate links like "/en/pages/1?page=1")

<%= pagination_links @conn, @page, ["en", @page_id], path: &page_path/4, action: :show %>

Query String Parameters

Any additional query string parameters can be passed in as well.

<%= pagination_links @conn, @page, ["en"], some_parameter: "data" %>
# Or if there are no URL parameters
<%= pagination_links @conn, @page, some_parameter: "data" %>

Custom Actions

If you need to hit a different action other than :index, simply pass the action name to use in the url helper.

<%= pagination_links @conn, @page, action: :show %>

Customizing Output

Below are the defaults which are used without passing in any options.

<%= pagination_links @conn, @page, [], distance: 5, next: ">>", previous: "<<", first: true, last: true, view_style: :bootstrap %>
# Which is the same as
<%= pagination_links @conn, @page %>

To prevent HTML escaping (i.e. seeing things like &lt; on the page), simply use Phoenix.HTML.raw/1 for any &amp; strings passed in, like so:

<%= pagination_links @conn, @page, previous: Phoenix.HTML.raw("&leftarrow;"), next: Phoenix.HTML.raw("&rightarrow;") %>

To show icons instead of text, simply render custom html templates, like:

(this example uses materialize icons)

# Using Phoenix.HTML's sigil_E for EEx
<%= pagination_links @conn, @page, previous: ~E(<i class="material-icons">chevron_left</i>), next: ~E(<i class="material-icons">chevron_right</i>) %>
# Or by calling render
<%= pagination_links @conn, @page, previous: render("pagination.html", direction: :prev), next: render("pagination.html", direction: :next)) %>

The same can be done for first/last links as well (v1.7.0 or higher).

(this example uses materialize icons)

<%= pagination_links @conn, @page, first: ~E(<i class="material-icons">chevron_left</i>), last: ~E(<i class="material-icons">chevron_right</i>) %>

View Styles

There are six view styles currently supported:

  • :bootstrap (the default) This styles the pagination links in a manner that is expected by Bootstrap 3.x.
  • :bootstrap_v4 This styles the pagination links in a manner that is expected by Bootstrap 4.x.
  • :foundation This styles the pagination links in a manner that is expected by Foundation for Sites 6.x.
  • :semantic This styles the pagination links in a manner that is expected by Semantic UI 2.x.
  • :materialize This styles the pagination links in a manner that is expected by Materialize css 0.x.
  • :bulma This styles the pagination links in a manner that is expected by Bulma 0.4.x, using the is-centered as a default.

Extending

For custom HTML output, see Scrivener.HTML.raw_pagination_links/2.

See Scrivener.HTML.raw_pagination_links/2 for option descriptions.

Scrivener.HTML can be included in your view and then just used with a simple call to pagination_links/1.

iex> Scrivener.HTML.pagination_links(%Scrivener.Page{total_pages: 10, page_number: 5}) |> Phoenix.HTML.safe_to_string()
"<nav>
  <ul class=\"pagination\">
    <li class=\"\"><a class=\"\" href=\"?page=4\">&lt;&lt;</a></li>
    <li class=\"\"><a class=\"\" href=\"?page=1\">1</a></li>
    <li class=\"\"><a class=\"\" href=\"?page=2\">2</a></li>
    <li class=\"\"><a class=\"\" href=\"?page=3\">3</a></li>
    <li class=\"\"><a class=\"\" href=\"?page=4\">4</a></li>
    <li class=\"active\"><a class=\"\" href=\"?page=5\">5</a></li>
    <li class=\"\"><a class=\"\" href=\"?page=6\">6</a></li>
    <li class=\"\"><a class=\"\" href=\"?page=7\">7</a></li>
    <li class=\"\"><a class=\"\" href=\"?page=8\">8</a></li>
    <li class=\"\"><a class=\"\" href=\"?page=9\">9</a></li>
    <li class=\"\"><a class=\"\" href=\"?page=10\">10</a></li>
    <li class=\"\"><a class=\"\" href=\"?page=6\">&gt;&gt;</a></li>
  </ul>
</nav>"

SEO

SEO attributes like rel are automatically added to pagination links. In addition, a helper for header <link> tags is available (v1.7.0 and higher) to be placed in the <head> tag.

See Scrivener.HTML.SEO documentation for more information.

scrivener_html's People

Contributors

andrewbredow avatar b-murphy avatar britto avatar cc3c avatar drapergeek avatar drewolson avatar ericsullivan avatar hadrianpaulo avatar hl avatar hzeus avatar jaimeiniesta avatar jerodsanto avatar jwarlander avatar kenta-aktsk avatar lee-dohm avatar mamantoha avatar manukall avatar martinmaillard avatar martinos avatar mcrumm avatar mgwidmann avatar mylanconnolly avatar ndbroadbent avatar phereford avatar sebastian avatar sgoettschkes avatar shulhi avatar smeevil avatar taj avatar victorsolis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

scrivener_html's Issues

all links in pagination marked as canonical except for next, prev

The library marks all page links except for next & prev as canonical. Which I think is wrong? It should only set the current page as canonical and make it a disabled link. However, this lib makes the current link a simple li element but tags all other links as canonical.

Currently, it outputs the following links:-
I'm on the page 4. However, it doesn't mark page 4 as canonical. It marks page 1 and 2 as canonical and page 3 as prev.

<ul class="pagination">
<li class="waves-effect"><a class="" href="/categories/health?page=3" rel="prev"><i class="material-icons">chevron_left</i></a></li>
<li class="waves-effect"><a class="" href="/categories/health" rel="canonical">1</a></li>
<li class="waves-effect"><a class="" href="/categories/health?page=2" rel="canonical">2</a></li>
<li class="waves-effect"><a class="" href="/categories/health?page=3" rel="prev">3</a></li>
<li class="active"><a class="">4</a></li></ul>

The library should also provide a method so that we can add next, prev and canonical links in the head using rel links or provide a feature to disable canonical tag marking in pagination links. the next and prev are marked correctly. However, I think marking every other link as canonical in the pagination is wrong. Some SEO expert may explain or share more informed opinion regarding it. This is a nice library and I'm planning to use in my first elixir project. My opinion is based on how Kaminari (a ruby gem) works.

Query string parameters name clashes

Hi!
It's currently not possible to have a query string parameter like distance, as it would most probably cause scrivener_html to raise ArithmeticError or at least have some undesired side-effect.

This is due to the fact that there is no distinction between pagination_links's own keywords and the possible query string parameter we can provide.

The workaround I found was to have a separate data structure with my query parameters, and to use the :path parameter to point not directly to a route helper but to another function, which will take the initial @conn and my data structure and make the call to the route helper.

Would it be possible to accept an anonymous function for example, which would be called with just the pagination parameters?

This would help keep a clean API, and even maybe remove the @conn parameter (as far as I can tell it's only used for the path, but is passed around all the way to raw_pagination_links...)

If this is OK for you I can try to implement this, but it may induce an API change, hence the discussion.

Style not applied

Hi people !

I'm using version "1.8" of the library and after following the installation process, I got the "pagination_links" without being styled like below:

screenshot 2019-02-22 at 21 57 00

Any idea how to solve this issue?

Generating links for non-REST paths

It would be nice if you could include in the documentation an example for using scrivener_html when you don't do pagination on the index action of a controller.
I a route in my routes file like this

  scope "/", AppName do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
    get "/shops", PageController, :shops
    ...
  end

I wanted to use the pagination on the shops page but I had only the page_path helper that would generate links sending me to the index action instead of the shops actions

<%= pagination_links @conn, @page, path: &page_path/3 .... %>

I could have easy created a new controller for the Shops resources and use it's index action but since I'm lazy I've found an easier solution:

Give the shops resource it's own path name using the as: option:

  scope "/", AppName do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
    get "/shops", PageController, :shops
    ...
  end

This will enable us to use the shops_path helper in our templates:

<%= pagination_links @conn, @page, action: :shops, path: &shops_path/3 .... %>

Note that we also need to pass the action option to the pagination_links helper, since the method will default to the :index action.

And that's it.
I really hope it will be useful to other people. It took me some time to figure out how to get this right but I think is a use case that will reoccur time and time again.

Phoenix 1.3 support

Hello! Is anyone working on updating this to work with Phoenix 1.3? I'm willing to get started on it, but don't want to step on any toes. Thanks!

Scrivener HTML 1.8 Displaying as Unordered List

Using Scriviener HTML with Phoenix 1.5.3 and the pagination component is appearing as an unordered list. Not sure why.

my code:

config.exs:

config :scrivener_html,
  routes_helper: Elijah.Routers.Helpers

view:

defmodule ElijahWeb.UserView do
  use ElijahWeb, :view
  import Scrivener.HTML
end

controller:

  def index(conn, params) do
    page =
      users()
      |> Repo.paginate(params)
      conn |> render("index.html", users: page.entries, page: page)
  end

index.html.eex:

<%= pagination_links @page %>

html output:

<nav><ul class="pagination"><li class="active"><a class="">1</a></li><li class=""><a class="" href="?page=2" rel="next">2</a></li><li class=""><a class="" href="?page=3" rel="canonical">3</a></li><li class=""><a class="" href="?page=4" rel="canonical">4</a></li><li class=""><a class="" href="?page=5" rel="canonical">5</a></li><li class=""><a class="" href="?page=6" rel="canonical">6</a></li><li class=""><span class="">&hellip;</span></li><li class=""><a class="" href="?page=12" rel="canonical">12</a></li><li class=""><a class="" href="?page=2" rel="next">&gt;&gt;</a></li></ul></nav>

Any suggestions?

Milligram view styles?

It seems that phoenix now uses milligram for creating its html views. I guess that a milligram view style would be an excellent addition to this project.

SEO header links not taking non-existing pages into account

Thanks for the library and the whole scrivener libraries!

I have an issue with the header links generated by scrivener_html. If I am reading the code correctly there is currently no checking if the next/prev page does indeed exist?

In my opinion, checking if the next/prev pages exist (by looking the the total_pages in relation to the page_number) and skipping prev/next should do the trick. This would also mean the special cases for the first and last page at https://github.com/mgwidmann/scrivener_html/blob/master/lib/scrivener/html/seo.ex#L42 could be removed as they would be covered.

If you are ok with a PR regarding this I can look into it!

Override total_entries and total_pages

I have a use case where i need to pass the total entries and total pages from a more complex query.

How can I override total_entries and total_pages from Scrivener.Ecto?
This doesn't work (from the Scrivener.Ecto README):

def index(conn, params) do
  page =
    MyApp.Person
    |> where([p], p.age > 30)
    |> order_by(desc: :age)
    |> preload(:friends)
    |> MyApp.Repo.paginate(params)

  render conn, :index,
    people: page.entries,
    page_number: page.page_number,
    page_size: page.page_size,
    total_pages: new_page_total, # NEW TOTAL PAGES, CALCULATED ELSEWHERE
    total_entries: new_entry_total # NEW TOTAL ENTRIES, CALCULATED ELSEWHERE
end

Add links to first/last pages

Hello. I think it would great if we had links to last and first pages, like this:
2016-12-29 02-05-12

By default it may be disabled, and can be enabled, for example with option like: recent_pages: true.

Also, I think about ability to configure links styles by options:

= pagination_links @page, previous_class: "some css class", next_class: "..."

and the same for last/first links.

What do you think about this? I think with these features we be able to easy pagination customize.

Thank you!

User can set page querystring higher than total pages

Resulting in the helper rendering as many page links as the page number.
For instance, here I tried entering the page number 100 even though there is only 11 pages in total:

screen shot 2017-02-22 at 00 16 39

At first sight this might just seem like a minor inconvenience for the user, but you could also enter a really high number like say 9999999999 which would make the backend try to render that many page links, causing the elixir process to hang. Do this enough times and you have an easy way to DDOS the site.

Elixir 1.4

Looks like Keyword.merge raises in Elixir 1.4 if both args aren't keyword lists: elixir-lang/elixir#5395

params_with_page = Keyword.merge(url_params, [{page_param, page_number}]) breaks in html.ex with expected a keyword list as the second argument, got: [{"p", 1}]

Can't use html entities in :next and :previous texts

Screenshot generated with this code :

Scrivener.HTML.pagination_links(var!(assigns)[:conn], var!(assigns)[:page], next: "&rightarrow;", previous: "&leftarrow;", view_style: :semantic, path: fn(conn, action, params) -> admin_resource_path(conn, action, var!(assigns)[:conn].params["resource"], params) end)

screenshot from 2016-09-15 19-15-15

Using {:scrivener_html, "~> 1.3"}

Altering the order of first/previous and last/next

If I wanted to do something like this

<%= pagination_links @conn, @page, distance: 2, first: "<<", previous: "<", next: ">", last: ">>", ellipsis: false %>

it would generate

< << 5 6 7 8 9 >> >

but I would want to generate

<< < 5 6 7 8 9 > >>

Another enhancement that I don't think is currently possible is always showing the previous/next buttons but mark them as disabled if on the first/last page respectively.

Phoenix 1.5.0 Compatibility

Phoenix 1.5.0 has been released in April. Would it be possible to update scrivener to provide compatibility with the new version ?

Support Scrivener 2?

Howdy Matt,

Thanks for creating this, I've been using it with much success!

I'm curious what your thoughts are on supporting Scrivener 2. I'm upgrading my Phoenix app to all the latest and scrivener_html's dependency on scrivener ~> 1.2, which depends on ecto ~> 1.1 has become a show stopper for me.

Happy to help however I can!

protocol Scrivener.Paginater not implemented for #Ecto.Query

Hello, I had pagination working fine in my app using:

  • phoenix 1.1.4
  • phoenix_ecto 2.0
  • scrivener_html 1.1
  • scrivener 1.1.4

But now, after upgrading to:

  • phoenix 1.2.0
  • phoenix_ecto 3.0
  • scrivener_html 1.3
  • scrivener 2.0.0

It's failing with:

[error] #PID<0.4760.0> running MyApp.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /s
** (exit) an exception was raised:
    ** (Protocol.UndefinedError) protocol Scrivener.Paginater not implemented for #Ecto.Query<from s in MyApp.Sitemap, where: s.user_id == ^1, order_by: [desc: s.inserted_at]>
        lib/scrivener/paginater.ex:1: Scrivener.Paginater.impl_for!/1
        lib/scrivener/paginater.ex:10: Scrivener.Paginater.paginate/2
        (MyApp) web/controllers/sitemap_controller.ex:28: MyApp.SitemapController.index/2
        (MyApp) web/controllers/sitemap_controller.ex:1: MyApp.SitemapController.action/2
        (MyApp) web/controllers/sitemap_controller.ex:1: MyApp.SitemapController.phoenix_controller_pipeline/2
        (MyApp) lib/MyApp/endpoint.ex:1: MyApp.Endpoint.instrument/4
        (MyApp) lib/phoenix/router.ex:261: MyApp.Router.dispatch/2
        (MyApp) web/router.ex:1: MyApp.Router.do_call/2
        (MyApp) lib/MyApp/endpoint.ex:1: MyApp.Endpoint.phoenix_pipeline/1
        (MyApp) lib/plug/debugger.ex:93: MyApp.Endpoint."call (overridable 3)"/2
        (MyApp) lib/MyApp/endpoint.ex:1: MyApp.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

I've already cleaned up and reinstalled the deps, not sure what is failing here. This is the code in the controller:

  def index(conn, params) do
    sitemaps = Sitemap
               |> where(user_id: ^(current_user(conn).id))
               |> order_by(desc: :inserted_at)
               |> Repo.paginate(params)

    render(conn, "index.html", sitemaps: sitemaps)
  end

Any idea?
Thanks!

Add helper to display item count information

Similar to what will_paginate has:
00000370

I always found this type of information handy and have included it in my Rails projects. I didn't see this option available for this plugin (sorry, not sure if that is the right term) and added a simple version of the functionality myself.

Would you accept a PR for this?

issue with paginate url params

pagination issue

I run into issue that by default the generated pagination do not included any params I used on the search, so basically click on next link will return wrong results.
I know that we have options to pass additional params into the pagination_links but that would be much work if we have to manually do that.

Did I miss something on the docs? or do you intentionally do that? I'm not sure this is an issue or not?

I have done a little work on my project to make it automatically pass all the params into the url, if you need it I can make an PR for this

protocol Phoenix.Param not implemented for [locale: "en", page: 1]

I changed the router to add the locale, and it looks like this:

scope "/:locale", App do
    pipe_through [:browser, :browser_session]

    get "/page", PageController, :index

    resources "/products", ProductController
end`

when I inject the "locale: :en" in the pagination_links:

<%= pagination_links @conn, @page, distance: 5, next: ">>", previous: "<<", first: true, last: true, locale: "en" %>

I got this error:

protocol Phoenix.Param not implemented for [locale: "en", page: 1]

bootstrap 4 support?

Hi,

any plans for Bootstrap 4 support. Design for pagination links is broken.

Best,
Tomaz

No path in links

Hey,

My pagination menu is generated correctly, excepted there is no path in generated links : there is only the query string part (something like href="/?page=XXX"). So the links consistently lead me to the root path.

I'm displaying a paginated list of POIs on a shop edit form ("/shop/:id/edit"). I understand that scrivener doesn't manage to guess the correct path.

Add phoenix_html 2.3 support

The upgrade to new phoenix (1.1) fails with error:

Looking up alternatives for conflicting requirements on phoenix_html
Activated version: 2.3.0
From mix.exs: ~> 2.3
From scrivener_html v1.0.0: ~> 2.2.0

** (Mix) Hex dependency resolution failed, relax the version requirements or unlock dependencies

an exception was raised: (FunctionClauseError) no function clause matching in :lists.reverse/1

Hi, I added {:scrivener_html, "~> 1.0"} to mix.exs and then run phoenix.server, it shows these errors:

Server: localhost:4000 (http)
Request: GET /messages
 ** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in :lists.reverse/1

diff:

diff --git a/mix.exs b/mix.exs
index a6e360c..5cb25da 100644
--- a/mix.exs
+++ b/mix.exs
      {:postgrex, "~> 0.11"},
-     {:scrivener, "~> 1.0"}]
+     {:scrivener, "~> 1.0"},
+     {:scrivener_html, "~> 1.0"}]
   end

diff --git a/mix.lock b/mix.lock
index f8e42f2..3aa7fa9 100644
--- a/mix.lock
+++ b/mix.lock
+  "scrivener_html": {:hex, :scrivener_html, "1.1.0"......

Customization of wrapper classes

Hi there,
First of all thank you for your work, I will use this library in my next pet project.
However, I noticed that there is no easy way to change wrapper classes for pagination_links. For example, the templates for Bulma CSS are centered "by default", which means the classes are hard-coded in private functions. I ended up rewriting the functions like this:

  def pagination_links(paginator, conn) do
    %{page_number: current_page} = paginator
    path_fn = fn number -> Routes.post_path(conn, :index, %{"page" => number}) end

    content_tag :nav, class: "pagination" do
      content_tag :ul, class: "pagination-list" do
        Scrivener.HTML.raw_pagination_links(paginator)
        |> Enum.map(fn content -> page(content, current_page, path_fn) end)
      end
    end
  end

  defp page({number, number}, number, path_fn),
    do: link(Integer.to_string(number), to: path_fn.(number), class: "pagination-link is-current")

  defp page({number, number}, _, path_fn),
    do: link(Integer.to_string(number), to: path_fn.(number), class: "pagination-link")

  defp page({text, number}, _, path_fn) when is_bitstring(text),
    do: link(text, to: path_fn.(number), class: "pagination-link")

  defp page({:ellipsis, safe}, _, _) do
    content_tag :li do
      content_tag(:span, safe, class: "pagination-ellipsis")
    end
  end

  defp page(anything, _, _), do: inspect(anything)

Do you think it would be possible to add options to change wrapper classes for each separate element? Or maybe add a simple API that one could use to define own renderer modules, a bit like HTTPoison.Base?
Technically, I'm still a newbie, but I'm open to implement and open a PR if you point me in the right direction.
Regards,
KM

Foundation uses CSS for ellipsis

Hello!

When I have a lot of pages (1k for example), pagination looks like this:
2016-12-28 17-45-38

Passes (i.e. ...) displays 2 times. Are it bug or feature?

If it can help, I do:

= pagination_links @subscribers, view_style: :foundation, query: @query

Link to first page without page paramter

For SEO purposes, it would be good to link to the first page without the page parameter. I don't see any way to do this right now. Would this be something which could be integrated as a default/only option or via some kind of flag.

If not I feel like I need to fork it and change it in the fork or would there be some way to let scrivener calculate the pagination data, change it and pass it back to scrivener to create the needed html?

Compilation error in v1.7

Getting this error when building with v1.7:

Compiling 28 files (.ex)

== Compilation error on file web/views/page_view.ex ==
** (CompileError) web/views/page_view.ex:3: module Scrivener.SEO is not loaded and could not be found

It looks like the using introduced in #59 is referring to Scrivener.SEO but the module is actually named Scrivener.HTML.SEO?

Most config options don't work in `config.exs`?

Hi,

If I set the following in my config.exs file, only the view_style takes effect:

config :scrivener_html,
  routes_helper: MyApp.Router.Helpers,
  view_style: :bootstrap_v4,
  distance: 5, next: "Next", previous: "Previous",
  first: true, last: true

I would have expected the other config options to be applied as well.

I wanted to do this because I have pagination on 4 different resources, and I didn't want to have to customize the next, previous, etc. for each one individually but currently that seems to be the only way to do it.

Also on a side note, first / last never show up, even if I set them directly on pagination_links in my eex template.

I'm running 1.7.1 (the latest release at the time of this issue).

protocol Phoenix.Param not implemented for [page: 1]

I have added the following in my template

<%= pagination_links @conn, @posts, path: &profile_path/3, action: :videos %>

My function in the controller looks like below

def videos(conn, params) do
    user_id = params["user_id"]
    user = load_user(user_id)
    posts = Post
            |> where([p], p.user_id == ^user.id)
            |> where([p], p.type == 1)
            |> order_by(desc: :inserted_at)
            |> preload([{:user, :personal_detail}])
            |> Repo.paginate(params)
   render(conn, "videos.html", posts: posts, user: user,)
end

Routes for the videos is http://localhost:4000/profiles/1/videos/?page=1

scope "/", SampleProject do
    pipe_through [:browser, :require_authentication]
    get "/profiles/:user_id/videos", ProfileController, :videos
end

I get this error:

protocol Phoenix.Param not implemented for [page: 1]

Scrivener.Html is not available in production

Hi, I get this error in my production machine, which deployed with edeliver.

I had to add :scrivener_html into my applications list in mix.exs to resolve it.

def application do
  ...
  applications: [..., :scrivener_html, ...]
end

could we add some tips in README?

Adding a pagination_info helper

It's pretty trivial, but I was thinking this library should included something like this:

iex> Scrivener.HTML.pagination_info(%Scrivener.Page{total_pages: 10, page_number: 5, total_entries: 100, page_size: 10}) |> Phoenix.HTML.safe_to_string()
"<div class="pagination_info">
  Showing 51 to 60 of 100 entries
</div>"

It should be as simple as something like:

def pagination_info(paginator, name \\ "entries") do
  text =
    with first = (paginator.page_number - 1) * paginator.page_size + 1,
         last = min(paginator.page_number * paginator.page_size, paginator.total_entries),
         total = paginator.total_entries,
         do: "Showing #{first} to #{last} of #{total} #{name}"

  content_tag(:div, safe(text), class: "pagination_info")
end

Wrong :path for nested resources

Hi,

In my router.ex I have some nested resources, for example:

scope "/admin", MyApp do
  scope "/nested", as: :nested do
    resources "/users", Nested.UserController
  end
end

When I use pagination_links function like that pagination_links @conn, @page, I'm getting an error undefined function MyApp.Router.Helpers.user_path/3, because correct path should be MyApp.Router.Helpers.nested_user_path/3.
To make it work I have to use additional :path argument to pagination_links like that
pagination_links @conn, @page, path: &nested_user_path(&1, &2, &3).
It would be nice to have nested resources :as argument to be included into the path by default, without using that additional :path argument.

Thanks!

no function clause matching in Scrivener.HTML.pagination_links/2

When trying to use the library I get

no function clause matching in Scrivener.HTML.pagination_links/2

my html looks like this

<div class="col-sm-12">
    <h3 class="text-center">Recent Auctions</h3>
    <%= for auction <- @recent_auctions do %>
      <%= render "auction_block.html", auction: auction %>
    <% end %>
    <%= pagination_links @conn, @page %>
  </div>

my controller looks like

query = from a in Auction,
             order_by: [desc: a.time],
             preload: [:state, :city, :facility]
      page = Repo.paginate(query, params)

      assign(conn, :user_city, nil)
      |> assign(:auctions, page.entries)
      |> assign(:page_number, page.page_number)
      |> assign(:page_size, page.page_size)
      |> assign(:total_pages, page.total_pages)
      |> assign(:total_entries, page.total_entries)
      |> assign(:recent_auctions, page.entries)
      |> assign(:page, page.entries)
      |> assign(:recent_auctions_for_user_city, [])

Any ideas what is going on?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.