From 3d52c10d704d161058e353b6a4265464cbabf4c5 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Wed, 16 Dec 2020 09:20:28 +0000
Subject: [PATCH] FEAT #14810 TIME 0:30 fix select search in opt

(cherry picked from commit ced2ebb7fcfdeecda31dc7c14e79b15f5e319410)
---
 .../select-search/select-search.component.ts  | 44 ++++++++-----------
 1 file changed, 19 insertions(+), 25 deletions(-)

diff --git a/src/frontend/plugins/select-search/select-search.component.ts b/src/frontend/plugins/select-search/select-search.component.ts
index 9bdc61e4084..a62e3a8fa2b 100755
--- a/src/frontend/plugins/select-search/select-search.component.ts
+++ b/src/frontend/plugins/select-search/select-search.component.ts
@@ -177,7 +177,7 @@ export class PluginSelectSearchComponent implements OnInit, OnDestroy, AfterView
         let group = '';
         let index = 1;
         this.datasClone = JSON.parse(JSON.stringify(this.datas));
-        this.datasClone.forEach((element: any) => {
+        this.datasClone.forEach((element: any, index: number) => {
             if (element.isTitle) {
                 group = `group_${index}`;
                 element.id = group;
@@ -198,30 +198,24 @@ export class PluginSelectSearchComponent implements OnInit, OnDestroy, AfterView
 
     // To resfresh filteredDatas if data is modfied
     ngDoCheck(): void {
-        if (JSON.stringify(this.datas) !== JSON.stringify(this.datasClone) && !this.isModelModified) {
-            this.isModelModified = true;
-
+        if (this.datasClone.length !== this.datas.length || this.datasClone.length === 0 && this.isModelModified) {
             this.datasClone = JSON.parse(JSON.stringify(this.datas));
-
-            // if more data => contruct title again
-            if (this.datasClone.length !== this.datas.length) {
-                let group = '';
-                let index = 1;
-                this.datasClone.forEach((element: any) => {
-                    if (element.isTitle) {
-                        group = `group_${index}`;
-                        element.id = group;
-                        index++;
-                    } else {
-                        element.group = group;
-                    }
-                });
-            }
+            let group = '';
+            let index = 1;
+            this.datasClone.forEach((element: any) => {
+                if (element.isTitle) {
+                    group = `group_${index}`;
+                    element.id = group;
+                    index++;
+                } else {
+                    element.group = group;
+                }
+            });
             this.filteredDatas = this.formControlSearch.valueChanges
-                .pipe(
-                    startWith(''),
-                    map(value => this._filter(value))
-                );
+            .pipe(
+                startWith(''),
+                map(value => this._filter(value))
+            );
             this.isModelModified = false;
         }
     }
@@ -400,9 +394,9 @@ export class PluginSelectSearchComponent implements OnInit, OnDestroy, AfterView
         } else if (typeof value === 'string' && value !== '') {
             const filterValue = this.latinisePipe.transform(value.toLowerCase());
 
-            const group = this.datas.filter((option: any) => option['isTitle'] && this.latinisePipe.transform(option['label'].toLowerCase()).includes(filterValue)).map((opt: any) => opt.id);
+            const group = this.datasClone.filter((option: any) => option['isTitle'] && this.latinisePipe.transform(option['label'].toLowerCase()).includes(filterValue)).map((opt: any) => opt.id);
 
-            return this.datas.filter((option: any) => (option['isTitle'] && group.indexOf(option['id']) > -1) || (group.indexOf(option['group']) > -1 || this.latinisePipe.transform(option['label'].toLowerCase()).includes(filterValue)));
+            return this.datasClone.filter((option: any) => (option['isTitle'] && group.indexOf(option['id']) > -1) || (group.indexOf(option['group']) > -1 || this.latinisePipe.transform(option['label'].toLowerCase()).includes(filterValue)));
         } else {
             return this.datas;
         }
-- 
GitLab