# mixitup.Mixer
## Overview
The `mixitup.Mixer` class is used to hold discreet, user-configured
instances of MixItUp on a provided container element.
Mixer instances are returned whenever the `mixitup()` factory function is called,
which expose a range of methods enabling API-based filtering, sorting,
insertion, removal and more.
### Contents
- [show()](#show)
- [hide()](#hide)
- [isMixing()](#isMixing)
- [filter()](#filter)
- [toggleOn()](#toggleOn)
- [toggleOff()](#toggleOff)
- [sort()](#sort)
- [changeLayout()](#changeLayout)
- [dataset()](#dataset)
- [multimix()](#multimix)
- [insert()](#insert)
- [insertBefore()](#insertBefore)
- [insertAfter()](#insertAfter)
- [prepend()](#prepend)
- [append()](#append)
- [remove()](#remove)
- [getConfig()](#getConfig)
- [configure()](#configure)
- [getState()](#getState)
- [forceRefresh()](#forceRefresh)
- [forceRender()](#forceRender)
- [destroy()](#destroy)
show()
*Version added: 3.0.0*
`.show()`
A shorthand method for `.filter('all')`. Shows all targets in the container.
| |Type | Name | Description
|---|--- | --- | ---
|Returns |`Promise.` |
###### Example: Showing all targets
```js
mixer.show()
.then(function(state) {
console.log(state.totalShow === state.totalTargets); // true
});
```
hide()
*Version added: 3.0.0*
`.hide()`
A shorthand method for `.filter('none')`. Hides all targets in the container.
| |Type | Name | Description
|---|--- | --- | ---
|Returns |`Promise.` |
###### Example: Hiding all targets
```js
mixer.hide()
.then(function(state) {
console.log(state.totalShow === 0); // true
console.log(state.totalHide === state.totalTargets); // true
});
```
isMixing()
*Version added: 2.0.0*
`.isMixing()`
Returns a boolean indicating whether or not a MixItUp operation is
currently in progress.
| |Type | Name | Description
|---|--- | --- | ---
|Returns |`boolean` |
###### Example: Checking the status of a mixer
```js
mixer.sort('random', function() {
console.log(mixer.isMixing()) // false
});
console.log(mixer.isMixing()) // true
```
filter()
*Version added: 2.0.0*
`.filter(selector [, animate] [, callback])`
Filters all targets in the container by a provided selector string, or the values `'all'`
or `'none'`. Only targets matching the selector will be shown.
| |Type | Name | Description
|---|--- | --- | ---
|Param |`string, HTMLElement, Array.` | `selector` | Any valid CSS selector (i.e. `'.category-a'`), or the values `'all'` or `'none'`. The filter method also accepts a reference to single target element or a collection of target elements to show.
|Param |`boolean` | `[animate]` | An optional boolean dictating whether the operation should animate, or occur syncronously with no animation. `true` by default.
|Param |`function` | `[callback]` | An optional callback function to be invoked after the operation has completed.
|Returns |`Promise.` | A promise resolving with the current state object.
###### Example 1: Filtering targets by a class selector
```js
mixer.filter('.category-a')
.then(function(state) {
console.log(state.totalShow === containerEl.querySelectorAll('.category-a').length); // true
});
```
###### Example 2: Filtering targets by an attribute selector
```js
mixer.filter('[data-category~="a"]')
.then(function(state) {
console.log(state.totalShow === containerEl.querySelectorAll('[data-category~="a"]').length); // true
});
```
###### Example 3: Filtering targets by a compound selector
```js
// Show only those targets with the classes 'category-a' AND 'category-b'
mixer.filter('.category-a.category-c')
.then(function(state) {
console.log(state.totalShow === containerEl.querySelectorAll('.category-a.category-c').length); // true
});
```
###### Example 4: Filtering via an element collection
```js
var collection = Array.from(container.querySelectorAll('.mix'));
console.log(collection.length); // 34
// Filter the collection manually using Array.prototype.filter
var filtered = collection.filter(function(target) {
return parseInt(target.getAttribute('data-price')) > 10;
});
console.log(filtered.length); // 22
// Pass the filtered collection to MixItUp
mixer.filter(filtered)
.then(function(state) {
console.log(state.activeFilter.collection.length === 22); // true
});
```
toggleOn()
*Version added: 3.0.0*
`.toggleOn(selector [, animate] [, callback])`
Adds an additional selector to the currently active filter selector, concatenating
as per the logic defined in `controls.toggleLogic`.
| |Type | Name | Description
|---|--- | --- | ---
|Param |`string` | `selector` | Any valid CSS selector (i.e. `'.category-a'`)
|Param |`boolean` | `[animate]` | An optional boolean dictating whether the operation should animate, or occur syncronously with no animation. `true` by default.
|Param |`function` | `[callback]` | An optional callback function to be invoked after the operation has completed.
|Returns |`Promise.` | A promise resolving with the current state object.
###### Example: Toggling on a filter selector
```js
console.log(mixer.getState().activeFilter.selector); // '.category-a'
mixer.toggleOn('.category-b')
.then(function(state) {
console.log(state.activeFilter.selector); // '.category-a, .category-b'
});
```
toggleOff()
*Version added: 3.0.0*
`.toggleOff(selector [, animate] [, callback])`
Removes a selector from the active filter selector.
| |Type | Name | Description
|---|--- | --- | ---
|Param |`string` | `selector` | Any valid CSS selector (i.e. `'.category-a'`)
|Param |`boolean` | `[animate]` | An optional boolean dictating whether the operation should animate, or occur syncronously with no animation. `true` by default.
|Param |`function` | `[callback]` | An optional callback function to be invoked after the operation has completed.
|Returns |`Promise.` | A promise resolving with the current state object.
###### Example: Toggling off a filter selector
```js
console.log(mixer.getState().activeFilter.selector); // '.category-a, .category-b'
mixer.toggleOff('.category-b')
.then(function(state) {
console.log(state.activeFilter.selector); // '.category-a'
});
```
sort()
*Version added: 2.0.0*
`.sort(sortString [, animate] [, callback])`
Sorts all targets in the container according to a provided sort string.
| |Type | Name | Description
|---|--- | --- | ---
|Param |`string, Array.` | `sortString` | A valid sort string (e.g. `'default'`, `'published-date:asc'`, or `'random'`). The sort method also accepts an array of all target elements in a user-defined order.
|Param |`boolean` | `[animate]` | An optional boolean dictating whether the operation should animate, or occur syncronously with no animation. `true` by default.
|Param |`function` | `[callback]` | An optional callback function to be invoked after the operation has completed.
|Returns |`Promise.` | A promise resolving with the current state object.
###### Example 1: Sorting by the default DOM order
```js
// Reverse the default order of the targets
mixer.sort('default:desc')
.then(function(state) {
console.log(state.activeSort.attribute === 'default'); // true
console.log(state.activeSort.order === 'desc'); // true
});
```
###### Example 2: Sorting by a custom data-attribute
```js
// Sort the targets by the value of a `data-published-date` attribute
mixer.sort('published-date:asc')
.then(function(state) {
console.log(state.activeSort.attribute === 'published-date'); // true
console.log(state.activeSort.order === 'asc'); // true
});
```
###### Example 3: Sorting by multiple attributes
```js
// Sort the targets by the value of a `data-published-date` attribute, then by `data-title`
mixer.sort('published-date:desc data-title:asc')
.then(function(state) {
console.log(state.activeSort.attribute === 'published-date'); // true
console.log(state.activeSort.order === 'desc'); // true
console.log(state.activeSort.next.attribute === 'title'); // true
console.log(state.activeSort.next.order === 'asc'); // true
});
```
###### Example 4: Sorting by random
```js
mixer.sort('random')
.then(function(state) {
console.log(state.activeSort.order === 'random') // true
});
```
###### Example 5: Sorting via an element collection
```js
var collection = Array.from(container.querySelectorAll('.mix'));
// Swap the position of two elements in the collection:
var temp = collection[1];
collection[1] = collection[0];
collection[0] = temp;
// Pass the sorted collection to MixItUp
mixer.sort(collection)
.then(function(state) {
console.log(state.targets[0] === collection[0]); // true
});
```
changeLayout()
*Version added: 2.0.0*
`.changeLayout(containerClassName [, animate] [, callback])`
Changes the layout of the container by adding, removing or updating a
layout-specific class name. If `animation.animateResizetargets` is
enabled, MixItUp will attempt to gracefully animate the width, height,
and position of targets between layout states.
| |Type | Name | Description
|---|--- | --- | ---
|Param |`string` | `containerClassName` | A layout-specific class name to add to the container.
|Param |`boolean` | `[animate]` | An optional boolean dictating whether the operation should animate, or occur syncronously with no animation. `true` by default.
|Param |`function` | `[callback]` | An optional callback function to be invoked after the operation has completed.
|Returns |`Promise.` | A promise resolving with the current state object.
###### Example 1: Adding a new class name to the container
```js
mixer.changeLayout('container-list')
.then(function(state) {
console.log(state.activeContainerClass === 'container-list'); // true
});
```
###### Example 2: Removing a previously added class name from the container
```js
mixer.changeLayout('')
.then(function(state) {
console.log(state.activeContainerClass === ''); // true
});
```
dataset()
*Version added: 3.0.0*
`.dataset(dataset [, animate] [, callback])`
Updates the contents and order of the container to reflect the provided dataset,
if the dataset API is in use.
The dataset API is designed for use in API-driven JavaScript applications, and
can be used instead of DOM-based methods such as `.filter()`, `.sort()`,
`.insert()`, etc. When used, insertion, removal, sorting and pagination can be
achieved purely via changes to your data model, without the uglyness of having
to interact with or query the DOM directly.
| |Type | Name | Description
|---|--- | --- | ---
|Param |`Array.