From 280e61c785bba5b254969d531c883f5a439b9103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TINANT=20Herv=C3=A9?= Date: Wed, 19 Jul 2017 14:32:24 +0200 Subject: [PATCH] fix: initial select is not displayed when list of items contains string value --- src/demo/app/app.component.html | 1 + src/demo/app/app.module.ts | 4 +- src/demo/app/app.routes.ts | 3 + .../components/simple-value.component.html | 84 +++++++++++++++++++ .../components/simple-value.component.scss | 0 .../app/components/simple-value.component.ts | 34 ++++++++ src/lib/src/service/select.service.ts | 21 +++-- 7 files changed, 140 insertions(+), 7 deletions(-) create mode 100644 src/demo/app/components/simple-value.component.html create mode 100644 src/demo/app/components/simple-value.component.scss create mode 100644 src/demo/app/components/simple-value.component.ts diff --git a/src/demo/app/app.component.html b/src/demo/app/app.component.html index 5db7c14..dd0c31e 100644 --- a/src/demo/app/app.component.html +++ b/src/demo/app/app.component.html @@ -16,6 +16,7 @@ diff --git a/src/demo/app/app.module.ts b/src/demo/app/app.module.ts index 9b88edc..c587211 100644 --- a/src/demo/app/app.module.ts +++ b/src/demo/app/app.module.ts @@ -9,6 +9,7 @@ import { AppComponent } from './app.component'; import { FlatComponent } from './components/flat.component'; import { HierarchicalComponent } from './components/hierarchical.component'; import { AppRoutes } from './app.routes'; +import { SimpleValueComponent } from './components/simple-value.component'; @NgModule({ imports: [ @@ -21,7 +22,8 @@ import { AppRoutes } from './app.routes'; declarations: [ AppComponent, FlatComponent, - HierarchicalComponent + HierarchicalComponent, + SimpleValueComponent ], bootstrap: [AppComponent] }) diff --git a/src/demo/app/app.routes.ts b/src/demo/app/app.routes.ts index dd248e2..b9eb2a6 100644 --- a/src/demo/app/app.routes.ts +++ b/src/demo/app/app.routes.ts @@ -1,8 +1,11 @@ import { FlatComponent } from './components/flat.component'; import { HierarchicalComponent } from './components/hierarchical.component'; +import { SimpleValueComponent } from "./components/simple-value.component"; export const AppRoutes = [ { path: '', redirectTo: '/flat', pathMatch: 'full' }, { path: 'flat', component: FlatComponent }, { path: 'hierarchical', component: HierarchicalComponent }, + { path: 'simplevalue', component: SimpleValueComponent }, + ]; diff --git a/src/demo/app/components/simple-value.component.html b/src/demo/app/components/simple-value.component.html new file mode 100644 index 0000000..6887d0b --- /dev/null +++ b/src/demo/app/components/simple-value.component.html @@ -0,0 +1,84 @@ +
+ +
+
Simple select
+
+
+ +
+
Simple select is required
+
+
+
+ {{simpleSelected}} +
+
+
+
+
Multiple select
+
+
+ + +
+
Multiple select is required
+
You must choose at least 2 items on Multiple select
+
You must choose maximum 4 items on Multiple select
+
+
+
+
  • {{itm}}
  • +
    +
    +
    +
    +
    Options
    +
    +
    +
    + + +
    +
    + + +
    +
    + +
    +
    + +
    +
    +
    +
    +
    diff --git a/src/demo/app/components/simple-value.component.scss b/src/demo/app/components/simple-value.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/src/demo/app/components/simple-value.component.ts b/src/demo/app/components/simple-value.component.ts new file mode 100644 index 0000000..d34359a --- /dev/null +++ b/src/demo/app/components/simple-value.component.ts @@ -0,0 +1,34 @@ +import { Component } from '@angular/core'; +import { FlatCountries } from '../../../datas'; + +@Component({ + selector: 'simple-value-sample', + templateUrl: './simple-value.component.html' +}) +export class SimpleValueComponent { + public items = [ + 'Jacques', + 'Jad', + 'Jana', + 'Jasmine', + 'Jeremie', + 'Jeremy', + 'Joachim', + 'Johan', + 'Johanna', + 'Jonathan', + 'Jordan', + 'Joseph', + 'Jules', + 'Justin' + ]; + + public ShowFilter = true; + public Disabled = false; + public FilterPlaceholder = 'Type here to filter elements...'; + public MaxDisplayed = 5; + + + public simpleSelected = 'Jeremy'; + public multipleSelected = ['Jeremy', 'Jordan']; +} diff --git a/src/lib/src/service/select.service.ts b/src/lib/src/service/select.service.ts index 9a90988..714a8ad 100644 --- a/src/lib/src/service/select.service.ts +++ b/src/lib/src/service/select.service.ts @@ -214,14 +214,23 @@ export class SelectService { private getItemForModel(value: any, array: SelectableItem[]): SelectableItem[] { let result: SelectableItem[] = []; for (let v of array) { - if (value && value[this.Configuration.idProperty]) { - if (v.id === (value[this.Configuration.idProperty] || '').toString()) { - result.push(v); + if (value) { + if (typeof value !== 'object') { + if (v.data === value) { + result.push(v); + } + } else { + if (value[this.Configuration.idProperty]) { + if (v.id === (value[this.Configuration.idProperty] || '').toString()) { + result.push(v); + } + } + if (this.Configuration.isHierarchy() && v.children && v.children.length > 0) { + result = [...result, ...this.getItemForModel(value, v.children)]; + } } } - if (this.Configuration.isHierarchy() && v.children && v.children.length > 0) { - result = [...result, ...this.getItemForModel(value, v.children)]; - } + }; return result; }