forked from javve/list.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.html
executable file
·395 lines (345 loc) · 15.3 KB
/
api.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
---
layout: default
title: List API
redirect_from:
- /docs/list-api
- /docs/options
---
<h2>
<a name="parameters" class="anchor" href="#API"><span class="octicon octicon-link"></span></a>
List API
</h2>
<h4>Options</h4>
<ul class="api-index">
<li><a href="#i">i</a></li>
<li><a href="#indexAsync">indexAsync</a></li>
<li><a href="#item">item</a></li>
<li><a href="#listClass">listClass</a></li>
<li><a href="#page">page</a></li>
<li><a href="#pagination">pagination</a></li>
<li><a href="#searchClass">searchClass</a></li>
<li><a href="#searchColumns">searchColumns</a></li>
<li><a href="#searchDelay">searchDelay</a></li>
<li><a href="#sortClass">sortClass</a></li>
<li><a href="#valueNames">valueNames</a></li>
</ul>
<h4>Properties</h4>
<ul class="api-index">
<li><a href="#filtered">filtered</a></li>
<li><a href="#items">items</a></li>
<li><a href="#list">list</a></li>
<li><a href="#listContainer">listContainer</a></li>
<li><a href="#matchingItems">matchingItems</a></li>
<li><a href="#searched">searched</a></li>
<li><a href="#visibleItems">visibleItems</a></li>
</ul>
<h4>Methods</h4>
<ul class="api-index">
<li><a href="#add">add</a></li>
<li><a href="#clear">clear</a></li>
<li><a href="#filter">filter</a></li>
<li><a href="#fuzzysearch">fuzzySearch</a></li>
<li><a href="#get">get</a></li>
<li><a href="#on">on</a></li>
<li><a href="#reindex">reIndex</a></li>
<li><a href="#remove">remove</a></li>
<li><a href="#search">search</a></li>
<li><a href="#size">size</a></li>
<li><a href="#show">show</a></li>
<li><a href="#sort">sort</a></li>
<li><a href="#update">update</a></li>
</ul>
<h3>
<a name="parameters" class="anchor" href="#parameters"><span class="octicon octicon-link"></span></a>
Options
</h3>
<p>Using List.js is pretty much plug and play, but you can change some options if you feel like it.</p>
<pre><code>new List(id/element, options, values);</code></pre>
<ul>
<li>
<strong><a name="id" href="#id" class="anchor"></a>id</strong> or <strong><a name="element" href="#element" class="anchor"></a>element</strong> <em class="docs-parameter-description">*required</em><br>
Id the element in which the list area should be initialized. OR the actual element itself.
</li>
<li>
<p><strong><a name="options" href="#options" class="anchor"></a>options</strong> <em class="docs-parameter-description">Object, default: undefined</em><br>
Some of the option parameters are required at some times</p>
<ul>
<li>
<p><strong><a name="valueNames" href="#valueNames" class="anchor"></a>valueNames</strong> <em class="docs-parameter-description">Array, default: null. *required</em><br>
If the list contains items on initialization, then this array
has to contain the value names (class names) for the different values of
each list item.</p>
<pre><code><ul class="list">
<li>
<span class="name">Jonny</span>
<span class="city">Sundsvall</span>
</li>
</ul>
var valueNames = ['name', 'city'];
</code></pre>
<pre><code><ul class="list">
<li data-id="1">
<a href="http://javve.com" class="link name">Jonny</a>
<p class="born timestamp" data-timestamp="1234">1986</p>
<img class="image" src="javve.jpg" />
</li>
</ul>
var valueNames = [
'name',
'born',
{ data: ['id'] },
{ name: 'timestamp', attr: 'data-timestamp' },
{ name: 'link', attr: 'href' },
{ name: 'image', attr: 'src' }
];</code></pre>
</li>
<li>
<p><strong><a name="item" href="#item" class="anchor"></a>item</strong> <em class="docs-parameter-description">String, default: undefined</em><br>
ID to item template element or a string of HTML. Can also be a function which receives a <code>values</code> object and which must return the complete item's HTML as a string.</p>
<pre><code class="javascript">var options = {
item: "<li><span class='name'></span><span class='city'></span></li>"
}
// or
var options = {
item: 'cool-item-id'
};
// or
var options = {
item: function(values) {
return `<li><span class='name'>${values.name}</span><span class='city'>${values.city}</span></li>`;
}
};
</code></pre>
</li>
<li><p><strong><a name="listClass" href="#listClass" class="anchor"></a>listClass</strong> <em class="docs-parameter-description">String, default: "list"</em><br>
What is the class of the list-container?</p></li>
<li><p><strong><a name="searchClass" href="#searchClass" class="anchor"></a>searchClass</strong> <em class="docs-parameter-description">String, default: "search"</em><br>
What is the class of the search field?</p></li>
<li><p><strong><a name="searchColumns" href="#searchColumns" class="anchor"></a>searchColumns</strong> <em class="docs-parameter-description">Array of strings, default: undefined</em><br>
Restrict searching to just these column names? Default is to search all columns.</p></li>
<li><p><strong><a name="searchDelay" href="#searchDelay" class="anchor"></a>searchDelay</strong> <em class="docs-parameter-description">Int default: 0</em><br>
Delay in milliseconds after last keypress in search field before search starts. 250→750 is good for very large lists.</p></li>
<li><p><strong><a name="sortClass" href="#sortClass" class="anchor"></a>sortClass</strong> <em class="docs-parameter-description">String, default: "sort"</em><br>
What is the class of the sort buttons?</p></li>
<li><p><strong><a name="indexAsync" href="#indexAsync" class="anchor"></a>indexAsync</strong> <em class="docs-parameter-description">Boolean, default: false</em><br>
If there are already items in the list to which the
List.js-script is added, then should the indexing be done
in a asynchronous way? Good for large lists (> 500 items).</p></li>
<li><p><strong><a name="page" href="#page" class="anchor"></a>page</strong> <em class="docs-parameter-description">Int, default: 200</em><br>
Defines how many items that should be visible at the same time. This affects
performance.</p></li>
<li><p><strong><a name="i" href="#i" class="anchor"></a>i</strong> <em class="docs-parameter-description">Int, default: 1</em><br>
Which item should be shown as the first one.</p></li>
<li><p><strong><a name="pagination" href="#pagination" class="anchor"></a>pagination</strong> <em class="docs-parameter-description">Boolean, default: undefined</em><br>
Read more <a href="{{ "/docs/pagination" | relative_url }}">here</a>.</p></li>
</ul>
</li>
<li><p><strong><a name="values" href="#values" class="anchor"></a>values</strong> <em class="docs-parameter-description">Array of objects, default: undefined</em><br>
Values to add to the list on initialization.</p></li>
</ul>
<h3><a name="properties" class="anchor" href="#properties"></a>Properties</h3>
<ul>
<li>
<p>
<strong>listContainer <a name="listContainer" class="anchor" href="#listContainer"></a></strong>
<em class="docs-parameter-description">Element</em><br>
The element node that contains the entire list area.
</p>
</li>
<li>
<p>
<strong>list <a name="list" class="anchor" href="#list"></a></strong>
<em class="docs-parameter-description">Element</em><br>
The element containing all items.
</p>
</li>
<li>
<p>
<strong>items <a name="items" class="anchor" href="#items"></a></strong>
<em class="docs-parameter-description">Array</em><br>
An Array of all Item-objects in the list.
</p>
</li>
<li>
<p>
<strong>visibleItems <a name="visibleItems" class="anchor" href="#visibleItems"></a></strong>
<em class="docs-parameter-description">Array</em><br>
The currently visible items in the list
</p>
</li>
<li>
<p>
<strong>matchingItems <a name="matchingItems" class="anchor" href="#matchingItems"></a></strong>
<em class="docs-parameter-description">Array</em><br>
The items matching the currently active filter and search.
</p>
</li>
<li>
<p>
<strong>searched <a name="searched" class="anchor" href="#searched"></a></strong>
<em class="docs-parameter-description">Boolean</em><br>
Returns true if the list is searched.
</p>
</li>
<li>
<p>
<strong>filtered <a name="filtered" class="anchor" href="#filtered"></a></strong>
<em class="docs-parameter-description">Boolean</em><br>
Returns true if there is an active filter.
</p>
</li>
</ul>
<h3><a name="methods" class="anchor" href="#methods"></a>Methods</h3>
<ul>
<li>
<p><strong><a name="add" class="achor" href="#add"></a>add(values, callback)</strong><br>
Adds one or more items to the list.</p>
<pre><code class="javascript">listObj.add({ name: "Jonny", city: "Stockholm" });
listObj.add([
{ name: "Gustaf", city: "Sundsvall" }
, { name: "Jonas", city: "Berlin" }
]);</code></pre>
<p>If <code>callback</code> is set then items are added to the list in a asynchronous way, and the
callback is called when all items are added. This is especially useful
when adding very many items (200+ or something), or if you just like the
asynchronous coding style.</p>
<pre><code class="javascript">listObj.add(arrayWithManyManyItems, function(items) {
console.log('All ' + items.length + ' were added!');
});</code></pre>
</li>
<li>
<p><strong><a name="remove" class="achor" href="#remove"></a>remove(valueName, value)</strong><br>
Removes items from the list where the value named <code>valueName</code> has value <code>value</code>.
Returns the number of items that where removed.</p>
<pre><code class="javascript">itemsInList = [
{ id: 1, name: "Jonny" }
, { id: 2, name "Gustaf" }
];
listObj.remove("id", 1); // return 1</code></pre>
</li>
<li>
<p><strong><a name="get" class="achor" href="#get"></a>get(valueName, value)</strong><br>
Returns values from the list where the value named <code>valueName</code> has value <code>value</code>.</p>
<pre><code class="javascript">itemsInList = [
{ id: 1, name: "Jonny" }
, { id: 2, name "Gustaf" }
];
listObj.get("id", 2); // return { id: 2, name "Gustaf" }</code></pre>
</li>
<li>
<p><strong><a name="sort" class="achor" href="#sort"></a>sort(valueName, {<br/>
order: 'desc',<br/>
alphabet: undefined,<br/>
insensitive: true,<br/>
sortFunction: undefined<br/>
})</strong><br>
Sorts the list based on values the in the column named <code>valueName</code>.
The <code>alphabet</code> option is used when you have non-english alphabet
where which JavaScript don't know how to sort some characters by default.</p>
<p>The default sort function is found here
<a href="https://github.com/nwoltman/string-natural-compare">https://github.com/nwoltman/string-natural-compare</a>,
if you want to use your own, <a href="https://github.com/javve/list.js/blob/master/src/sort.js">read the code</a> and
<a href="https://github.com/javve/list.js/blob/master/test/test.sort.js">check out the tests</a>.</p>
<pre><code class="javascript">listObj.sort('name', { order: "asc" }); // Sorts the list in abc-order based on names
listObj.sort('name', { order: "desc" }); // Sorts the list in zxy-order based on names
// Sort swedish characters correcly, case-insensitive.
listObj.sort('name', { alphabet: "ABCDEFGHIJKLMNOPQRSTUVXYZÅÄÖabcdefghijklmnopqrstuvxyzåäö" });
// Sort swedish characters correcly, case-sensitive.
listObj.sort('name', { alphabet: "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvXxYyZzÅåÄäÖö" });
// Alphabet could also be on the actual listObj via
listObj.alphabet = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvXxYyZzÅåÄäÖö";
</code></pre>
</li>
<li>
<p><strong><a name="search" class="achor" href="#search"></a>search(searchString, columns, searchFunction)</strong><br>
Searches the list</p>
<pre><code class="javascript">itemsInList = [
{ id: 1, name: "Jonny Stromberg", born: 1986 }
, { id: 2, name "Jonas Arnklint", born: 1985 }
, { id: 3, name "Martina Elm", born: 1986 }
, { id: 4, name "Gustaf Lindqvist", born: 1983 }
, { id: 5, name "Jonny Strandberg", born: 1990 }
];
listObj.search('Jonny'); // Only items with name Jonny are shown (also returns these items)
listObj.search(); // Show all items in list
listObj.search('Jonny', ['name']); // Only search in the 'name' column</code></pre>
<p>Space-separated words match in any order using logical AND. Surround a phrase in quotes for exact matches:</p>
<pre><code class="javascript">listObj.search('Jon 198'); // Items that match Jon AND 198
listObj.search('"Jonny S" 1990'); // Items that match "Jonny S" AND 1990</code></pre>
<p>Optionally your own search function can be used:</p>
<pre><code class="javascript">listObj.search('Jonny', searchFunction); // Custom search for Jonny
listObj.search('Jonny', ['name'], searchFunction); // Custom search in the 'name' column
function searchFunction(searchString, columns) {
for (var k = 0, kl = listObj.items.length; k < kl; k++) {
listObj.items[k].found = false;
// Insert your custom search logic here, set found = true
}
};</code></pre>
</li>
<li>
<p><strong><a name="clear" class="achor" href="#clear"></a>clear()</strong><br>
Removes all items from the list</p>
</li>
<li>
<p><strong><a name="filter" class="achor" href="#filter"></a>filter(filterFunction)</strong></p>
<pre><code class="javascript">itemsInList = [
{ id: 1, name: "Jonny" }
, { id: 2, name "Gustaf" }
, { id: 3, name "Jonas" }
];
listObj.filter(function(item) {
if (item.values().id > 1) {
return true;
} else {
return false;
}
}); // Only items with id > 1 are shown in list
listObj.filter(); // Remove all filters</code></pre>
</li>
<li>
<p><strong><a name="size" class="achor" href="#size"></a>size()</strong><br>
Returns the size of the list.</p>
</li>
<li>
<p><strong><a name="show" class="achor" href="#show"></a>show(i, page)</strong><br>
Shows <code>page</code> number of items from <code>i</code>. Use for paging etc.</p>
<pre><code class="javascript">itemsInList = [
{ id: 1, name: "Jonny" }
, { id: 2, name "Gustaf" }
, { id: 3, name "Jonas" }
, { id: 4, name "Egon" }
, { id: 5, name "Frank" }
, { id: 6, name "Ester" }
];
listObj.show(4, 3); // Display item 4,5,6 </code></pre>
</li>
<li>
<p><strong><a name="update" class="achor" href="#update"></a>update()</strong><br>
Updates the current state of the list. Meaning that if you for instance
hides some items with the <code>itemObj.hide()</code> method then you have to call <code>listObj.update()</code>
if you want the paging to update.</p>
</li>
<li>
<p><strong><a name="reindex" class="achor" href="#reindex"></a>reIndex()</strong><br>
Re-index list from HTML. Good to use if the HTML has been changed by something
else than List.js.</p>
</li>
<li>
<p><strong><a name="fuzzySearch" class="achor" href="#fuzzySearch"></a>fuzzySearch()</strong><br>
Read more <a href="{{ "/docs/fuzzysearch" | relative_url }}">here</a></p>
</li>
<li>
<p><strong><a name="on" class="achor" href="#on"></a>on(event, callback)</strong><br>
Execute <code>callback</code> when list have been updated (triggered by <code>update()</code>, which is used by a lot of methods). Use <code>updated</code> as the event.</p>
<h4>Avaliable events</h4>
<ul>
<li>updated</li>
<li>searchStart</li>
<li>searchComplete</li>
<li>filterStart</li>
<li>filterComplete</li>
<li>sortStart</li>
<li>sortComplete</li>
</ul>
</li>
</ul>