| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent feaf140 commit 9e2e8ec
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,37 @@ | |||
| 1 | + import { EventData } from "tns-core-modules/data/observable"; | ||
| 2 | + import { Page } from "tns-core-modules/ui/page"; | ||
| 3 | + import { Observable } from "tns-core-modules/data/observable"; | ||
| 4 | + | ||
| 5 | + export class ListPickerJsonArrayModel extends Observable { | ||
| 6 | + public items = [ | ||
| 7 | + { id: 1, name: "Ter Stegen", role: "Goalkeeper" }, | ||
| 8 | + { id: 3, name: "Piqué", role: "Defender" }, | ||
| 9 | + { id: 4, name: "I. Rakitic", role: "Midfielder" }, | ||
| 10 | + { id: 5, name: "Sergio", role: "Midfielder" }, | ||
| 11 | + { id: 6, name: "Denis Suárez", role: "Midfielder" }, | ||
| 12 | + { id: 7, name: "Arda", role: "Midfielder" }, | ||
| 13 | + { id: 8, name: "A. Iniesta", role: "Midfielder" }, | ||
| 14 | + { id: 9, name: "Suárez", role: "Forward" }, | ||
| 15 | + { id: 10, name: "Messi", role: "Forward" }, | ||
| 16 | + { id: 11, name: "Neymar", role: "Forward" }, | ||
| 17 | + { id: 12, name: "Rafinha", role: "Midfielder" }, | ||
| 18 | + { id: 13, name: "Cillessen", role: "Goalkeeper" }, | ||
| 19 | + { id: 14, name: "Mascherano", role: "Defender" }, | ||
| 20 | + { id: 17, name: "Paco Alcácer", role: "Forward" }, | ||
| 21 | + { id: 18, name: "Jordi Alba", role: "Defender" }, | ||
| 22 | + { id: 19, name: "Digne", role: "Defender" }, | ||
| 23 | + { id: 20, name: "Sergi Roberto", role: "Midfielder" }, | ||
| 24 | + { id: 21, name: "André Gomes", role: "Midfielder" }, | ||
| 25 | + { id: 22, name: "Aleix Vidal", role: "Midfielder" }, | ||
| 26 | + { id: 23, name: "Umtiti", role: "Defender" }, | ||
| 27 | + { id: 24, name: "Mathieu", role: "Defender" }, | ||
| 28 | + { id: 25, name: "Masip", role: "Goalkeeper" }, | ||
| 29 | + ]; | ||
| 30 | + | ||
| 31 | + public selectedItem = ""; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + export function navigatingTo(args: EventData) { | ||
| 35 | + let page = <Page>args.object; | ||
| 36 | + page.bindingContext = new ListPickerJsonArrayModel(); | ||
| 37 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,11 @@ | |||
| 1 | + <Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page"> | ||
| 2 | + <StackLayout class="p-20"> | ||
| 3 | + <ListPicker items="{{ items }}" | ||
| 4 | + textField="name" | ||
| 5 | + valueField="role" | ||
| 6 | + selectedIndex="2" | ||
| 7 | + selectedValue="{{ selectedItem }}"> | ||
| 8 | + </ListPicker> | ||
| 9 | + <Label text="{{ selectedItem }}" textWrap="true" /> | ||
| 10 | + </StackLayout> | ||
| 11 | + </Page> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,5 +13,6 @@ export function loadExamples() { | |||
| 13 | 13 | const examples = new Map<string, string>(); | |
| 14 | 14 | examples.set("issue_2895", "list-picker/issue_2895"); | |
| 15 | 15 | examples.set("list-picker", "list-picker/list-picker"); | |
| 16 | + examples.set("list-picker-json-array", "list-picker/list-picker-json-array"); | ||
| 16 | 17 | return examples; | |
| 17 | 18 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,9 @@ export class ListPickerBase extends View implements ListPickerDefinition { | |||
| 9 | 9 | public selectedIndex: number; | |
| 10 | 10 | public items: any[] | ItemsSource; | |
| 11 | 11 | public isItemsSource: boolean; | |
| 12 | + public textField: string; | ||
| 13 | + public valueField: string; | ||
| 14 | + public selectedValue: any; | ||
| 12 | 15 | ||
| 13 | 16 | public _getItemAsString(index: number): any { | |
| 14 | 17 | let items = this.items; | |
@@ -17,7 +20,25 @@ export class ListPickerBase extends View implements ListPickerDefinition { | |||
| 17 | 20 | } | |
| 18 | 21 | ||
| 19 | 22 | let item = this.isItemsSource ? (<ItemsSource>this.items).getItem(index) : this.items[index]; | |
| 20 | - return (item === undefined || item === null) ? index + "" : item + ""; | ||
| 23 | + | ||
| 24 | + return (item === undefined || item === null) ? index + "" : this.parseItem(item); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + private parseItem(item) { | ||
| 28 | + return this.textField ? item[this.textField] + "" : item + ""; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + public updateSelectedValue(index) { | ||
| 32 | + var newVal = null; | ||
| 33 | + if (index >= 0) { | ||
| 34 | + const item = this.items[index]; | ||
| 35 | + | ||
| 36 | + newVal = this.valueField ? item[this.valueField] : item; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + if (this.selectedValue !== newVal) { | ||
| 40 | + this.set("selectedValue", newVal); | ||
| 41 | + } | ||
| 21 | 42 | } | |
| 22 | 43 | } | |
| 23 | 44 | ||
@@ -40,6 +61,8 @@ export const selectedIndexProperty = new CoercibleProperty<ListPickerBase, numbe | |||
| 40 | 61 | value = -1; | |
| 41 | 62 | } | |
| 42 | 63 | ||
| 64 | + target.updateSelectedValue(value); | ||
| 65 | + | ||
| 43 | 66 | return value; | |
| 44 | 67 | } | |
| 45 | 68 | }); | |
@@ -52,3 +75,21 @@ export const itemsProperty = new Property<ListPickerBase, any[] | ItemsSource>({ | |||
| 52 | 75 | } | |
| 53 | 76 | }); | |
| 54 | 77 | itemsProperty.register(ListPickerBase); | |
| 78 | + | ||
| 79 | + export const textFieldProperty = new Property<ListPickerBase, string>({ | ||
| 80 | + name: "textField", | ||
| 81 | + defaultValue: "" | ||
| 82 | + }); | ||
| 83 | + textFieldProperty.register(ListPickerBase); | ||
| 84 | + | ||
| 85 | + export const valueFieldProperty = new Property<ListPickerBase, string>({ | ||
| 86 | + name: "valueField", | ||
| 87 | + defaultValue: "" | ||
| 88 | + }); | ||
| 89 | + valueFieldProperty.register(ListPickerBase); | ||
| 90 | + | ||
| 91 | + export const selectedValueProperty = new Property<ListPickerBase, string>({ | ||
| 92 | + name: "selectedValue", | ||
| 93 | + defaultValue: null | ||
| 94 | + }); | ||
| 95 | + selectedValueProperty.register(ListPickerBase); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -40,6 +40,7 @@ function initializeNativeClasses(): void { | |||
| 40 | 40 | ||
| 41 | 41 | onValueChange(picker: android.widget.NumberPicker, oldValue: number, newValue: number): void { | |
| 42 | 42 | selectedIndexProperty.nativeValueChange(this.owner, newValue); | |
| 43 | + this.owner.updateSelectedValue(newValue); | ||
| 43 | 44 | } | |
| 44 | 45 | } | |
| 45 | 46 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -111,6 +111,7 @@ class ListPickerDelegateImpl extends NSObject implements UIPickerViewDelegate { | |||
| 111 | 111 | let owner = this._owner.get(); | |
| 112 | 112 | if (owner) { | |
| 113 | 113 | selectedIndexProperty.nativeValueChange(owner, row); | |
| 114 | + owner.updateSelectedValue(row); | ||
| 114 | 115 | } | |
| 115 | 116 | } | |
| 116 | 117 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments