<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<!DOCTYPE html><html class=''>
<head><script src='//production-assets.codepen.io/assets/editor/live/console_runner-079c09a0e3b9ff743e39ee2d5637b9216b3545af0de366d4b9aad9dc87e26bfd.js'></script><script src='//production-assets.codepen.io/assets/editor/live/events_runner-73716630c22bbc8cff4bd0f07b135f00a0bdc5d14629260c3ec49e5606f98fdd.js'></script><script src='//production-assets.codepen.io/assets/editor/live/css_live_reload_init-2c0dc5167d60a5af3ee189d570b1835129687ea2a61bee3513dee3a50c115a77.js'></script><meta charset='UTF-8'><meta name="robots" content="noindex"><link rel="shortcut icon" type="image/x-icon" href="//production-assets.codepen.io/assets/favicon/favicon-8ea04875e70c4b0bb41da869e81236e54394d63638a1ef12fa558a4a835f1164.ico" /><link rel="mask-icon" type="" href="//production-assets.codepen.io/assets/favicon/logo-pin-f2d2b6d2c61838f7e76325261b7195c27224080bc099486ddd6dccb469b8e8e6.svg" color="#111" /><link rel="canonical" href="https://codepen.io/stwilson/pen/oBRePd" />
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/css/jquery.dataTables.min.css'>
<style class="cp-pen-styles"></style></head><body>
<div id="tabledemo">
<h3>Vue Datatable example</h3>
Filter by anything: <input v-model="search">
<hr>
<data-table :users="filteredUsers"></data-table>
</div>
<script src='//production-assets.codepen.io/assets/common/stopExecutionOnTimeout-b2a7b3fe212eaa732349046d8416e00a9dec26eb7fd347590fbced3ab38af52e.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/js/jquery.dataTables.min.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.10/vue.min.js'></script>
<script >Vue.component('data-table', {
template: '<table></table>',
props: ['users'],
data() {
return {
headers: [
{ title: 'ID' },
{ title: 'Username', class: 'some-special-class' },
{ title: 'Real Name' },
{ title: 'Phone' },
{ title: 'Email' },
{ title: 'Website' }
],
rows: [] ,
dtHandle: null
}
},
watch: {
users(val, oldVal) {
let vm = this;
vm.rows = [];
// You should _probably_ check that this is changed data... but we'll skip that for this example.
val.forEach(function (item) {
// Fish out the specific column data for each item in your data set and push it to the appropriate place.
// Basically we're just building a multi-dimensional array here. If the data is _already_ in the right format you could
// skip this loop...
let row = [];
row.push(item.id);
row.push(item.username);
row.push(item.name);
row.push(item.phone);
row.push('<a href="mailto://' + item.email + '">' + item.email + '</a>');
row.push('<a href="http://' + item.website + '" target="_blank">' + item.website + '</a>');
vm.rows.push(row);
});
// Here's the magic to keeping the DataTable in sync.
// It must be cleared, new rows added, then redrawn!
vm.dtHandle.clear();
vm.dtHandle.rows.add(vm.rows);
vm.dtHandle.draw();
}
},
mounted() {
let vm = this;
// Instantiate the datatable and store the reference to the instance in our dtHandle element.
vm.dtHandle = $(this.$el).DataTable({
// Specify whatever options you want, at a minimum these:
columns: vm.headers,
data: vm.rows,
searching: false,
paging: false,
info: false
});
}
});
new Vue({
el: '#tabledemo',
data: {
users: [],
search: ''
},
computed: {
filteredUsers: function () {
let self = this
let search = self.search.toLowerCase()
return self.users.filter(function (user) {
return user.username.toLowerCase().indexOf(search) !== -1 ||
user.name.toLowerCase().indexOf(search) !== -1 ||
user.phone.indexOf(search) !== -1 ||
user.email.toLowerCase().indexOf(search) !== -1 ||
user.website.toLowerCase().indexOf(search) !== -1
})
}
},
mounted() {
let vm = this;
$.ajax({
url: 'https://jsonplaceholder.typicode.com/users',
success(res) {
vm.users = res;
}
});
}
});
//# sourceURL=pen.js
</script>
</body></html>