由于需要一个不确定数量的输入控件所以做了出来,然后封装了一下,大家自取
基于Element-UI的Vue动态输入框
由于需要一个不确定数量的输入控件所以做了出来,然后封装了一下,大家自取

vue源码:DynamicInput.vue
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
| <!-- ---- Title: 动态输入框 ---- Time: 2019/12/23 ---- By: 陈思奇 --> <template> <div> <div v-for="(item, index) in items" v-bind:key="index"> <el-input v-model="item.name" :placeholder="'请输入' + config.title" class="dynamic-input" align="right"> </el-input> <el-button type="danger" icon="el-icon-delete" class="delete-input" v-if="index !== 0" @click="deleteItem(index)"> </el-button> </div> <el-button type="primary" icon="el-icon-plus" @click="addItem" style="float: left"> {{'添加' + config.title}} </el-button> <el-button v-if="config.showMsg" @click="print" style="float: left"> 输出信息 </el-button> </div> </template>
<script> export default { name: 'DynamicInput', props: { // 输入数组 items: { type: Array, default: () => [ { name: '' } ] }, // 配置 config: { type: Object, default: () => [{ title: '', showMsg: false }] } }, methods: { addItem () { this.items.push({ name: '' }) }, deleteItem (index) { this.items.splice(index, 1) }, print () { let itemNames = '' for (let i = 0; i < this.items.length; i++) { if (this.items[i].name != null && this.items[i].name !== '') { if (itemNames === '') { itemNames += this.items[i].name } else { itemNames += ',' + this.items[i].name } } } alert(itemNames) } } } </script>
<style scoped> .dynamic-input{ width: 89.4%; float:left; margin-bottom: 5px; } .delete-input{ float: right } </style>
|
引入
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
| <template> <div class="hello"> <DynamicInput :items="items" :config="config"> </DynamicInput> </div> </template>
<script> import DynamicInput from './DynamicInput' export default { name: 'HelloWorld', components: {DynamicInput}, data () { return { items: [ { name: '孩子1' }, { name: '孩子2' }, { name: '' } ], config: { title: '孩子', showMsg: true } } } } </script>
<style scoped> </style>
|
此为博主副博客,留言请去主博客,转载请注明出处:https://www.baby7blog.com/myBlog/61.html