|
| 1 | +# vue-column-sortable |
| 2 | +[](https://vuejs.org/) [](https://www.npmjs.com/package/vue-column-sortable) |
| 3 | + |
| 4 | +vue-column-sortable is an data sortable directive for vue.js. |
| 5 | + |
| 6 | +### Demo |
| 7 | +[](https://codesandbox.io/s/jvj47rxz33?module=%2Fsrc%2FApp.vue) |
| 8 | + |
| 9 | +### Install |
| 10 | + |
| 11 | +- In ES2015 |
| 12 | + ```Bash |
| 13 | + npm install vue-column-sortable --save |
| 14 | + ``` |
| 15 | + ```JavaScript |
| 16 | + //globally |
| 17 | + import columnSortable from 'vue-column-sortable' |
| 18 | + Vue.use(columnSortable) |
| 19 | + |
| 20 | + //for a single instance |
| 21 | + import columnSortable from 'vue-column-sortable' |
| 22 | + new Vue({ |
| 23 | + directives: {columnSortable} |
| 24 | + }) |
| 25 | + ``` |
| 26 | + |
| 27 | +- Direct include |
| 28 | + |
| 29 | + ```html |
| 30 | + < script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/vue-column-sortable.js"></ script> |
| 31 | + ``` |
| 32 | + |
| 33 | +### Usage |
| 34 | + |
| 35 | +- Step 1 |
| 36 | + - Use v-column-sortable:{ data key } in HTML |
| 37 | + ```html |
| 38 | + <table> |
| 39 | + <thead> |
| 40 | + <th v-column-sortable:name>Name</th> |
| 41 | + <th v-column-sortable:age>Age</th> |
| 42 | + . |
| 43 | + . |
| 44 | + </thead> |
| 45 | + </table> |
| 46 | + ``` |
| 47 | +- Step 2 |
| 48 | + - copy method: orderBy |
| 49 | + |
| 50 | + ```javascript |
| 51 | + new Vue({ |
| 52 | + el: '#app', |
| 53 | + data: { |
| 54 | + userArray: [ |
| 55 | + { name: 'John', age: 20 }, |
| 56 | + { name: 'Peter', age: 22 }, |
| 57 | + ] |
| 58 | + }, |
| 59 | + directives: {columnSortable}, |
| 60 | + methods:{ |
| 61 | + orderBy(sortFn) { |
| 62 | + // sort your array data like this.userArray |
| 63 | + this.userArray.sort(sortFn); |
| 64 | + }, |
| 65 | + } |
| 66 | + }) |
| 67 | + ``` |
| 68 | + #### Options |
| 69 | + |
| 70 | + ###### showIcon |
| 71 | + Default `true` |
| 72 | + Show sort icon with `fortawesome` svg |
| 73 | + |
| 74 | + ```javascript |
| 75 | + Vue.use(columnSortable, { |
| 76 | + showIcon: false, |
| 77 | + }); |
| 78 | + ``` |
| 79 | + |
| 80 | +### Code Example |
| 81 | + |
| 82 | +<img src="https://github.com/runkids/vue-column-sortable/blob/master/demo/column-sortable.gif?raw=true"/> |
| 83 | + |
| 84 | +```html |
| 85 | +<template> |
| 86 | + <div class="about"> |
| 87 | + <table border="1"> |
| 88 | + <thead> |
| 89 | + <th>#</th> |
| 90 | + <th v-column-sortable:name>Name</th> |
| 91 | + <th v-column-sortable:birthday>Date of Birth</th> |
| 92 | + <th v-column-sortable:point>Point</th> |
| 93 | + <th v-column-sortable:address.country>Country</th> |
| 94 | + <th v-column-sortable:address.city>City</th> |
| 95 | + </thead> |
| 96 | + <tbody> |
| 97 | + <tr v-for="(d,index) in dataArray" :key="index"> |
| 98 | + <td>{{ index+1 }}</td> |
| 99 | + <td>{{ d.name }}</td> |
| 100 | + <td>{{ d.birthday }}</td> |
| 101 | + <td>{{ d.point }}</td> |
| 102 | + <td>{{ d.address.country }}</td> |
| 103 | + <td>{{ d.address.city }}</td> |
| 104 | + </tr> |
| 105 | + </tbody> |
| 106 | + </table> |
| 107 | + </div> |
| 108 | +</template> |
| 109 | +``` |
| 110 | +```javascript |
| 111 | +<script> |
| 112 | +import columnSortable from 'vue-column-sortable' |
| 113 | + |
| 114 | +export default { |
| 115 | + data() { |
| 116 | + return { |
| 117 | + dataArray: [ |
| 118 | + { |
| 119 | + name: 'Jimmy Rutherford', |
| 120 | + birthday: '1945-5-6', |
| 121 | + point: 100, |
| 122 | + address: { |
| 123 | + country: 'United States of America', |
| 124 | + city: 'South Ryann', |
| 125 | + }, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: 'Camila Gutmann', |
| 129 | + birthday: '1950-11-16', |
| 130 | + point: 230, |
| 131 | + address: { |
| 132 | + country: 'Taiwan', |
| 133 | + city: 'Lake Destinview', |
| 134 | + }, |
| 135 | + }, |
| 136 | + ], |
| 137 | + }; |
| 138 | + }, |
| 139 | + methods: { |
| 140 | + orderBy(fn) { |
| 141 | + this.dataArray.sort(fn); |
| 142 | + }, |
| 143 | + }, |
| 144 | + directives: { |
| 145 | + columnSortable, |
| 146 | + }, |
| 147 | +}; |
| 148 | +</script> |
| 149 | +``` |
| 150 | + |
| 151 | +### License |
| 152 | + |
| 153 | +MIT |
0 commit comments