Templates API
tpl.swap()
asyncSwaps the positions of two template elements.
-
Accepts the template name, as a string, as the first argument.
Accepts second and third arguments, which can be one of the following:TypeDescriptionobjectThe template element whose values match the specified ones in the object (case-insensitive), or whose values match the specified regular expression in the object will be selected.arrayThe template element with the index corresponding to one of the specified numbers from the array will be selected.numberThe template element whose index corresponds to the specified number will be selected. -
Returns an array of objects, each of which has the following properties:
PropertyTypeDescriptionitDOM elementThe template element.dataobjectThe data of the current template element.
Returns null if at least one template element is not selected or if it is not possible to swap their positions. - It is not necessary to wait for the method to be executed using await if not needed.
Example:
await tpl.swap();
// false
await tpl.swap('tplName:second', 0, 1);
// null
await tpl.swap('tplName', 0, 1);
await tpl.swap('tplName', [0, 1], 1);
await tpl.swap('tplName', {name: 'book'}, {name: 'book 2'});
/*
[
{
it: div.field,
data: {name: 'book', secondName: 'cat'}
},
{
it: div.field,
data: {name: 'book 2', secondName: 'cat 2'}
}
]
*/
await tpl.swap('tplName', {name: /2/, secondName: 'cat 2'}, {name: 'book'});
await tpl.swap('tplName', {name: ['book 2', 'book']}, 0);
/*
[
{
it: div.field,
data: {name: 'book 2', secondName: 'cat 2'}
},
{
it: div.field,
data: {name: 'book', secondName: 'cat'}
}
]
*/