浏览代码

add explanation of passing an object to `x-for` (#3928)

`x-for` also accepts objects in addition to arrays. the docs currently only mention arrays, so users might not be sure if objects are allowed. this explicitly states they are and gives an example how to use them.
Andrew Brown 1 年之前
父节点
当前提交
c780cad8c5
共有 1 个文件被更改,包括 24 次插入0 次删除
  1. 24 0
      packages/docs/src/en/directives/for.md

+ 24 - 0
packages/docs/src/en/directives/for.md

@@ -25,6 +25,30 @@ Alpine's `x-for` directive allows you to create DOM elements by iterating throug
 </div>
 <!-- END_VERBATIM -->
 
+You may also pass objects to `x-for`.
+
+```alpine
+<ul x-data="{ car: { make: 'Jeep', model: 'Grand Cherokee', color: 'Black' } }">
+    <template x-for="(value, index) in car">
+        <li>
+            <span x-text="index"></span>: <span x-text="value"></span>
+        </li>
+    </template>
+</ul>
+```
+
+<!-- START_VERBATIM -->
+<div class="demo">
+    <ul x-data="{ car: { make: 'Jeep', model: 'Grand Cherokee', color: 'Black' } }">
+        <template x-for="(value, index) in car">
+            <li>
+                <span x-text="index"></span>: <span x-text="value"></span>
+            </li>
+        </template>
+    </ul>
+</div>
+<!-- END_VERBATIM -->
+
 There are two rules worth noting about `x-for`:
 
 >`x-for` MUST be declared on a `<template>` element