Ram Kumar
Ram Kumar

Reputation: 590

Sencha Touch filterBy the Store

Code:

this.searchField = new Ext.form.Text({
            displayField: 'name',
            valueField: 'name',
            editable: true,
            forceSelection: true,
            triggerAction: 'all',
            emptyText: 'Search...',
            selectOnFocus: true
    });

this.searchButton = new Ext.Button({// The button press will invoke the search action
        text: 'Go',
        handler: this.searchButtonTap,
        scope: this
    });

    this.topToolbar = new Ext.Toolbar({
                   items: [
                { xtype: 'spacer' },    
                    this.searchField,
                    this.searchButton,
                    { xtype: 'spacer' },
                ]
        });

searchButtonTap: function () {

        var searchText = this.searchField.getValue();
        console.log(searchText);
        //console.log(this.myappsList.store.getCount());
        console.log(this.myappsList.store.filter('name', searchText));
        //this.myappsList.store.filter(searchText);

    },

in my models i have four fields:

Name, Age, Sex, Charecteristic

What i am trying over here is:

display the content of my database in a list on the page, since the list is long i wanted to search, when user writes the query in search field and presses the search button, the searchButtonTap handler is invoked which tries to filter and display the list with names similar to the ones in query i also tried filterBy but that too did not work. Please let me know whats wrong?

Thanks.

Upvotes: 2

Views: 4331

Answers (1)

whatf
whatf

Reputation: 6458

you can try this way:

searchButtonTap: function () {
        var searchTerm = this.searchField.getValue();
        if (searchTerm) {
            this.myappsList.store.filterBy(function(record){
            var fieldData = record.data.city.toLowerCase().indexOf(searchTerm);
            if (fieldData > -1 ) 
                return record;
            });


        }
        else {
            this.myappsList.store.clearFilter();
            }
    },

Upvotes: 5

Related Questions