form.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <div class="form" >
  3. <by-tool :propConfig="toolConfig" @onAdd="onAdd" :customTools="customTools" />
  4. <by-form :propConfig="config" ref="form">
  5. <template v-slot:slotField='{ value }'>这是插槽:{{ value.name }}</template>
  6. </by-form>
  7. <el-button type="primary" @click="getFormData">获取数据</el-button>
  8. <el-button type="primary" @click="getFormVali">获取表单验证</el-button>
  9. </div>
  10. </template>
  11. <script lang="ts">
  12. import { Component, Prop, Vue, Watch } from "vue-property-decorator";
  13. import a from './a.vue'
  14. @Component
  15. export default class DemoForm extends Vue {
  16. config={
  17. attr:{
  18. size:'small',
  19. height:500,
  20. rules:{
  21. name:[{
  22. required: true, message: '请输入名称', trigger: 'blur'
  23. }]
  24. }
  25. },
  26. columns:[
  27. [{
  28. span:6,
  29. label:'名称',
  30. prop:'name',
  31. component:'by-input',
  32. compConfig:{
  33. attr:{
  34. placeholder:'请输入名称',
  35. clearable:true
  36. }
  37. }
  38. },{
  39. span:6,
  40. label:'名称2',
  41. prop:'name2',
  42. component:'by-input',
  43. compConfig:{
  44. attr:{
  45. placeholder:'请输入名称',
  46. clearable:true
  47. }
  48. }
  49. },{
  50. label:'测试',
  51. prop:'name3',
  52. // required:true,
  53. component:a
  54. }],
  55. [{
  56. span:6,
  57. label:'下拉框',
  58. prop:'select',
  59. rules:[
  60. { required: true, message: '请选择数据', trigger: 'change' }
  61. ],
  62. component:'by-select',
  63. compConfig:{
  64. attr:{
  65. placeholder:'请选择数据',
  66. clearable:true,
  67. data:[{
  68. value: '选项1',
  69. label: '黄金糕'
  70. }, {
  71. value: '选项2',
  72. label: '双皮奶'
  73. }, {
  74. value: '选项3',
  75. label: '蚵仔煎'
  76. }, {
  77. value: '选项4',
  78. label: '龙须面'
  79. }, {
  80. value: '选项5',
  81. label: '北京烤鸭'
  82. }]
  83. }
  84. }
  85. },{
  86. span:6,
  87. label:'时间',
  88. prop:'date',
  89. rules:[
  90. { required: true, message: '请选择时间', trigger: 'change' }
  91. ],
  92. component:'by-date-picker',
  93. compConfig:{
  94. format:'yyyy-MM-dd',
  95. type:'date'
  96. }
  97. },{
  98. span:6,
  99. label:'插槽',
  100. slot:true,
  101. prop:'slotField',
  102. }]
  103. ]
  104. }
  105. toolConfig={
  106. tools:{
  107. onAdd:true,
  108. onUpdate:true,
  109. onDelete:true,
  110. onExport:true,
  111. onRefresh:true
  112. }
  113. }
  114. customTools:Array<any>=[
  115. {
  116. name: '新增123', icon: 'el-icon-plus', event:{
  117. click:()=>{
  118. console.log('新增123')
  119. }
  120. }
  121. }
  122. ]
  123. onAdd(){
  124. console.log('点击新增')
  125. }
  126. getFormData(){
  127. let data = (this as any).$refs.form.getValue();
  128. console.log('表单数据:',data);
  129. }
  130. getFormVali(){
  131. (this as any).$refs.form.validate().then(()=>{
  132. console.log('验证通过')
  133. }).catch(()=>{
  134. console.log('验证不通过')
  135. });
  136. }
  137. }
  138. </script>
  139. <style lang="scss" scoped>
  140. .form{
  141. width: 100%;
  142. }
  143. </style>