Skip to content

Instantly share code, notes, and snippets.

@fkleuver
Forked from davismj/app.html
Created April 29, 2020 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fkleuver/42ea5b81feb5fc9e39b610d362c227b1 to your computer and use it in GitHub Desktop.
Save fkleuver/42ea5b81feb5fc9e39b610d362c227b1 to your computer and use it in GitHub Desktop.
Modify a template and rerender
<template>
<require from="custom-table"></require>
<custom-table items.bind="data">
<template replace-part="row">
<tr>
<th>${item.v}</th>
<th>${item.a}</th>
<th>${item.k}</th>
</tr>
</template>
</custom-table>
</template>
export class App {
data = [
{ v: 1, a: 1, k: 2 },
{ v: 3, a: 5, k: 8 },
{ v: 13, a: 22, k: 35 }
]
}
<template>
<style>th,td { border: 1px solid }</style>
<table>
<thead>
<th>one <button click.delegate="reorder()">></button></th>
<th>two</th>
<th>three</th>
</thead>
<tbody>
<tr repeat.for="item of items" part="row"></tr>
</tbody>
</table>
</template>
import { inject, bindable, TargetInstruction, ViewResources, ViewCompiler, TaskQueue } from 'aurelia-framework';
@inject(TargetInstruction, ViewResources, ViewCompiler, TaskQueue)
export class CustomTableCustomElement {
@bindable items = [];
constructor(inst, resources, compiler, tq) {
this.inst = inst.behaviorInstructions[0];
this.resources = resources;
this.compiler = compiler;
this.tq = tq;
}
reorder() {
const { inst, resources, compiler, tq } = this;
const template = inst.partReplacements.row.template;
const first = template.querySelector('th:first-child');
first.nextElementSibling.insertAdjacentElement('afterend', first);
inst.partReplacements.row = compiler.compile(template, resources);
const items = this.items.splice(0);
tq.queueMicroTask(() => {
this.items = items;
});
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/jspm_packages/system.js"></script>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/config.js"></script>
<script>
System.import('aurelia-bootstrapper');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment