A Pen by Edward Lance Lorilla on CodePen.
Last active
October 17, 2019 00:17
-
-
Save edwardlorilla/00adcde4603a6d114ddf667ab05f2512 to your computer and use it in GitHub Desktop.
vue jquery datatable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="tabledemo"> | |
| <h3>Vue Datatable example</h3> | |
| Filter by anything: <input v-model="search"> | |
| <hr> | |
| <data-table :comments="filteredComments"></data-table> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Vue.component('data-table', { | |
| render: function (createElement) { | |
| return createElement( | |
| "table", null, [] | |
| ) | |
| }, | |
| props: ['comments'], | |
| data() { | |
| return { | |
| headers: [ | |
| { title: 'Name' }, | |
| { title: 'Email' }, | |
| { title: 'Body' }, | |
| ], | |
| rows: [] , | |
| dtHandle: null | |
| } | |
| }, | |
| watch: { | |
| comments(val, oldVal) { | |
| let vm = this; | |
| vm.rows = []; | |
| val.forEach(function (item) { | |
| let row = []; | |
| row.push(item.name); | |
| row.push('<a href="mailto://' + item.email + '">' + item.email + '</a>'); | |
| row.push(item.body); | |
| vm.rows.push(row); | |
| }); | |
| vm.dtHandle.clear(); | |
| vm.dtHandle.rows.add(vm.rows); | |
| vm.dtHandle.draw(); | |
| } | |
| }, | |
| mounted() { | |
| let vm = this; | |
| vm.dtHandle = $(this.$el).DataTable({ | |
| columns: vm.headers, | |
| data: vm.rows, | |
| searching: true, | |
| paging: true, | |
| info: false | |
| }); | |
| } | |
| }); | |
| new Vue({ | |
| el: '#tabledemo', | |
| data: { | |
| comments: [], | |
| search: '' | |
| }, | |
| computed: { | |
| filteredComments: function () { | |
| let self = this | |
| let search = self.search.toLowerCase() | |
| return self.comments.filter(function (comments) { | |
| return comments.name.toLowerCase().indexOf(search) !== -1 || | |
| comments.email.toLowerCase().indexOf(search) !== -1 || | |
| comments.body.toLowerCase().indexOf(search) !== -1 | |
| }) | |
| } | |
| }, | |
| mounted() { | |
| let vm = this; | |
| $.ajax({ | |
| url: 'https://jsonplaceholder.typicode.com/comments', | |
| success(res) { | |
| vm.comments = res; | |
| } | |
| }); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17-beta.0/vue.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> | |
| <script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <link href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment