Problem/Motivation
Views Style > List contains two options: unordered (i.e bulleted list) and ordered (i.e. numbered list). When the ordered list option is used and the views pager displays a second page, the items in the list are numbered starting from 1, rather than continuing at the next higher number according to the paged results set and number of items per page.
This issue affects the default views-view-list.html.twig template in core, and all core themes including those that do not explicitly override the default template, as well as the core themes that override this template, which currently includes the stable9 theme and the starterkit_theme, and accordingly, any theme that was generated using the starterkit theme.
Steps to reproduce
- Install Drupal.
- Create at least 3 nodes.
- Create a view with a "Page" display to show all nodes.
- Use a Style of HTML List > Ordered list.
- Set a full pager (or a mini pager) and configure the view to display 2 nodes per page.
- Visit the URL of the view page, confirm there are 2 nodes in a numbered list starting at "1".
- Click the next page.
- Expected result the numbered list should start at "3".
Proposed resolution
Update template_preprocess_views_view_list to add a "start" attribute for ordered lists.
On the first page: current_page=0, items_per_page=2 ––> start="1"
On the second page: current_page=1, items_per_page=2 ––> start="3"
On the third page: current_page=2, items_per_page=2 ––> start="5"
Remaining tasks
Patch- Tests
User interface changes
Paged results in views that use numbered lists will increment properly.
API changes
None
Data model changes
None
Original report by @izmeez
Views 2 is fantastic and keeps getting better and better. Thanks.
I have two questions related to the Style>List options.
There are two options, unordered (which is bullets in the Garland theme) and ordered (which is numbered). If this is the essential difference then maybe calling one bullets and the other numbered would be worth considering. Especially since the order is more a function of the sort.
Secondly, when the ordered (numbered) list option is used and pager displays a second page the items are numbered again from 1 ... rather than continuing at the next higher number. Is there somewhere to change this behaviour?
Thanks very much,
Izzy
Issue fork drupal-293295
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 293295-alternative-approach
changes, plain diff MR !10966
- 293295-views-ordered-list
changes, plain diff MR !4415
Comments
Comment #1
merlinofchaos commentedThey're called Ordered and Unordered because that's what they're called in HTML. The browser actually does the numbering, so Views can't be held responsible for that. There's no really easy way to change that, I don't think, though maybe adding a style directive via theming I think CSS can tell the ordered list what number to start on.
Comment #2
grand2li commentedIs there anyone know how to solve this problem by using CSS?
Thanks in advance!
Comment #3
NoRandom commentedActually there is a deprecated property for < ol > to set the starting number: http://www.w3.org/TR/html401/struct/lists.html
But I don't know how to use it with the view output. Maybe you could try editing the output template for lists.
And I don't think this can be done using css since css is static and you would need something dynamic wich changes through the different view pages.
Regards.
Comment #4
diodata commented+1 most definitely for this feature.
[The following was taken from http://stackoverflow.com/questions/1770482/set-values-of-li-tag-in-html]
True, the ul start and li value have been deprecated in HTML4.
However, they are included in HTML5:
http://www.w3.org/TR/html5/semantics.html#the-ol-element
http://www.w3.org/TR/html5/semantics.html#the-li-element
You could do the same using CSS, although I'm not sure of the browser nuances.
Comment #5
diodata commentedDoes anybody know of a work-around for this? Is there some way to override the <ol start="xx"> that is generated for each page? (xx = page number - 1 * number of items per page) Any ideas on where to start?
Comment #6
christian_m commentedCan you not simply use the $id variable in the template?
Something like this:
I have not tried it, though, but I assume the $id variable is not reset between pages.
If that doesn't work, you could probably use the page number from the URL and put it into your formula...
Just my thoughts.
Comment #7
jofdesign commentedSomebody does have another solution ?
Comment #8
dchatryI'll let my little workaround here, in case that could help anyone.
So basically, all you need to do is to create a specific view-view-list template for your view (for example view-view-list--my-view.tpl.php) and paste this code in it :
Comment #9
VKrishn commentedTry:
Adding "class" to all ol would continue the list number,
whereas adding "id" can help in resetting particular ol lists from rest.
See some of classreset examples here:
http://insteps.net/pr/a/pmwiki/Cookbook/Precode-sample4
Notice css entry:
body { counter-reset: linenum alpha beta rc1; }Regards.
Comment #10
Tarun Nagpal commentedThank you so much, it gives a new thing to learn and understand about views....:)
Comment #11
jwilson3Solution for Drupal 8:
Copy views-view-list.html.twig into your theme and replace:
with:
In process of trying to get various things working, i ran into issue #2845400: Adding attributes to views-view-list.html.twig doesn't work if Views List class Style option is empty, and found a workaround using
create_attribute()for when the list.attributes is empty.Here is a complete solution with both example CSS classes, and the
start="N"syntax. Hope you find it useful:Edit: I've modified the above to use
setAttributeinstead ofaddAttributewhich was incorrect.Comment #12
robcarrThanks @jwilson - your template tweak for
views-view-list.html.twigin 11 worked really well.Comment #13
jwilson3Awesome @robcarr!
It was closed years ago by Merlin of Chaos with this reasoning:
However, the approach in #11 proves that this can (and should) be done really easily with an HTML attribute in the template, not in the theme's CSS.
Therefore, I'm thinking we could reopen this and switch it to the core Drupal issue queue's Views sub-system.
Comment #15
lendudeFor the initial question in the IS, isn't this something you would use the 'View result counter' field for? That would not get it right on a HTML level but it would show the correct numbers right?
Comment #16
jwilson3IS:
#15:
Sure, you could use view result counter, but not if you want semantic html list. Effectively, if you were to use the ordered (numbered) list option with the view result counter field, then you end up with two sets of numbers, one from the HTML
<li>itself, and the other from the field value, and as described above, from page two onward the two values would start to diverge. Using view result counter with numbered list would only require further hacking the numbered list functionality to hide the ordinals/bullets in your theme's CSS. This workaround doesn't do any thing for the viability of the numbered list feature out of the box.Comment #18
jwilson3Comment #19
jwilson3Comment #20
smustgrave commentedSeems to have a test failure.
Also could the issue summary be updated please
Comment #21
jwilson3I've updated the IS. The test failure seemed to be unrelated so I re-ran tests, and they passed.
Comment #22
jwilson3Leaving in needs work for two reasons:
Comment #23
pameeela commentedThis feels like a feature request to me, and something that should be optional. It seems strange to have a page with an ordered list that starts from an arbitrary number? I don't think it's safe to assume that everyone using ordered lists would want it to work that way, which is also supported by the lack of activity on this issue in 15 years.
Comment #24
jwilson3I'm having trouble making it all the way to your side to re-classify this as a feature request instead of a bug. (I'd honestly be very interested to see a use case where it makes sense to start ordered, paged output back at 1 on each subsequent page load.) That being said, it is a valid point that this should probably be refactored to be optional, and on by default for new new views, but off by default for existing views, so folks can make a decision whether to opt in on a case-by-case basis.
In my opinion, a better indicator would be to know real-life examples about how many views intentionally use the Ordered List on paged output (and state their use case), versus those that tried it but had to move away from it because the pagination didn't work intuitively in its current state and instead had to implement incremental ids inside the row data, because they didn't know HTML could do this on its own.
Comment #25
pameeela commentedI think I just consider each page distinct, but it sounds like you are considering them states of a single page.
I will concede I haven’t used the ordered list formatter very much in my 12+ years of Drupaling but just because it was not needed. I use the results summary far more often. Just anecdotal but could be it’s not widely used generally and that’s why there isn't much demand for this.
Comment #26
jwilson3Anecdotally, I've had the need for this several times over the years. So much so that I decided to revive this issue and share my work. I use ordered lists on paged search results, where yes each page in the search results is part of a larger context. This fix is also really helpful when using "Views Load More" or "Views Infinite Scroll" module, where new results are loaded into the current page via ajax without a round trip to the server.
Keep in mind, I'm implementing designs from UX teams with limited knowledge of Drupal, and this pattern of incrementing the index of given search results across pages just makes sense intuitively. It is also worth noting that this issue was closed 16 years ago by Earl, the original author of Views, which could also explain why not more people are subscribed to this issue. The fact that there were 10 comments on the issue after it was originally closed and before I reopened it is a decent indicator that there is a need, however small, for this fix.
I rely on the method in #11 (the Twig template override) as the go-to solution on many projects because it is more semantic and properly announced and easily navigable by screen readers, as opposed to having to go with the "views result counter" approach in #15 inside each row and using the "Unformatted" Views display format, which then requires more theming effort to restore basic formatting styles that you get for free with an
<ol>and<li>.Comment #27
pameeela commentedInfinite scroll is certainly a good use case :)
I guess I just haven't come across designs for numbered search. I sure wish I could say I was implementing designs from teams with knowledge of Drupal, do those exist? LOL. Just lucky I guess!
Comment #28
catchAt one point Wim Leers had a number of issues which were moving logic from preprocess to templates, see [#701667] and #3260399: [meta] Theme/render system cleanup, which also fits with the way SDCs work. So having the logic in the template might be fine.
This it does need yes.
Comment #29
quietone commentedThis was a random issue for Bug Smash today. It has been recently updated so just adding tag.
Comment #30
lendudeDidn't test, but what happens on a View with no pager and you try to use
view.pager.current_page? Should it check for a pager before using this? And does the Some pager haveview.pager.current_page?Just looked at existing coverage that might make testing this easier, but alas, there seems to be very little coverage for the list style, just
\Drupal\Tests\views\Kernel\Plugin\StyleHtmlListTestas far as I could quickly find. No tests for 'ol' list at all.Comment #32
smustgrave commentedStill needs tests but tried an alternative approach to add the attribute via code vs twig. Now we don't need a CR for the twig changes.
Comment #33
johnvComment #35
smustgrave commentedGoes it a shot with the tests.
Comment #36
acbramley commentedThis definitely looks a lot cleaner with the logic inside a preprocess function, although back in #28 it was stated that the logic might be better in twig IMO it makes sense here to be in a preprocess function.
Comment #37
nicxvan commentedChange looks straightforward.
Test only fails as expected.
I manually tested following the reproduction steps in the IS and it does not start at the right number after paging on 11.x but does start at the right point on this branch so this works.
For @lendude in 30, I also tested with no pager present and saw no issues so I think that is not an issue.
First glance I thought the class changes were out of scope but it's so the attributes exist even if a custom class isn't set.
RTBC
Comment #38
jwilson3Thank everyone for pushing this forward.
Good point. Also worth pointing out that initializing the Attributes may also have the positive knock-on effect of resolving #2845400-24: Adding attributes to views-view-list.html.twig doesn't work if Views List class Style option is empty 🙌 (which I mentioned way back in comment #11 above). This is worth confirming and double-checking because the patch there is similar to the work done here, but has one additional hunk not present in this issue. Even if it doesn't completely solve that issue, maintainers might consider bringing over issue credit from that issue for the parts that overlap?
Comment #39
nicxvan commentedComment #41
catchComment #42
catchCommitted/pushed to 11.x, thanks!