簡體   English   中英

NativeScript ListView itemTap 不會清空項目

[英]NativeScript ListView itemTap won't empty items

作為我正在構建的 NativeScript iOS/Android 應用程序的一部分,我需要:
1. 搜索項目列表
2.選擇搜索結果之一
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);
    });

}

非常感謝任何可以提供幫助的人。 我認為列表項清除它自己的父列表有一些更復雜的事情。

埃米爾的建議最終為我解決了這個問題。

我改為使用可觀察對象,這意味着當點擊列表項時,我可以成功清空數組:

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);
    });

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM