NativeScript ListView itemTap 不会清空项目
NativeScript ListView itemTap won't empty items
作为我正在构建的 NativeScript iOS/Android 应用程序的一部分,我需要:
1. 搜索项目列表
2. Select其中一个搜索结果
3.由于选择了一个,清除搜索结果
但它不起作用。奇怪的是,相同的 clearItems 函数在附加到正在清除的 SearchBar 时起作用,只是在点击 ListView 中的项目时不起作用。
相关代码如下:
XML
<Page>
<AbsoluteLayout loaded="layoutLoaded">
<SearchBar hint="Search for your opponent" id="search" submit="performSearch" />
<ListView id="itemList" items="{{ itemList }}" visibility="{{ listVisible ? 'visible' : 'collapsed' }}">
<ListView.itemTemplate>
<WrapLayout>
<Image cssClass="itemImage" src="{{ image }}" />
<Label cssClass="itemLabel" text="{{ niceName }}" />
</WrapLayout>
</ListView.itemTemplate>
</ListView>
</AbsoluteLayout>
</Page>
JavaScript
function clearItems(thisLayout){
thisLayout.bindingContext.itemList = [];
thisLayout.bindingContext.listVisible = false;
}
exports.layoutLoaded = function(args){
var thisLayout = args.object
thisLayout.bindingContext = {
itemList: [],
listVisible: false
};
var searchBar = view.getViewById(thisLayout, 'search');
searchBar.on(searchBarModule.SearchBar.clearEvent, function(args) {
clearItems(args.object.parent);
});
var itemListWrap = view.getViewById(thisLayout, 'itemList');
itemListWrap.on('itemTap', function(args){
var page = args.object.parent;
clearItems(page);
});
}
非常感谢任何可以提供帮助的人。我假设列表项清除它自己的父列表会更复杂。
Emil 的建议最终帮我解决了。
我改为使用可观察对象,这意味着当点击列表项时,我可以成功清空数组:
function clearItems(viewModel){
while(viewModel.itemList.length > 0){
viewModel.itemList.pop();
}
viewModel.set('listVisible', false);
}
var viewModel = new observableModule.Observable({
listVisible: false,
itemList: new observableArrayModule.ObservableArray([])
});
exports.layoutLoaded = function(args){
var thisLayout = args.object
thisLayout.bindingContext = viewModel;
var searchBar = view.getViewById(thisLayout, 'search');
searchBar.on(searchBarModule.SearchBar.clearEvent, function(args) {
clearItems(viewModel);
});
var itemListWrap = view.getViewById(thisLayout, 'itemList');
itemListWrap.on('itemTap', function(args){
clearItems(viewModel);
});
}
作为我正在构建的 NativeScript iOS/Android 应用程序的一部分,我需要:
1. 搜索项目列表
2. Select其中一个搜索结果
3.由于选择了一个,清除搜索结果
但它不起作用。奇怪的是,相同的 clearItems 函数在附加到正在清除的 SearchBar 时起作用,只是在点击 ListView 中的项目时不起作用。
相关代码如下:
XML
<Page>
<AbsoluteLayout loaded="layoutLoaded">
<SearchBar hint="Search for your opponent" id="search" submit="performSearch" />
<ListView id="itemList" items="{{ itemList }}" visibility="{{ listVisible ? 'visible' : 'collapsed' }}">
<ListView.itemTemplate>
<WrapLayout>
<Image cssClass="itemImage" src="{{ image }}" />
<Label cssClass="itemLabel" text="{{ niceName }}" />
</WrapLayout>
</ListView.itemTemplate>
</ListView>
</AbsoluteLayout>
</Page>
JavaScript
function clearItems(thisLayout){
thisLayout.bindingContext.itemList = [];
thisLayout.bindingContext.listVisible = false;
}
exports.layoutLoaded = function(args){
var thisLayout = args.object
thisLayout.bindingContext = {
itemList: [],
listVisible: false
};
var searchBar = view.getViewById(thisLayout, 'search');
searchBar.on(searchBarModule.SearchBar.clearEvent, function(args) {
clearItems(args.object.parent);
});
var itemListWrap = view.getViewById(thisLayout, 'itemList');
itemListWrap.on('itemTap', function(args){
var page = args.object.parent;
clearItems(page);
});
}
非常感谢任何可以提供帮助的人。我假设列表项清除它自己的父列表会更复杂。
Emil 的建议最终帮我解决了。
我改为使用可观察对象,这意味着当点击列表项时,我可以成功清空数组:
function clearItems(viewModel){
while(viewModel.itemList.length > 0){
viewModel.itemList.pop();
}
viewModel.set('listVisible', false);
}
var viewModel = new observableModule.Observable({
listVisible: false,
itemList: new observableArrayModule.ObservableArray([])
});
exports.layoutLoaded = function(args){
var thisLayout = args.object
thisLayout.bindingContext = viewModel;
var searchBar = view.getViewById(thisLayout, 'search');
searchBar.on(searchBarModule.SearchBar.clearEvent, function(args) {
clearItems(viewModel);
});
var itemListWrap = view.getViewById(thisLayout, 'itemList');
itemListWrap.on('itemTap', function(args){
clearItems(viewModel);
});
}