| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Do you want a 7 KB cross-browser native JavaScript that makes your plain HTML lists super flexible, searchable, sortable and filterable? Yeah! Do you also want the possibility to add, edit and remove items by dead simple templating? Hell yeah!
More examples are found at Listjs.com and Listjs.com/examples.html
HTML
<div id="hacker-list">
<ul class="list">
<li>
<h3 class="name">Jonny</h3>
<p class="city">Stockholm</p>
</li>
<li>
<h3 class="name">Jonas</h3>
<p class="city">Berlin</p>
</li>
</ul>
</div>
Javascript
var options = {
valueNames: [ 'name', 'city' ]
};
var hackerList = new List('hacker-list', options);
HTML
<div id="hacker-list">
<ul class="list"></ul>
</div>
<div style="display:none;">
<!-- A template element is needed when list is empty, TODO: needs a better solution -->
<li id="hacker-item">
<h3 class="name"></h3>
<p class="city"></p>
</li>
</div>
JavaScript
var options = {
item: 'hacker-item'
};
var values = [
{ name: 'Jonny', city:'Stockholm' }
, { name: 'Jonas', city:'Berlin' }
];
var hackerList = new List('hacker-list', options, values);
HTML
<div id="hacker-list">
<ul class="list">
<li>
<h3 class="name">Jonny</h3>
<p class="city">Stockholm</p>
</li>
</ul>
</div>
JavaScript
var options = {
valueNames: ['name', 'city']
};
var hackerList = new List('hacker-list', options);
hackerList.add( { name: 'Jonas', city:'Berlin' } );
HTML
<div id="hacker-list">
<input class="search" />
<span class="sort" rel="name">Sort by name</span>
<span class="sort" rel="city">Sort by city</span>
<ul class="list">
<li>
<h3 class="name">Jonny</h3>
<p class="city">Stockholm</p>
</li>
<li>
<h3 class="name">Jonas</h3>
<p class="city">Berlin</p>
</li>
</ul>
</div>
Javascript (nothing special)
var options = {
valueNames: [ 'name', 'city' ]
};
var hackerList = new List('hacker-list', options);
The secret behind the search field, the sort buttons and the list container element are the classes. By default does all inputs with class search becomes search fields for the list.
<input type="text" class="search" />
The sorting gets activated for all elements with class sort and then sorts the valueName corresponding the the rel value of the element.
<span class="sort" rel="name">Sort names</span>
The element containing the list have to have the class list (or one that you define)
<ul class="list"></ul> # Can be a div, table, dl, or whatever fits your purpose
All of these classes can be defined by yourself when creating the list by setting the options searchClass, listClass and sortClass.
valueNames (Array, default: null) (*only required if list already contains items before initialization)
If the list contains items on initialization does this array
have to contain the value names (class names) for the different values of
each list item.
<ul class="list">
<li>
<span class="name">Jonny</span>
<span class="city">Sundsvall</span>
</li>
</ul>
var valueNames = ['name', 'city'];
item (String, default: undefined)
ID to item template element
listClass (String, default: "list")
What is class of the list-container?
searchClass (String, default: "search")
What is class of the search field?
sortClass (String, default: "sort")
What is class of the sort buttons?
indexAsync (Boolean, default: false)
If there already are items in the list to which the
List.js-script is added, should the indexing be done
in a asynchronous way? Good for large lists (> 500 items).
maxVisibleItemsCount (Int, default: 200) Defines how many items that should be visible at the same time. This affects performance.
These methods are available for the List-object.
add(values)
Adds one or more items to the list.
listObj.add({ name: "Jonny", city: "Stockholm" });
listObj.add([
{ name: "Gustaf", city: "Sundsvall" }
, { name: "Jonas", city: "Berlin" }
]);
addAsync(values)
Adds one or more items the the list in a asynchronous way, works like regular .add()
but better if adding very many items (100+ or something).
remove(valueName, value)
Removes items from the list where the value named valueName has value value.
Returns the count of items that where removed.
itemsInList = [
{ id: 1, name: "Jonny" }
, { id: 2, name "Gustaf" }
]
listObj.remove("id", 1); -> return 1
get(valueName, value)
Returns values from the list where the value named valueName has value value.
itemsInList = [
{ id: 1, name: "Jonny" }
, { id: 2, name "Gustaf" }
]
listObj.get("id", 2); -> return { id: 2, name "Gustaf" }
sort(valueName, sortFunction)
Sorts the list based in values in column named valeuName. The sortFunction
parameter is used if you want to make you one sort function.
Default sort function is found here http://my.opera.com/GreyWyvern/blog/show.dml/1671288
search(searchString, columns)
Searches the list
filter(filterFunction)
itemsInList = [
{ id: 1, name: "Jonny" }
, { id: 2, name "Gustaf" }
, { id: 3, name "Jonas" }
]
listObj.filter(function(itemValues) {
if (itemValues.id > 1) {
return true;
} else {
return false;
}
}); -> Only items with id > 1 are shown in list
size()
Returns the size of the list
These methods are available for all Items that are returned by the list.
values(newValues)
newValues optional If variable newValues are present the new values replaces the current item values and updates the list. If newValues are not present, the function returns the current values.
item.values() -> { name: "Jonny", age: 24, city: "Umeå" }
item.values({
age: 25,
city: "Stockholm"
});
item.values() -> { name: "Jonny", age: 25, city: "Stockholm" }
show() Shows the item
hide() Hides the item (removes the element from the list, and then when its shown its appended again, the element will thereby change position in the list, bug, but a good solution is yet to find)
Only needed if you want to build you own template engine
None
get(item, valueNames) Get values from item corresponding to valueNames
set(item, values) Sets values to item
create(item) Creates html element and adds to item
add(item) Adds a items html element to the list
remove(item) Removes item
show(item) Shows item
hide(item) Hides the item
clear() Removes all items from the list
Called by ListJsHelpers.functionName()
getByClass()
http://www.dustindiaz.com/getelementsbyclass
addEvent()
http://net.tutsplus.com/tutorials/javascript-ajax/javascript-from-null-cross-browser-event-binding/
Updated in some ways, thought.
getAttribute()
http://stackoverflow.com/questions/3755227/cross-browser-javascript-getattribute-method
isNodeList()
http://stackoverflow.com/questions/7238177/detect-htmlcollection-nodelist-in-javascript
Type just ant in the console while in root folder.
Copyright (c) 2011 Jonny Strömberg http://jonnystromberg.se/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
| Back | FazBrowse Home | New Git URL |