我正在创建一个简单的Nativescript应用程序与基于数组的项目列表视图。我在掌握可观察到的模块/MVVM模式时遇到了问题。我使用nativescript-down来提供一个排序依据列表,以便对列表视图中的数据进行排序。当页面第一次呈现时,数据将被正确排序,但是当索引更改时,数据不会更新。
如何确保dropdown的"selectedIndex“值正确传递给视图模型,并相应地更新列表视图?
我已经尝试过在模型和"dropDownSelectedIndexChanged“函数中基于selectedIndex进行排序。
main-page.ts
const mainPageModel = new MainPageModel();
export function navigatingTo(args: EventData) {
const page = <Page>args.object;
page.bindingContext = mainPageModel;
}
我已经尝试在主页中设置此函数,但它不会更新列表,除非导航离开,然后返回页面:
export function dropDownSelectedIndexChanged(args: SelectedIndexChangedEventData) {
if (args.newIndex == 0) {
mainPageModel.dataItems.sort(function (a, b) {
return a.name.localeCompare(b.name);
});
} else if (args.newIndex == 1) {
mainPageModel.dataItems.sort(function (a, b) {
return b.name.localeCompare(a.name);
});
}
}
main-view-model.ts
export class MainPageModel extends Observable {
// Dropdown populating
@ObservableProperty() dropdownItems: ObservableArray<any>;
@ObservableProperty() selectedIndex: number;
// Recipe templating
@ObservableProperty() isBusy: boolean = true;
@ObservableProperty() dataItems: ObservableArray<any>;
constructor() {
super();
this.isBusy = true;
this.dataItems = new ObservableArray<any>();
this.dropdownItems = new ObservableArray<any>();
this.selectedIndex = 0;
// Populate sort by dropdown
const items = ["Name (A-Z)", "Name (Z-A)"];
this.dropdownItems.push(items);
// Populate recipes stored locally
let storedRecipes = [];
try {
storedRecipes = appSettings.getAllKeys();
storedRecipes.forEach(element => {
this.dataItems.push(JSON.parse(appSettings.getString(element)))
});
} catch (e) {
console.log("No stored recipes")
}
// Populate recipes from placeholder data
getData().then((recipeData) => {
this.dataItems.push(recipeData);
this.isBusy = false;
// Sort recipes by index
// Done as an alternative to "dropDownSelectedIndexChanged"
if (this.selectedIndex == 0) {
this.dataItems.sort(function (a, b) {
return a.name.localeCompare(b.name);
});
} else if (this.selectedIndex == 1) {
this.dataItems.sort(function (a, b) {
return b.name.localeCompare(a.name);
});
}
});
}
}
main-page.xml
<GridLayout columns="*,auto,auto" rows="auto,5*" >
<!-- Add  back to button -->
<Button horizontalAlignment="left" row="0" col="0" text="Add New Recipe" tap="addRecipe" class="add-button" />
<Label horizontalAlignment="right" verticalAlignment="center" text="Sort by:" col="1" row="0" padding="10" fontWeight="bold" fontSize="18" />
<dd:DropDown items="{{ dropdownItems }}" selectedIndex="{{ selectedIndex }}"
opened="dropDownOpened" closed="dropDownClosed"
selectedIndexChanged="dropDownSelectedIndexChanged"
row="0" col="2" itemsPadding="8" verticalAlignment="center" itemsTextAlignment="center" horizontalAlignment="right" />
<ListView row="1" colSpan="3" class="list-group" items="{{ dataItems }}" separatorColor="transparent">
<ListView.itemTemplate>
<GridLayout recipeData="{{ $value }}" tap="recipeDetail" route="recipe-detail/recipe-detail" rows="*,auto,auto" columns="5*, *" class="list-group-item">
<Label row="0" col="0" text="{{ name }}" class="h2" />
<Image horizontalAlignment="center" row="0" rowSpan="2" col="2" src="{{ image }}" />
</GridLayout>
</ListView.itemTemplate>
</ListView>
<ActivityIndicator row="1" colSpan="3" busy="{{ isBusy }}" class="activity-indicator" />
</GridLayout>
如果逻辑在视图模型中,则排序在初始页面加载时有效,而如果我选择另一个索引,则排序不会更改。或者,如果我试图通过"dropDownSelectedIndexChanged“函数更改"selectedIndex”值,列表视图不会立即排序,只有在导航离开并返回主页后才会更新。
发布于 2019-10-21 18:14:34
与其他方法(切片、弹出、推送)不同,sort(...)
方法不会通知组件更改。尝试在排序后刷新ListView,
export function dropDownSelectedIndexChanged(args: SelectedIndexChangedEventData) {
if (args.newIndex == 0) {
mainPageModel.dataItems.sort(function (a, b) {
return a.name.localeCompare(b.name);
});
} else if (args.newIndex == 1) {
mainPageModel.dataItems.sort(function (a, b) {
return b.name.localeCompare(a.name);
});
}
(<any>event.object).page.getViewById('YourListViewId').refresh();
}
https://stackoverflow.com/questions/58490991
复制