-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Access nested items in x-for #158
Comments
As a short follow up to this and a quick chat with @SimoTod , there might still be some cases where you want to print something from the initial, 'parent' loop item. I'll find a syntax for this and report back. |
This is my IMHO, but i hope it will main way. If you think about alpine like vue replacement, you should correct some expectations. Alpine use to be small, DOM-oriented library, without massive DOM manipulation. In Alpine way you should get not an items array, but a html fragment with rendered items. If you want other - go with Vue. |
I think people would expect to be able to write <div x-data="{items: [{title: 'test',tags: ['ok1','ok2']}, {title: 'test2',tags: ['ok2']}]}">
<template x-for="item in items">
<div>
<span x-text="item.title"></span>
<template x-for="tag in item.tags">
<span x-text="tag"></span>
</template>
</div>
</template>
</div> which doesn't seem unrealistic to me. In my opinion, either Alpine should support it or the documentation should make it clear that it's not a valid use case. @ryangjchandler and I were chatting about that, we don't think it's impossible even with the current structure and it should't add any complexity to the project. |
In response to this, I agree with @SimoTod , no reason this shouldn't be supported. Gonna throw a PR in tomorrow, it's currently midnight in the UK so too late for me. Also going to investigate the syntax I proposed in the twitter thread, as a way of reducing the nested loops and hard to read code. If people don't like it, that's fine. |
I agree that the documentation should describe the main approaches. Now i know that EDIT: |
I agree that backend should do most of the work but, with that in mind, we shouldn't have supported x-for and x-if at all, right? As far as I know, x-if inside x-for works correctly. Looking at the source code, x-for is the only directive where we don't pass the extraVars variable, all the other combinations are supported as expected. We will litteraly pass a variable in 3 points of the code, no added complexity nor massive increment in size, so it seems reasonable to support it. I appreciate we can use x-html but it leads to complicated and error-prone code (my personal view) and I wouldn't suggest it as a best practice (devs with less experience would find that hard). |
|
@ryangjchandler thanks for creating this issue. |
100%. I don't think you should have to load Vue for this single component. I'm going to throw a PR in tonight that should hopefully fix the problem you were having, we'll see how it goes. |
Ah! I started using Alpine today on a project without checking here the issues on github. Immediately stumbled upon the same problem while I was building a form with nested repeater fields. I'd like for this feature to be added too so I can remove Vuejs from my project 👍 |
Adding my voice for nested x-for templates. We're using Alpine to present a single view of our application that other applications can inject, and I'd much rather use this than Vue for something so lightweight. Our use case wouldn't work with the "item.iterable as thing in list" approach, as I'm having to loop through one list of items, then finding all the matching items in a second list. For example:
It's not a big deal if this isn't possible, but it makes sense to me that if statements can be nested, they can use the scope of the current iteration for the nested loops. |
Hi, guys. Obviously, we need this feature to loop through an array of objects twice. So, this happens mostly with people working with APIs. |
There is how to deal with indexes right now (not Vue-way) <h3>Iterate array</h3>
<div x-data="{list:['a', 'b', 'c']}">
<template x-for="[idx, val] in Object.entries(list)">
<div>
<span x-text="idx"></span>
<span x-text="val"></span>
</div>
</template>
</div>
<h3>Iterate object</h3>
<div x-data="{obj: {a: 'A', b: 'B', c: 'C'}}">
<template x-for="[idx, val] in Object.entries(obj)">
<div>
<span x-text="idx"></span>
<span x-text="val"></span>
</div>
</template>
</div> One notice about Also check browser support at caniuse |
I'm interested in this issue. |
@SimoTod has this been fixed now that the new reactivity core has been merged? |
I believe it's still an issue. I mentioned in #230 that the new reactivity component won't fix it, this is the reason why it shows in this conversation. |
Have just seen that PR mention! Sorry I missed it. |
Hi. I am confused. Does alpine support nested loops in current version(2.x.x)? |
@samadadi not yet |
Is there any plan from alpine team to add nested loop implementation in future? |
yeah, that PR needs updating since there have been a few changes to the x-for logic since so it won't work straight away. |
@samadadi check this out and see if it fixes your issue temporarily :) |
Any updates on this? |
Thanks everyone, this issue will be fixed in the next release. Here is the PR: #316 Note: for now, we are not supporting directly nesting <div x-data="{ foos: [{bars: [{bobs: ['one', 'two']}, {bobs: ['three', 'four']}]}, {bars: [{bobs: ['five', 'six']}, {bobs: ['seven', 'eight']}]}] }">
<template x-for="foo in foos">
<div>
<template x-for="bar in foo.bars">
<div>
<template x-for="bob in bar.bobs">
<span x-text="bob"></span>
</template>
</div>
</template>
</div>
</template>
</div> |
Is nested loops supported yet I tried with the lastest version but doesn't seem to work 🤔 |
Yes, this works now. I landed on this report because I had this problem earlier, but the cause is weird: AlpineJS got tripped by void elements e.g. I don't know if this what your problem was though. |
@Uplink03 There is no such thing as a void element in HTML.
Elements are all either self closing, or an open tag. You cannot choose which. If you do
|
@wilari932 Nested for loops have worked since 2021. |
What's the current state of play with supporting directly nesting tags? Very often I need to use data from nested items, but I can't use wrapper tags.
child elements must be direct descendants of the parent element |
Easy, flatten your data.
Or use the display contents wrapper element |
You didn't take into account that you also need to have
but it's not very good, imho |
I'd probably jsut use a |
@Sans84 the inner loop has access to the parent scope without workarounds. What issue are you having exactly? This post was about v2 where you couldn't do it but it's been supported for years. |
This is the problem. Either we get implicit behaviour with the order in which elements are displayed, or we are forced to use wrapper elements, which is not always possible
This creates other implicit issues with CSS |
Which issues does it create? It can impact selectors that use direct child selectors, which is able to be worked around, but it won't impact actual CSS styling. I use |
Ok, I was confused because the original post was about something else and your original example didn't have any problems. The div with `display: contents' is practically invisible to css rules unless you use a direct child selector. You can try to submit a pr if you find a way to fix it, too. |
This issue is to open a discussion about on the problem found here: https://twitter.com/devgummibeer/status/1223415287447461889?s=21
I replied to this tweet with an alternative syntax for this too, see here: https://twitter.com/ryangjchandler/status/1223664594784264194?s=21
I personally think this syntax would be pretty clean and takes a PHP-approach with the syntax too, using the 'as' keyword. Removed the need for nested x-fors, when you only need to do the second loop on a single nested item. In theory, this could be taken a lot further with deeply nested items.
I am playing around with the x-for code currently to find a good way of achieving this syntax, and if it's something people would find useful, I'll throw a PR in for Caleb to ponder over. Have a good one guys! 🤙🏼
The text was updated successfully, but these errors were encountered: