1 | <template>
|
2 | <div>
|
3 |
|
4 | <slot name="header"></slot>
|
5 |
|
6 | <table class="grid">
|
7 |
|
8 | </table>
|
9 |
|
10 |
|
11 | <slot name="footer"></slot>
|
12 | </div>
|
13 | </template>
|
14 |
|
15 | <script>
|
16 | const text = { ala: 'makota' }
|
17 |
|
18 | /**
|
19 | * This is an example of creating a reusable grid component and using it with external data.
|
20 | * @version 1.0.5
|
21 | * @author [Rafael](https:
|
22 | * @since Version 1.0.1
|
23 | */
|
24 | export default {
|
25 | name: 'grid',
|
26 | props: {
|
27 |
|
28 | /**
|
29 | * object/array defaults should be returned from a factory function
|
30 | * @version 1.0.5
|
31 | * @since Version 1.0.1
|
32 | * @see See [Wikipedia](https:
|
33 | * @link See [Wikipedia](https:
|
34 | */
|
35 | msg: {
|
36 | type: [String, Number],
|
37 | default: (text) => 'ala',
|
38 | required: true,
|
39 | },
|
40 | /**
|
41 | * Model example
|
42 | * @model
|
43 | */
|
44 | value: {
|
45 | type: String,
|
46 | default: 'something'
|
47 | },
|
48 | /**
|
49 | * describe data
|
50 | * @version 1.0.5
|
51 | */
|
52 | data: [Array],
|
53 | /**
|
54 | * get columns list
|
55 | */
|
56 | columns: [Array],
|
57 | /**
|
58 | * filter key
|
59 | */
|
60 | filterKey: {
|
61 | type: String,
|
62 | default: 'example'
|
63 | }
|
64 | },
|
65 | data () {
|
66 | var sortOrders = {}
|
67 | this.columns.forEach(function (key) {
|
68 | sortOrders[key] = 1
|
69 | })
|
70 | return {
|
71 | sortKey: '',
|
72 | sortOrders: sortOrders
|
73 | }
|
74 | },
|
75 | computed: {
|
76 | filteredData: function () {
|
77 | var sortKey = this.sortKey
|
78 | var filterKey = this.filterKey && this.filterKey.toLowerCase()
|
79 | var order = this.sortOrders[sortKey] || 1
|
80 | var data = this.data
|
81 | if (filterKey) {
|
82 | data = data.filter(function (row) {
|
83 | return Object.keys(row).some(function (key) {
|
84 | return String(row[key]).toLowerCase().indexOf(filterKey) > -1
|
85 | })
|
86 | })
|
87 | }
|
88 | if (sortKey) {
|
89 | data = data.slice().sort(function (a, b) {
|
90 | a = a[sortKey]
|
91 | b = b[sortKey]
|
92 | return (a === b ? 0 : a > b ? 1 : -1) * order
|
93 | })
|
94 | }
|
95 | return data
|
96 | }
|
97 | },
|
98 | filters: {
|
99 | capitalize: function (str) {
|
100 | return str.charAt(0).toUpperCase() + str.slice(1)
|
101 | }
|
102 | },
|
103 | methods: {
|
104 |
|
105 | /**
|
106 | * Sets the order
|
107 | *
|
108 | * @public
|
109 | * @version 1.0.5
|
110 | * @since Version 1.0.1
|
111 | * @param {string} key Key to order
|
112 | * @returns {string} Test
|
113 | */
|
114 | sortBy: function (key) {
|
115 | this.sortKey = key
|
116 | this.sortOrders[key] = this.sortOrders[key] * -1;
|
117 |
|
118 | /**
|
119 | * Success event.
|
120 | *
|
121 | * @event success
|
122 | * @type {object}
|
123 | */
|
124 | this.$emit('success', {
|
125 | demo: 'example',
|
126 | })
|
127 | },
|
128 |
|
129 | hiddenMethod: function(){
|
130 |
|
131 | }
|
132 | }
|
133 | }
|
134 | </script> |
\ | No newline at end of file |