tidev / titanium-sdk

🚀 Native iOS and Android Apps with JavaScript
https://titaniumsdk.com/
Other
2.76k stars 1.21k forks source link

fix(android): fix noresults event in ListView with custom query #14034

Closed m1ga closed 6 months ago

m1ga commented 6 months ago

When using a custom Ti.UI.Textfield for a search view the noresults event is not fired. The issue is that it is just checking for filterQuery which is not set when you use listView.searchText = "". This PR will set that value which makes the noeresults event fire again.

var win = Ti.UI.createWindow({layout: "vertical"});
var searchView = Ti.UI.Android.createSearchView();

var txtSearch = Ti.UI.createTextField({
    left: '80dp',
    right: '55dp',
    top: 0,
    backgroundColor: 'red',
    borderStyle: Titanium.UI.INPUT_BORDERSTYLE_NONE,
    height: '40dp', //my change
    hintText: "Search from drinks",
    borderStyle: Titanium.UI.INPUT_BORDERSTYLE_NONE,
    returnKeyType: Titanium.UI.RETURNKEY_SEARCH,
    id: "txtSearch"
});

win.add(txtSearch);

var listView = Ti.UI.createListView({});
listView.addEventListener("noresults", function() {
    console.log("no results")
})
txtSearch.addEventListener('change', function(e) {
    listView.searchText = e.value.toLowerCase();
});
var sections = [];
var fruitDataSet = [{   properties: {searchableText: "foo",title: 'Apple'}}, {properties: {searchableText: "foo",title: 'Banana'}}];
var fruitSection = Ti.UI.createListSection({headerTitle: 'Fruits',items: fruitDataSet});
sections.push(fruitSection);
var vegDataSet = [{properties: {searchableText: "foo",title: 'Carrots'}}, {properties: {searchableText: "foo",title: 'Potatoes'}}];
var vegSection = Ti.UI.createListSection({headerTitle: 'Vegetables',items: vegDataSet});
sections.push(vegSection);
listView.sections = sections;
var fishDataSet = [{properties: {searchableText: "foo",title: 'Cod'}}, {properties: {searchableText: "foo",title: 'Haddock'}}];
var fishSection = Ti.UI.createListSection({headerTitle: 'Fish', items: fishDataSet});
listView.appendSection(fishSection);
win.add(listView);
win.open();