1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| ---HTML--- <div id="app"> <table class="table"> <tbody> <tr is="row-component-one" v-for="(item, key) in data" v-if="key % 2" :item="item" :key="key"></tr> <tr is="row-component-two" v-for="(item, key) in data" v-if="(key - 1) % 2" :item="item" :key="key"></tr> </tbody> </table> </div>
---JavaScript--- <script type="text/x-template" id="row-component"> <tr> <td>{{ item.name }}</td> <td>{{ item.cash | currency | dollarSign }}</td> <td>{{ item.icash | currency | dollarSign }}</td> </tr> </script>
<script type="text/x-template" id="row-component-two"> <tr class="bg-primary text-white"> <td>{{ item.name }}</td> <td>{{ item.cash | currency | dollarSign }}</td> <td>{{ item.icash | currency | dollarSign }}</td> </tr> </script>
<script> var childOne = { props: ['item'], data: function() { return { data: {}, extendData: '這段文字是 extend 得到' } }, template: '#row-component', filters: { dollarSign: function (n) { return `$ ${n}` }, currency: function(n) { return n.toFixed(2).replace(/./g, function(c, i, a) { return i && c !== "." && ((a.length - i) % 3 === 0) ? ',' + c : c; }); } }, mounted: function() { console.log('Extend:', this) } }
var childTwo = { props: ['item'], data: function() { return { data: {}, extendData: '這段文字是 extend 得到' } }, template: '#row-component-two', filters: { dollarSign: function (n) { return `$ ${n}` }, currency: function(n) { return n.toFixed(2).replace(/./g, function(c, i, a) { return i && c !== "." && ((a.length - i) % 3 === 0) ? ',' + c : c; }); } }, mounted: function() { console.log('Extend:', this) } } var app = new Vue({ el: '#app', data: { data: [ { name: '小明', cash: 100, icash: 500, }, { name: '杰倫', cash: 10000, icash: 5000, }, { name: '漂亮阿姨', cash: 500, icash: 500, }, { name: '老媽', cash: 10000, icash: 100, }, ] }, components: { "row-component-one": childOne, "row-component-two": childTwo, }, mounted: function() { console.log('Vue init:', this) } }); </script>
|