@alpinejs/docs 3.13.8-revision.2 → 3.13.8-revision.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/en/plugins/sort.md +18 -8
package/package.json
CHANGED
package/src/en/plugins/sort.md
CHANGED
|
@@ -78,26 +78,36 @@ The primary API for using this plugin is the `x-sort` directive. By adding `x-so
|
|
|
78
78
|
You can react to sorting changes by passing a handler function to `x-sort` and adding keys to each item using `x-sort:key`. Here is an example of a simple handler function that shows an alert dialog with the changed item's key and its new position:
|
|
79
79
|
|
|
80
80
|
```alpine
|
|
81
|
-
<
|
|
82
|
-
<
|
|
81
|
+
<ul x-sort="alert($key + ' - ' + $position)">
|
|
82
|
+
<li x-sort:key="1">foo</li>
|
|
83
|
+
<li x-sort:key="2">bar</li>
|
|
84
|
+
<li x-sort:key="3">baz</li>
|
|
85
|
+
</ul>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
<!-- START_VERBATIM -->
|
|
89
|
+
<div x-data>
|
|
90
|
+
<ul x-sort="alert($key + ' - ' + $position)">
|
|
83
91
|
<li x-sort:key="1">foo</li>
|
|
84
92
|
<li x-sort:key="2">bar</li>
|
|
85
93
|
<li x-sort:key="3">baz</li>
|
|
86
94
|
</ul>
|
|
87
95
|
</div>
|
|
88
|
-
|
|
96
|
+
<!-- END_VERBATIM -->
|
|
89
97
|
|
|
90
|
-
|
|
91
|
-
|
|
98
|
+
The `x-sort` handler will be called every time the sort order of the items change. The `$key` magic will contain the key of the sorted element (derived from `x-sort:key`), and `$position` will contain the new position of the item (staring at index `0`).
|
|
99
|
+
|
|
100
|
+
You can also pass a handler function to `x-sort` and that function will receive the `key` and `position` as the first and second parameter:
|
|
101
|
+
|
|
102
|
+
```alpine
|
|
103
|
+
<div x-data="{ handle: (key, position) => { ... } }">
|
|
92
104
|
<ul x-sort="handle">
|
|
93
105
|
<li x-sort:key="1">foo</li>
|
|
94
106
|
<li x-sort:key="2">bar</li>
|
|
95
107
|
<li x-sort:key="3">baz</li>
|
|
96
108
|
</ul>
|
|
97
109
|
</div>
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
As you can see, the `key` and `position` parameters are passed into the handler function on every sorting change. The `key` parameter is the value provided to `x-sort:key`, and the `position` parameter is its new position in the list of children (starting at index `0`).
|
|
110
|
+
```
|
|
101
111
|
|
|
102
112
|
Handler functions are often used to persist the new order of items in the database so that the sorting order of a list is preserved between page refreshes.
|
|
103
113
|
|