Update component rules

Form component

::: demo

  1. <template>
  2. <div>
  3. <form-create :rule="rule" v-model="fApi" :option="options"/>
  4. <h2 v-text="disabled? 'cancel' : 'disable'"></h2>
  5. <ElButton @click="disabled1">Way1</ElButton>
  6. <ElButton @click="disabled2">Way2</ElButton>
  7. <ElButton @click="disabled3">Way3</ElButton>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. data(){
  13. return {
  14. disabled:false,
  15. fApi:{},
  16. options:{
  17. onSubmit:(formData)=>{
  18. alert(JSON.stringify(formData));
  19. },
  20. submitBtn:false,
  21. resetBtn:false
  22. },
  23. rule:[
  24. {
  25. type:'input',
  26. field:'input',
  27. title:'product name',
  28. value: '',
  29. props:{
  30. disabled:false
  31. }
  32. }
  33. ]
  34. }
  35. },
  36. computed:{
  37. title(){
  38. return this.disabled ? 'Product name (disabled)' : 'product name';
  39. }
  40. },
  41. methods:{
  42. swatch(){
  43. this.disabled = !this.disabled;
  44. },
  45. disabled1(){
  46. this.swatch();
  47. this.fApi.disabled(this.disabled,'input');
  48. this.rule[0].title = this.title;
  49. },
  50. disabled2(){
  51. this.swatch();
  52. this.fApi.updateRule('input',{
  53. title: this.title,
  54. props:{
  55. disabled: this.disabled
  56. }
  57. })
  58. },
  59. disabled3(){
  60. this.swatch();
  61. const rule = this.fApi.getRule('input');
  62. rule.title = this.title;
  63. rule.props.disabled = this.disabled;
  64. },
  65. }
  66. }
  67. </script>

:::

Modify the options of a selection component

::: demo

  1. <template>
  2. <div>
  3. <form-create :rule="rule" v-model="fApi" :option="options"/>
  4. <h2>Operate</h2>
  5. <p>Increase option of select component</p>
  6. <ElButton @click="add1">Way1</ElButton>
  7. <ElButton @click="add2">Way2</ElButton>
  8. <ElButton @click="reset1">reset 1</ElButton>
  9. <ElButton @click="reset2">reset 1</ElButton>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. data(){
  15. return {
  16. i: 0,
  17. fApi:{},
  18. options:{
  19. onSubmit:(formData)=>{
  20. alert(JSON.stringify(formData));
  21. },
  22. submitBtn:false,
  23. resetBtn:false
  24. },
  25. rule:[
  26. {
  27. type:'select',
  28. field:'select',
  29. title:'select',
  30. value: '',
  31. options: [{value:0, label:'0'.repeat(5)}]
  32. }
  33. ]
  34. }
  35. },
  36. methods:{
  37. getOption(){
  38. this.i++;
  39. return {value:this.i, label:String(this.i).repeat(5)}
  40. },
  41. add1(){
  42. this.rule[0].options.push(this.getOption());
  43. },
  44. add2(){
  45. this.fApi.getRule('select').options.push(this.getOption());
  46. },
  47. reset1(){
  48. this.fApi.updateRule('select', {
  49. options: [this.getOption()]
  50. }, true);
  51. },
  52. reset2(){
  53. this.rule[0].options = [this.getOption()];
  54. }
  55. }
  56. }
  57. </script>

:::

Modify the value of a form component

::: demo

  1. <template>
  2. <div>
  3. <form-create :rule="rule" v-model="fApi" :option="options"/>
  4. <h2>Operate</h2>
  5. <ElButton @click="changeValue1">Way1</ElButton>
  6. <ElButton @click="changeValue2">Way2</ElButton>
  7. <ElButton @click="changeValue3">Way3</ElButton>
  8. <ElButton @click="changeValue4">Way4</ElButton>
  9. </div>
  10. </template>
  11. <script>
  12. export default {
  13. data(){
  14. return {
  15. fApi:{},
  16. options:{
  17. onSubmit:(formData)=>{
  18. alert(JSON.stringify(formData));
  19. },
  20. submitBtn:false,
  21. resetBtn:false
  22. },
  23. rule:[
  24. {
  25. type:'inputNumber',
  26. field:'number',
  27. title:'stock',
  28. value: 0
  29. }
  30. ]
  31. }
  32. },
  33. methods:{
  34. changeValue1(){
  35. this.rule[0].value++;
  36. },
  37. changeValue2(){
  38. this.fApi.setValue('number', this.fApi.getValue('number') + 1);
  39. },
  40. changeValue3(){
  41. this.fApi.updateRule('number', {
  42. value: this.rule[0].value + 1
  43. });
  44. },
  45. changeValue4(){
  46. this.fApi.setValue({
  47. number: this.rule[0].value + 1
  48. });
  49. }
  50. }
  51. }
  52. </script>

:::

Custom component

After defining the custom component name, you can get the component rules through the $ f.component () method

自定义表单组件

::: demo

  1. <template>
  2. <div>
  3. <form-create :rule="rule" v-model="fApi" :option="options"/>
  4. <h2>Operate</h2>
  5. <p>Modify the type property of the button component</p>
  6. <ElButton @click="changeType1">Way 1</ElButton>
  7. <ElButton @click="changeType2">Way 2</ElButton>
  8. <ElButton @click="changeType3">Way 3</ElButton>
  9. </div>
  10. </template>
  11. <script>
  12. export default {
  13. data(){
  14. return {
  15. disabled:false,
  16. fApi:{},
  17. options:{
  18. onSubmit:(formData)=>{
  19. alert(JSON.stringify(formData));
  20. },
  21. submitBtn:false,
  22. resetBtn:false
  23. },
  24. rule:[
  25. {
  26. type:'el-button',
  27. name:'btn',
  28. props:{
  29. type:'success'
  30. },
  31. children:['Custom content']
  32. }
  33. ]
  34. }
  35. },
  36. computed:{
  37. type(){
  38. return this.disabled ? 'danger' : 'success';
  39. }
  40. },
  41. methods:{
  42. swatch(){
  43. this.disabled = !this.disabled;
  44. },
  45. changeType1(){
  46. this.swatch();
  47. this.rule[0].props.type = this.type;
  48. },
  49. changeType2(){
  50. this.swatch();
  51. this.fApi.updateRule('btn',{
  52. props:{
  53. type: this.type
  54. }
  55. })
  56. },
  57. changeType3(){
  58. this.swatch();
  59. const rule = this.fApi.component().btn;
  60. rule.props.type = this.type;
  61. },
  62. }
  63. }
  64. </script>

:::