Skip to content

Commit 7fca04e

Browse files
committed
Fix problem with specified keys.
1 parent dc718ca commit 7fca04e

File tree

3 files changed

+49
-45
lines changed

3 files changed

+49
-45
lines changed

app/demo-app.component.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { DualListComponent } from './dual-list.component';
77
template: `
88
<div class="container-fluid">
99
<p></p>
10-
<dual-list [sort]="keepSorted" [source]="stations" [(destination)]="confirmed" height="265px"></dual-list>
10+
<dual-list [sort]="keepSorted" [source]="stations" key="key" display="station" [(destination)]="confirmed" height="265px"></dual-list>
1111
<div style="margin-top:32px;"><h4>Confirmed</h4><pre>{{confirmed|json}}</pre></div>
1212
</div>
1313
`
@@ -18,42 +18,42 @@ export class DemoAppComponent {
1818
private keepSorted:boolean = true;
1919

2020
private stations:Array<any> = [
21-
{ _id: 1, _name: 'Antonito' },
22-
{ _id: 2, _name: 'Big Horn' },
23-
{ _id: 3, _name: 'Sublette' },
24-
{ _id: 4, _name: 'Toltec' },
25-
{ _id: 5, _name: 'Osier' },
26-
{ _id: 6, _name: 'Chama' },
27-
{ _id: 7, _name: 'Monero' },
28-
{ _id: 8, _name: 'Lumberton' },
29-
{ _id: 9, _name: 'Duice' },
30-
{ _id: 10, _name: 'Navajo' },
31-
{ _id: 11, _name: 'Juanita' },
32-
{ _id: 12, _name: 'Pagosa Jct' },
33-
{ _id: 13, _name: 'Carracha' },
34-
{ _id: 14, _name: 'Arboles' },
35-
{ _id: 15, _name: 'Allison' },
36-
{ _id: 16, _name: 'Tiffany' },
37-
{ _id: 17, _name: 'La Boca' },
38-
{ _id: 18, _name: 'Ignacio' },
39-
{ _id: 19, _name: 'Oxford' },
40-
{ _id: 20, _name: 'Florida' },
41-
{ _id: 21, _name: 'Bocea' },
42-
{ _id: 22, _name: 'Carbon Jct' },
43-
{ _id: 23, _name: 'Durango' },
44-
{ _id: 24, _name: 'Home Ranch' },
45-
{ _id: 25, _name: 'Trimble' },
46-
{ _id: 26, _name: 'Hermosa' },
47-
{ _id: 27, _name: 'Rockwood' },
48-
{ _id: 28, _name: 'Tacoma' },
49-
{ _id: 29, _name: 'Needleton' },
50-
{ _id: 30, _name: 'Elk Park' },
51-
{ _id: 31, _name: 'Silverton' },
52-
{ _id: 32, _name: 'Eureka' }
21+
{ key: 1, station: 'Antonito', state: 'CO' },
22+
{ key: 2, station: 'Big Horn', state: 'NM' },
23+
{ key: 3, station: 'Sublette', state: 'NM' },
24+
{ key: 4, station: 'Toltec', state: 'NM' },
25+
{ key: 5, station: 'Osier', state: 'CO' },
26+
{ key: 6, station: 'Chama', state: 'NM'},
27+
{ key: 7, station: 'Monero', state: 'NM' },
28+
{ key: 8, station: 'Lumberton', state: 'NM' },
29+
{ key: 9, station: 'Duice', state: 'NM' },
30+
{ key: 10, station: 'Navajo', state: 'NM' },
31+
{ key: 11, station: 'Juanita', state: 'CO' },
32+
{ key: 12, station: 'Pagosa Jct', state: 'CO' },
33+
{ key: 13, station: 'Carracha', state: 'CO' },
34+
{ key: 14, station: 'Arboles', state: 'CO' },
35+
{ key: 15, station: 'Solidad', state: 'CO' },
36+
{ key: 16, station: 'Tiffany', state: 'CO' },
37+
{ key: 17, station: 'La Boca', state: 'CO' },
38+
{ key: 18, station: 'Ignacio', state: 'CO' },
39+
{ key: 19, station: 'Oxford', state: 'CO' },
40+
{ key: 20, station: 'Florida', state: 'CO' },
41+
{ key: 21, station: 'Bocea', state: 'CO' },
42+
{ key: 22, station: 'Carbon Jct', state: 'CO' },
43+
{ key: 23, station: 'Durango', state: 'CO' },
44+
{ key: 24, station: 'Home Ranch', state: 'CO' },
45+
{ key: 25, station: 'Trimble Springs', state: 'CO' },
46+
{ key: 26, station: 'Hermosa', state: 'CO' },
47+
{ key: 27, station: 'Rockwood', state: 'CO' },
48+
{ key: 28, station: 'Tacoma', state: 'CO' },
49+
{ key: 29, station: 'Needleton', state: 'CO' },
50+
{ key: 30, station: 'Elk Park', state: 'CO' },
51+
{ key: 31, station: 'Silverton', state: 'CO' },
52+
{ key: 32, station: 'Eureka', state: 'CO' }
5353
];
5454

5555
private confirmed:Array<any> = [
56-
{ _id: 32, _name: 'Eureka' }
56+
{ key: 32, station: 'Eureka', state: 'CO' }
5757
];
5858

5959
}

app/dual-list.component.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class DualListComponent implements DoCheck, OnChanges {
134134
let sourceChanges = this.sourceDiffer.diff(this.source);
135135
if (sourceChanges) {
136136
sourceChanges.forEachRemovedItem((r:any) => {
137-
let idx = this.findItemIndex(this.available.list, r.item);
137+
let idx = this.findItemIndex(this.available.list, r.item, this.key);
138138
if (idx !== -1) {
139139
this.available.list.splice(idx, 1);
140140
}
@@ -143,7 +143,7 @@ export class DualListComponent implements DoCheck, OnChanges {
143143

144144
sourceChanges.forEachAddedItem((r:any) => {
145145
// Do not add duplicates even if source has duplicates.
146-
if (this.findItemIndex(this.available.list, r.item) === -1) {
146+
if (this.findItemIndex(this.available.list, r.item, this.key) === -1) {
147147
this.available.list.push( { _id: r.item[this.key], _name: this.makeName(r.item) });
148148
}
149149
}
@@ -157,7 +157,7 @@ export class DualListComponent implements DoCheck, OnChanges {
157157
let destChanges = this.destinationDiffer.diff(this.destination);
158158
if (destChanges) {
159159
destChanges.forEachRemovedItem((r:any) => {
160-
let idx = this.findItemIndex(this.confirmed.list, r.item);
160+
let idx = this.findItemIndex(this.confirmed.list, r.item, this.key);
161161
if (idx !== -1) {
162162
if (!this.isItemSelected(this.confirmed.pick, this.confirmed.list[idx])) {
163163
this.selectItem(this.confirmed.pick, this.confirmed.list[idx]);
@@ -168,7 +168,7 @@ export class DualListComponent implements DoCheck, OnChanges {
168168
);
169169

170170
destChanges.forEachAddedItem((r:any) => {
171-
let idx = this.findItemIndex(this.available.list, r.item);
171+
let idx = this.findItemIndex(this.available.list, r.item, this.key);
172172
if (idx !== -1) {
173173
if (!this.isItemSelected(this.available.pick, this.available.list[idx])) {
174174
this.selectItem(this.available.pick, this.available.list[idx]);
@@ -290,12 +290,12 @@ export class DualListComponent implements DoCheck, OnChanges {
290290
}
291291
}
292292

293-
findItemIndex(list:Array<any>, item:any) {
293+
findItemIndex(list:Array<any>, item:any, key:any = '_id') {
294294
let idx = -1;
295295

296296
// Assumption is that the arrays do not have duplicates.
297297
list.filter( (e:any) => {
298-
if (e._id === item[this.key]) {
298+
if (e._id === item[key]) {
299299
idx = list.indexOf(e);
300300
return true;
301301
}
@@ -324,18 +324,20 @@ export class DualListComponent implements DoCheck, OnChanges {
324324
}
325325
} else {
326326
mv = source.list.filter( src => {
327-
return (src[this.key] === source.pick[i][this.key]);
327+
return (src._id === source.pick[i]._id);
328328
});
329329
}
330330

331331
// Should only ever be 1
332332
if (mv.length === 1) {
333333
// Move if item wasn't already moved by drag-and-drop.
334-
if (item && item[this.key] === mv[0][this.key]) {
334+
// if (item && item[this.key] === mv[0][this.key]) {
335+
if (item && item._id === mv[0]._id) {
335336
target.list.push( mv[0] );
336337
} else {
337338
// see if it is already in target?
338-
if ( target.list.filter( trg => { return trg[this.key] === mv[0][this.key]; }).length === 0) {
339+
// if ( target.list.filter( trg => { return trg[this.key] === mv[0][this.key]; }).length === 0) {
340+
if ( target.list.filter( trg => { return trg._id === mv[0]._id; }).length === 0) {
339341
target.list.push( mv[0] );
340342
}
341343
}
@@ -383,7 +385,9 @@ export class DualListComponent implements DoCheck, OnChanges {
383385
}
384386

385387
selectItem(list:Array<any>, item:any) {
386-
let pk = list.filter( (e:any) => { return Object.is(e, item); });
388+
let pk = list.filter( (e:any) => {
389+
return Object.is(e, item);
390+
});
387391
if (pk.length > 0) {
388392
// Already in list, so deselect.
389393
for (let i = 0, len = pk.length; i < len; i += 1) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular2-dual-listbox",
33
"description": "Angular 2 component for a dual listbox control.",
4-
"version": "2.0.0",
4+
"version": "2.1.0",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/czeckd/angular2-dual-listbox.git"

0 commit comments

Comments
 (0)