• angular中多层嵌套结构的表单如何处理回显问题


    最近在处理angular表单时,有一个4层结构的表单。而且很多元素时动态生成,如下:

    1. this.validateForm=this.fb.group({
    2. storeId: ["test12"],
    3. storeNameKey:[''],
    4. config:this.fb.group({
    5. tableSize:this.fb.group({
    6. toggle:[false],
    7. groupSize:this.fb.array([
    8. this.fb.group({
    9. cate:['indoor'],
    10. title_en:[''],
    11. title_zh:[''],
    12. tableSize:this.fb.array([
    13. this.fb.group({
    14. size: [""],
    15. desc_en:["small"],
    16. desc_zh: ["小桌"],
    17. number:['A02'],
    18. preTitle: ["C"],
    19. maxPerson: [8],
    20. minPerson: [5],
    21. alias: "S",
    22. }),
    23. ])
    24. }),
    25. this.fb.group({
    26. cate:['outdoor'],
    27. title_en:[''],
    28. title_zh:[''],
    29. tableSize:this.fb.array([
    30. this.fb.group({
    31. size: ["small"],
    32. desc_en:["Small"],
    33. desc_zh: ["小桌"],
    34. number:['A02'],
    35. preTitle: ["C"],
    36. maxPerson: [8],
    37. minPerson: [5],
    38. alias: "S",
    39. }),
    40. ])
    41. }),
    42. ]),
    43. }),
    44. hasMoreTableSize:['false'],
    45. geoFancing:this.fb.group({
    46. // isOpenGeo:['true'],
    47. range:[100]
    48. }),
    49. dynamicQRCode:this.fb.group({
    50. refreshEx:[2]
    51. }),
    52. isLogin:[false],
    53. isShowBlockList:[false]
    54. }),
    55. })

    其界面表现如下:

    而在编辑的状态时如何根据后端返回数据结构进行回显。angular中formbuilder对象提供了setValue和patchValue两个方法。这两个方法只对一层对象有效,对于多层嵌套的数据结构,无法生效。经过摸索,发现可以这种方式解决。

    首先模拟后端数据结构:

    1. const formdata={
    2. storeId:"disneycart",
    3. storeNameKey:"123",
    4. config:{
    5. tableSize:{
    6. toggle:true,
    7. groupSize:[
    8. {
    9. cate:"indoor",
    10. title_en:'',
    11. title_zh:'',
    12. tableSize:[
    13. {
    14. alias: "S",
    15. desc_en: "small",
    16. desc_zh: "小4桌",
    17. maxPerson: 4,
    18. minPerson: 1,
    19. number: "A01",
    20. preTitle: "A",
    21. size: "small"
    22. },
    23. {
    24. alias: "m",
    25. desc_en: "middl",
    26. desc_zh: "中桌",
    27. maxPerson: 6,
    28. minPerson: 4,
    29. number: "b01",
    30. preTitle: "B",
    31. size: "middle"
    32. },
    33. {
    34. alias: "L",
    35. desc_en: "large",
    36. desc_zh: "中桌",
    37. maxPerson: 6,
    38. minPerson: 4,
    39. number: "b01",
    40. preTitle: "c",
    41. size: "large"
    42. },
    43. ]
    44. },
    45. {
    46. cate:"outdoor",
    47. title_en:'',
    48. title_zh:'',
    49. tableSize:[
    50. {
    51. alias: "S",
    52. desc_en: "small",
    53. desc_zh: "小4桌",
    54. maxPerson: 4,
    55. minPerson: 1,
    56. number: "A01",
    57. preTitle: "A",
    58. size: "small"
    59. },
    60. {
    61. alias: "m",
    62. desc_en: "middl",
    63. desc_zh: "中桌",
    64. maxPerson: 6,
    65. minPerson: 4,
    66. number: "b01",
    67. preTitle: "B",
    68. size: "middle"
    69. }
    70. ]
    71. }
    72. ]
    73. },
    74. dynamicQRCode:{
    75. refreshEx:200
    76. },
    77. geoFancing:{
    78. range:200.,
    79. // isOpenGeo:false
    80. },
    81. hasMoreTableSize:true,
    82. isLogin:true,
    83. isShowBlockList:true
    84. }
    85. }

    我们在页面初始化的时候模拟把该数据返回给前端进行回显。

    1. ngAfterViewInit(){
    2. setTimeout(()=>{
    3. this.repatchForm(formdata)
    4. console.log("settimeout---后", this.validateForm)
    5. },2000)
    6. }
    1. repatchForm(responseData:any){
    2. let arr2=this.resetAndGetGroupSize(responseData)
    3. console.log("settimeout---前", this.validateForm)
    4. this.validateForm.patchValue({
    5. storeId: responseData.storeId,
    6. storeNameKey: responseData.storeNameKey,
    7. config: {
    8. tableSize: {
    9. toggle: responseData.config.tableSize.toggle,
    10. groupSize: arr2
    11. },
    12. hasMoreTableSize: responseData.config.hasMoreTableSize,
    13. geoFancing: {
    14. range: responseData.config.geoFancing.range
    15. },
    16. dynamicQRCode: {
    17. refreshEx: responseData.config.dynamicQRCode.refreshEx
    18. },
    19. isLogin:responseData.config.isLogin,
    20. isShowBlockList:responseData.config.isShowBlockList
    21. }
    22. });
    23. }

    注意此处是核心 ,我们新建一个新的formGroup对象,然后通过表单对象把原本里面的group干掉

    最后通过patchValue进行重新复制。 这里特别注意我们清空原本的groupSize对象,最后通过遍历新的后端数据生成新的formArray对象,最后把这个新的formArray对象通过patchValue的方式进行重新赋值即可生效。

    1. //处理会显时table列表数据
    2. resetAndGetGroupSize(resData:any){
    3. let arr=resData?.config?.tableSize?.groupSize.map((group: any) => {
    4. return this.fb.group({
    5. cate: group.cate,
    6. title_en: group.title_en,
    7. title_zh: group.title_zh,
    8. tableSize: this.fb.array(group.tableSize.map((table: any) => {
    9. return this.fb.group({
    10. size: table.size,
    11. desc_en: table.desc_en,
    12. desc_zh: table.desc_zh,
    13. number: table.number,
    14. preTitle: table.preTitle,
    15. maxPerson: table.maxPerson,
    16. minPerson: table.minPerson,
    17. alias: table.alias
    18. });
    19. }))
    20. })
    21. })
    22. this.groupSize.clear()
    23. arr.forEach((item:FormGroup)=>{
    24. this.groupSize.push(item)
    25. })
    26. let arr2=arr.map((item:FormGroup)=>{
    27. return item.value
    28. })
    29. return arr2
    30. }

     

     

  • 相关阅读:
    爱尔兰药品局药品信息数据查询
    【OpenCV】Chapter2.图像的数值运算
    【反射】Constructor类
    使用Supervisor进行监控进程并实现自动重启
    《计算机校招之路》1.0.0 震撼发布
    K-优字符串(冬季每日一题 11)
    【牛客编程题】零基础入门前端之73题(html,css,ES5,WebAPI)
    在 Ubuntu Server 上配置静态 IP 地址
    什么是二级域名?二级域名如何注册申请?
    B - Magical Subsequence (CCPC2021哈尔滨)
  • 原文地址:https://blog.csdn.net/baidu_41601048/article/details/132759131