Write less. With BreedJS you won't spend your time writing repetitive HTML structures or developing complex functions to deal with filters, sorts and pagination. BreedJS takes care of this things for you with few commands. Learn how to use it checking the examples below and see which one fits to you. Visit the Github repo.
Click here to download the last .min version.
<script src="path/to/breed.js"></script>
Set the scope that you're going to work with.
<div b-scope="animals"></div>
Set the default scope when calling breed. Then you don't need to tell which scope you are using everytime you call breed.
breed.setDefaultScope('animals')
<div b-scope="animals"></div>
Activate a scope. The commands you passed in the tag where you set the scope only will be activated after you call this function.
var data = { animals_list: ["mouse", "cat", "dog"] }; breed.run({ scope: 'animals', input: data });
<div b-scope="animals"></div>
Object keys
Set commands to be executed when activate the scope.
Loops the tag where the command is through a scope iterable variable. This command will also create the inner tags.
var data = { animals_list: ["mouse", "cat", "dog"] }; breed.run({ scope: 'animals', input: data });
<div b-scope="animals" b-loop="animal in animals_list"></div>Result:
<div b-scope="animals"></div> <div b-scope="animals"></div> <div b-scope="animals"></div>
Its also possible to set javascript expressions between double brackets: {{your_expression}}. In this way, you can get more advantge over your loop command:
<div b-scope="animals" b-loop="animal in animals_list"> {{"This is a " + animal}} </div>Result:
<div b-scope="animals">This is a mouse</div> <div b-scope="animals">This is a cat</div> <div b-scope="animals">This is a dog</div>
Expressions can also be used inside attributes.
You can access the loop index using the variable #index inside an expression.
<div b-scope="animals" id="{{#index}}" b-loop="animal in animals_list"> {{"This is a " + animal}} </div>Result:
<div b-scope="animals" id="0">This is a mouse</div> <div b-scope="animals" id="1">This is a cat</div> <div b-scope="animals" id="2">This is a dog</div>
In order to access global variables inside expressions, you just need no call it, but to access variables from scope data, you need to use # before the variable name.
var hey = "Hey! "; var data = { animals_list: ["mouse", "cat", "dog"], text: "This is a " }; breed.run({ scope: 'animals', input: data });
<div b-scope="animals" b-loop="animal in animals_list"> <b>{{hey}}</b> {{#text + animal}} </div>Result:
<div b-scope="animals"><b>Hey!</b> This is a mouse</div> <div b-scope="animals"><b>Hey!</b> This is a cat</div> <div b-scope="animals"><b>Hey!</b> This is a dog</div>
Just pass your function to loopEnd key inside the run object to be called when the loop is over:
breed.run({ scope: 'animals', input: data, loopEnd: function(){ //here goes your code } });
This command will paginate the DOM elements at your scope that you created with b-loop. Paginating works with filters and sorts.
var data = { animals_list: ["bee", "mouse", "cat", "dog", "wolf", "lion"] }; breed.run({ scope: 'animals', input: data });
<div b-scope="animals" b-loop="animal in animals_list" b-paginate="3">{{animal}}</div>Result:
<div b-scope="animals">bee</div> <div b-scope="animals">mouse</div> <div b-scope="animals">cat</div> <div style="display:none;" b-scope="animals">dog</div> <div style="display:none;" b-scope="animals">wolf</div> <div style="display:none;" b-scope="animals">lion</div>
That's the function that really makes the pagination happen. Whenever you want to change pages, that's the one to be called.
Object keys
breed.paginate({ scope: 'animals', page: 2 });
<div style="display:none;" b-scope="animals">bee</div> <div style="display:none;" b-scope="animals">mouse</div> <div style="display:none;" b-scope="animals">cat</div> <div b-scope="animals">dog</div> <div b-scope="animals">wolf</div> <div b-scope="animals">lion</div>
This function changes how many items is shown per page.
Object keys
breed.setPageLength({ scope: 'animals', page_length: 6 });
In our example, that's going to result at showing all elements.
This function returns how many pages there are in your scope.
breed.getPageCount('animals');
If in our example the page_length value is 3, then this function will return 2.
This function returns the actual page.
breed.getActualPage('animals');
The first page index is 1. So if you are at page 2, for example, you'll get 2 from this function.
Just pass your function to pageChangeEnd key inside the run object to be called when the page change process is over:
breed.run({ scope: 'animals', input: data, pageChangeEnd: function(){ //here goes your code } });
Sorting can be very easy, simple and flexible with BreedJS. With few key configurations, you will be able to sort everything you want. Sort works with filters and pagination.
Consider this example:
var data = { animals_list: [ {name: 'dog', age: 4}, {name: 'cat', age: 7}, {name: 'fish', age: 1} ] }; breed.run({ scope: 'animals', input: data });
<div b-scope="animals" b-loop="animal in animals_list">{{animal.name}}</div>
This will generate divs with animals' name.
Everytime we want to sort these divs, we can do just like so:
breed.sort({ scope: 'animals' });
The result is going to be:
<div b-scope='animals'>cat</div> <div b-scope='animals'>dog</div> <div b-scope='animals'>fish</div>
We sorted this scope by the divs text.
This sort function can be more flexible. Let us take a look at sort options:
Object keys
Let's take a look at selector option. Consider this example:
<tr b-scope="animals" b-loop="animal in animals_list"> <td b-sort="name">{{animal.name}}</td> <td b-sort="age">{{animal.age}}</td> </tr>
This will generate table rows with animals name and age info. We put the b-sort attribute in order to sort the row by name or by age.
Now, if we want to sort it by name, we do this:
breed.sort({ scope: 'animals', selector: 'name' //the value inside the attribute b-sort });
And that's the result:
<tr b-scope="animals"> <td b-sort="name">cat</td> <td b-sort="age">7</td> </tr> <tr b-scope="animals"> <td b-sort="name">dog</td> <td b-sort="age">4</td> </tr> <tr b-scope="animals"> <td b-sort="name">fish</td> <td b-sort="age">1</td> </tr>
You can edit the analyzed text using the rule option:
breed.sort({ scope: 'animals', selector: 'age', rule: function(age){ return age == 1 ? age : -1 * (age % 2); } });
That's going to apply the rule for each age value and sort based on it. See the result:
<tr b-scope="animals"> <td b-sort="name">cat</td> <td b-sort="age">7</td> <!-- The rule result is -1 --> </tr> <tr b-scope="animals"> <td b-sort="name">dog</td> <td b-sort="age">4</td> <!-- The rule result is 0 --> </tr> <tr b-scope="animals"> <td b-sort="name">fish</td> <td b-sort="age">1</td> <!-- The rule result is 1 --> </tr>
You can have all the breed.sort() features at breed.sortAttr() function, but in sortAttr you don't sort by the tag inner text value, you sort by a given attribute.
Object keys
Consider this example:
<tr b-scope="animals" b-loop="animal in animals_list"> <td age="{{animal.age}}">{{animal.name}}</td> </tr>
Now, we want to sort this by the attribute age. This can be easily done with this command:
breed.sortAttr({ scope: 'animals', attr: 'age' });
And we got this result:
<tr b-scope="animals"> <td age="1">fish</td> </tr> <tr b-scope="animals"> <td age="4">dog</td> </tr> <tr b-scope="animals"> <td age="7">cat</td> </tr>
Now consider this other example:
<tr age="{{animal.age}}" b-scope="animals" b-loop="animal in animals_list"> <td age="{{animal.age == 1 ? animal.age : -1 * (animal.age % 2)}}" b-sort="name">{{animal.name}}</td> </tr>
We want to sort by the age attribute inside td tag. But using just the attr option, It will sort by the tr tag. To solve this, we back with b-sort attribute to help BreedJS find what attribute you're talking about. See:
breed.sort({ scope: 'animals', attr: 'age', selector: 'name' });
Result:
<tr age="7" b-scope="animals"> <td age="-1" b-sort="name">cat</td> </tr> <tr age="4" b-scope="animals"> <td age="0" b-sort="name">dog</td> </tr> <tr age="1" b-scope="animals"> <td age="1" b-sort="name">fish</td> </tr>
We provide a callback option for anykind of sort you do:
breed.run({ scope: 'animals', input: data, sortEnd: function(){ //here goes your code } });
Filter things usually can be very hard. But with BreedJS, It gets very simple and really flexible to your needs. Filter works with pagination and sorting.
Consider this data for our examples:
var data = { animals_list: [ {name: 'dog', age: 6}, {name: 'cat', age: 3}, {name: 'fish', age: 1}, {name: 'camel', age: 9}, {name: 'flamingo', age: 2} ] }; breed.run({ scope: 'animals', input: data });
<div b-scope="animals" b-loop="animal in animals_list">{{animal.name}}</div>
This will generate divs with animals' name.
Everytime we want to filter these divs, we can do just like so:
breed.filter({ scope: 'animals', key: 'ca' });
And that's gonna be the result:
<div style="display:none;" b-scope="animals">dog</div> <div b-scope="animals">cat</div> <div style="display:none;" b-scope="animals">fish</div> <div b-scope="animals">camel</div> <div style="display:none;" b-scope="animals">flamingo</div>
We filtered this scope by the divs text.
This filter function can be more flexible. Let us take a look at filter options:
Object keys
Let's take a look at field option. Consider this example:
<tr b-scope="animals" b-loop="animal in animals_list"> <td b-filter="name">{{animal.name}}</td> <td b-filter="age">{{animal.age}}</td> </tr>
This will generate table rows with animals name and age info. We put the b-filter attribute in order to filter the row by name or by age.
Now, if we want to filter it by name, we do this:
breed.filter({ scope: 'animals', field: 'name', //the value inside the attribute b-filter key: 'dog' });
And that's the result:
<tr b-scope="animals"> <td b-filter="name">dog</td> <td b-filter="age">6</td> </tr> <tr style="display:none;" b-scope="animals"> <td b-filter="name">cat</td> <td b-filter="age">3</td> </tr> <tr style="display:none;" b-scope="animals"> <td b-filter="name">fish</td> <td b-filter="age">1</td> </tr> <tr style="display:none;" b-scope="animals"> <td b-filter="name">camel</td> <td b-filter="age">9</td> </tr> <tr style="display:none;" b-scope="animals"> <td b-filter="name">flamingo</td> <td b-filter="age">2</td> </tr>
You can edit the filter pattern using the rule option:
breed.sort({ scope: 'animals', field: 'age', rule: function(age){ return age < 4; } });
That's going to apply the rule for the filter checking. See the result:
<tr style="display:none;" b-scope="animals"> <td b-filter="name">dog</td> <td b-filter="age">6</td> </tr> <tr b-scope="animals"> <td b-filter="name">cat</td> <td b-filter="age">3</td> </tr> <tr b-scope="animals"> <td b-filter="name">fish</td> <td b-filter="age">1</td> </tr> <tr style="display:none;" b-scope="animals"> <td b-filter="name">camel</td> <td b-filter="age">9</td> </tr> <tr b-scope="animals"> <td b-filter="name">flamingo</td> <td b-filter="age">2</td> </tr>
Only ages less than 4 got displayed.
With BreedJS, you can stack filters (filter of the filter) in a very neat way.
Consider this new situation:
var data = { people: [ {name: 'john', age: 18, birth: '23/06/1998'}, {name: 'josh', age: 13, birth: '12/04/2003'}, {name: 'jake', age: 20, birth: '11/09/1996'}, {name: 'jason', age: 19, birth: '13/08/1997'}, {name: 'james', age: 17, birth: '24/05/1999'} ] }; breed.run({ scope: 'people', input: data });
<tr b-scope="people" b-loop="person in people"> <td b-filter="name">{{person.name}}</td> <td b-filter="age">{{person.age}}</td> <td b-filter="birth">{{person.birth}}</td> </tr>
Now, we want to filter it by name and birthdate. So we do:
function compare(date){ return Date.parse(date) < Date.parse('01/01/2000'); } breed.filter({ scope: 'people', field: ['name', 'birth'], rule: ['jo', compare] });
Here is the result:
<tr b-scope="people"> <td b-filter="name">john</td> <td b-filter="age">18</td> <td b-filter="birth">23/06/1998</td> </tr> <tr style="display:none;" b-scope="people"> <td b-filter="name">josh</td> <td b-filter="age">13</td> <td b-filter="birth">12/04/2003</td> </tr> <tr style="display:none;" b-scope="people"> <td b-filter="name">jake</td> <td b-filter="age">20</td> <td b-filter="birth">11/09/1996</td> </tr> <tr style="display:none;" b-scope="people"> <td b-filter="name">jason</td> <td b-filter="age">19</td> <td b-filter="birth">13/08/1997</td> </tr> <tr style="display:none;" b-scope="people"> <td b-filter="name">james</td> <td b-filter="age">17</td> <td b-filter="birth">24/05/1999</td> </tr>
Data that contains 'jo' and has birthdate before 01/01/2000 got displayed.
If you don't have any rule function, you can set the key array on key option as well.
You can have all the breed.filter() features at breed.filterAttr() function, but in filterAttr you don't filter by the tag inner text value, you filter by a given attribute.
Object keys
Still with the people data example, let's do just like so:
<tr b-scope="people" b-loop="person in people"> <td age="{{person.age}}">{{person.name}}</td> </tr>
Now, we want to filter this by the attribute age. This can be easily done with this command:
breed.filterAttr({ scope: 'people', attr: 'age', rule: function(age){ return age < 19; } });
And that's the result:
<tr b-scope="people"> <td age="18">john</td> </tr> <tr b-scope="people"> <td age="13">josh</td> </tr> <tr style="display:none;" b-scope="people"> <td age="20">jake</td> </tr> <tr style="display:none;" b-scope="people"> <td age="19">jason</td> </tr> <tr b-scope="people"> <td age="17">james</td> </tr>
Now consider this other example:
<tr age="{{person.age}}" b-scope="people" b-loop="person in people"> <td age="{{person.age - 1}}" b-filter="name">{{person.name}}</td> </tr>
We want to filter by the age attribute inside td tag. But using just the attr option, It will filter by the tr tag. To solve this, we back with b-filter attribute to help BreedJS find what attribute you're talking about. See:
breed.filterAttr({ scope: 'people', attr: 'age', field: 'name', rule: function(age){ return age < 19; } });
And that's the result:
<tr age="18" b-scope="people"> <td age="17">john</td> </tr> <tr age="13" b-scope="people"> <td age="12">josh</td> </tr> <tr age="20" style="display:none;" b-scope="people"> <td age="19">jake</td> </tr> <tr age="19" b-scope="people"> <td age="18">jason</td> </tr> <tr age="17" b-scope="people"> <td age="16">james</td> </tr>
Here we give you freedom to select the information that you want to filter. In this function you pass a jQuery selector to find the element(s) you want to. This function will check the element inner text to filter. This function has the same features of filter.
Object keys
Using the people data example, check this out:
<tr b-scope="people" b-loop="person in people"> <td>{{person.name}}</td> <td>{{person.age}}</td> </tr>
In this example, we want to build a very generic filter. We want to give a filter key that will validade even for name and age at the same time. To do so, we do this:
breed.filterSelector({ scope: 'people', selector: 'td', key: ['18', 'jake'] //note that I'm stacking filters here });
Using td as a selector, I'm joining name and age, because They are the inner text of td tag. Here is the result:
<tr b-scope="people"> <td>john</td> <td>18</td> </tr> <tr style="display:none;" b-scope="people"> <td>josh</td> <td>13</td> </tr> <tr b-scope="people"> <td>jake</td> <td>20</td> </tr> <tr style="display:none;" b-scope="people"> <td>jason</td> <td>19</td> </tr> <tr style="display:none;" b-scope="people"> <td>james</td> <td>17</td> </tr>
We provide a callback option for anykind of filter you do:
breed.run({ scope: 'people', input: data, filterEnd: function(){ //here goes your code } });
We give you a little way to watch a variable that you want and print it at your page. See:
var my_value = null; breed.watch('my_value'); $("#txt").keyup(function() { my_value = $(this).val(); });
<input id="txt" type="text"><b-var var="my_value"></b-var>
The result:
Note that you have to use the b-var tag with an attribute var whose value is the variable name.
You can watch a custom object by passing it in the function:
var person = {name: 'John', age: 20}; breed.watch('name', person); breed.watch('age', person); $("#name").keyup(function() { person.name = $(this).val(); }); $("#age").keyup(function() { person.age = $(this).val(); });
<tr> <td><b-var var="name"></b-var></td> <td><b-var var="age"></b-var></td> </tr>
Then you get:
Name | Age |
---|---|
Some functions that can be useful.
This function checks if a string has an entire word:
breed.utils.hasWord('foo', 'foo boo bar'); // returns true breed.utils.hasWord('fo', 'foo boo bar'); // returns false
Pass true to the third arg to case sensitive.
This function removes accents of a string:
breed.utils.removeAccents('ãñáéíóô'); // returns 'anaeioo'