ng-alain / ng-alain

NG-ZORRO admin panel front-end framework (surge mirror https://ng-alain-doc.surge.sh)
https://ng-alain.com
MIT License
4.53k stars 1.15k forks source link

使用rootProperty.setValue为时间组件赋值失败,显示为空 #831

Closed danxiaogui closed 5 years ago

danxiaogui commented 5 years ago

版本信息

重现地址 https://stackblitz.com/edit/ng-alain-setup-kuzan-datetime?file=src/app/app.component.ts

重现步骤

1 打开此处代码注释,schema加载成功,时间组件赋值成功

image image

2 打开此处代码注释,schema加载成功,使用rootProperty.setValue为时间组件赋值失败

image image

代码

import { Component,ViewChild ,OnInit} from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { STColumn } from '@delon/abc';
import { SFSchema ,SFComponent} from '@delon/form';

@Component({
  selector: 'my-app',
    template: `<sf #sf  [schema]="schema"  [formData]="formData"
    (formSubmit)="submit($event)"
    (formChange)="change($event)"></sf>`,
})
export class AppComponent  implements OnInit {

  @ViewChild('sf') sf: SFComponent;

  formData: any = {};
  schema: SFSchema = {
    properties:{},
    ui:{}
  };

  constructor(public msg: NzMessageService) { }

  ngOnInit(): void {

    let tempSchema : SFSchema = {
      properties: {
        datetime: {
          type: 'string',
          format: 'date-time',
        },
        date: {
          type: 'string',
          format: 'date',
        },
      },
      required:['date','datetime'],
      ui:{
        spanLabel:4
      }
    }

    let tempDate = {
      'datetime':'2018-12-19T18:27:20+08:00',
      'date':'2018-11-14',
    }

    /**
     * 刷新 Schema 和 formData 没有问题
     */
    //this.schema = tempSchema;
    //this.formData = tempDate;

    /**
     * refreshSchema 刷新Schema表单结构成功
     * rootProperty.setValue 为时间组件初始化值出错,时间组件显示为空
     */
    //this.sf.refreshSchema(tempSchema);
    //this.sf.rootProperty.setValue(tempDate,false);

  }

   submit(value: any) {
     this.msg.success(JSON.stringify(value));
  }

  change(value: any) {
    console.log('change', value);
  }
}
cipchk commented 5 years ago

https://ng-alain.com/form/getting-started/en#Methods

danxiaogui commented 5 years ago

根据路径设置值,输入框组件设置成功,时间组件设置失败

image image

danxiaogui commented 5 years ago

动态表单提供的操作接口中,可以设置值的是这个,但是对时间组件不起作用

image

cipchk commented 5 years ago

@danxiaogui, Pls prvoide a clean reproduce demo.

danxiaogui commented 5 years ago

OK,I am so sorry here is the clean reproduce demo https://stackblitz.com/edit/ng-alain-setup-kuzan-datetime?file=src/app/app.component.ts thanks.

danxiaogui commented 5 years ago

版本信息

重现地址

我的项目中更新 ng-alain 版本为 7.0.0-rc.1,但是对时间组件的赋值仍然有问题,
以下的代码可以为时间组件赋值,实际上设值成功,但是页面显示为空
this.sf.rootProperty['properties']['datetime'].setValue('2018-12-19T18:27:20+08:00', false);
需要添加 resetValue 页面才可以将时间显示出来
this.sf.rootProperty['properties']['datetime'].resetValue('2018-12-19T18:27:20+08:00', true);
this.sf.rootProperty['properties']['datetime'].setValue('2018-12-19T18:27:20+08:00', false);
cipchk commented 5 years ago

@danxiaogui Pls provide a reproduce demo in 7.0.0-rc.1

danxiaogui commented 5 years ago

版本信息

重现地址

package.json地址

image

现在的demo版本是 7.0.0-rc.1,时间组件赋值的问题依旧存在

image

danxiaogui commented 5 years ago

我找到一种替代方法,即使用 resetValue 赋值 复现地址

动态表单的时间组件可以使用 resetValue 进行赋值

let datas = {
  'datetime': '2018-12-19T18:27:20+08:00',
  'date': '2018-12-19'
}
this.sf.refreshSchema(tempSchema).rootProperty.resetValue(datas,false);

也可以对动态表单中单个时间组件进行赋值 resetValue (但是会抛异常)

 this.sf.refreshSchema(tempSchema);
 this.sf.rootProperty['properties']['datetime'].resetValue('2018-12-19T18:27:20+08:00', false);
 this.sf.rootProperty['properties']['date'].resetValue('2018-12-19', false);

image

从我本次测试的结果来说,setValue 方法对时间组件的赋值还是存在问题,实际上已经成功将值设置到时间组件的FormProperty中,但页面上却没有显示出来,或许是我测试有误

cipchk commented 5 years ago

事实上这一问题的根治还是只能靠 ng-zorro-antd 全面性支持 OnPush模式,不再对此问题进一步优化。