Results 1 to 6 of 6

Thread: Searching Sub Stores

  1. #1
    Sencha User
    Join Date
    Apr 2007
    Posts
    138
    Answers
    1

    Default Answered: Searching Sub Stores

    I've got a dataview that is populated from a store with the following structure:

    Code:
    [
       {
          courseID: '1',
          courseName: 'Math',
          tests: [
              {
                     testID: '100',
                     testName: 'Equations',
                     testType: 'Test',
                     ...  
               },
               {
                     testID: '101',
                     testName: 'More mathy stuff',
                     testType: 'Quiz',
                     ...
               }
          ] 
       }
    ]
    I have a searchfield whereby I am trying to filter the store. I can filter by the 'courseName' using the following code in a controller:

    Code:
    if (queryString) {
        var reg = new RegExp(queryString, 'i');
    
        store.filterBy(function(record) {
            if (reg.test(record.get('courseName) {
                 return true;
            }
            return false;
        }
    }
    I'd also like to be able to search by 'testName' and 'testType'. I can retrieve the 'tests' data by using record.data.tests, but it obviously returns several objects. I'm assuming I would have to search through the sub stores, but I can't seem to figure out how that would work. Can anyone point me in the right direction?

  2. Try updating your onCourseSearchKeyup function as shown below:

    Code:
    function(textfield, e, eOpts) {
        // Grab the value from the text field each time a key is pressed
        queryString = textfield.getValue();
        //console.log(queryString);
    
    
        // Get store and clear filters
        var store = Ext.getStore('test');
        store.clearFilter();
    
    
        if (queryString) {
            var reg = new RegExp(queryString, 'i');
    
    
            store.filterBy(function(record) {
                var result = false;
    
    
                if (reg.test(record.get('courseName'))) {
                    console.log('matched on course name');
                    result = true;
                } else {
                    Ext.Array.each(record.tests().getRange(), function(test) {
                        if (reg.test(test.get('testType'))) {
                            console.log('matched on test type');
                            result = true;
                        } else if (reg.test(test.get('testName'))) {
                            console.log('matched on test name');
                            result = true;
                        }
    
    
                        if (result) {
                            return false; // stops the Ext.Array.each loop
                        }
                    });
                }
    
    
                return result;
            });
        }
    }
    Regards,
    Brian


  3. #2
    Sencha Premium Member u25771's Avatar
    Join Date
    Sep 2012
    Location
    Switzerland :)
    Posts
    253
    Answers
    27

    Default

    So "courseName" is in another store than testName and testType?


    Store A: testName & testType
    Store B: courseName

    Datalist data: Store A
    Filter: Store A & Store B

    is this correct?
    Mark thread as answered if your problem could be solved and leave a vote for helpful answers

    For more help check out my new blog: http://abitofcoding.blogspot.com

    T
    o get in contact with me just send me a message on Google+

  4. #3
    Sencha User
    Join Date
    Apr 2007
    Posts
    138
    Answers
    1

    Default

    Quote Originally Posted by u25771 View Post
    So "courseName" is in another store than testName and testType?
    Actually it's all in the same store, one with nested data. The top node is the course, while the underlying second node is the tests. The models look something like this:

    Course Model
    Code:
    Ext.define('testEnvironment.model.Course', {
        extend: 'Ext.data.Model',
    
        requires: [
            'Ext.data.Field',
            'Ext.data.association.HasMany'
        ],
        uses: [
            'testEnvironment2.model.Test'
        ],
    
        config: {
            fields: [
                {
                    name: 'courseID'
                },
                {
                    name: 'courseName'
                }
            ],
            hasMany: {
                associationKey: 'tests',
                primaryKey: 'uuid',
                model: 'testEnvironment.model.Test',
                name: 'tests'
            }
        }
    });
    Test Model
    Code:
    Ext.define('testEnvironment.model.Test', {
        extend: 'Ext.data.Model',
    
        requires: [
            'Ext.data.Field',
            'Ext.data.association.HasMany',
            'Ext.data.association.BelongsTo'
        ],
        uses: [
            'testEnvironment.model.Course'
        ],
    
        config: {
            idProperty: 'uuid',
            fields: [
                {
                    name: 'testID'
                },
                {
                    name: 'testName'
                },
                {
                    name: 'testType'
                }
            ],
            belongsTo: {
                model: 'testEnvironment.model.Course'
            }
        }
    });

  5. #4
    Sencha Premium Member u25771's Avatar
    Join Date
    Sep 2012
    Location
    Switzerland :)
    Posts
    253
    Answers
    27

    Default

    Thanks for the additional information.

    Does something like this help you?

    Code:
    if (queryString) {
    
    
        var didMatch = false;
        var reg = new RegExp(queryString, 'i');
    
    
        store.filterBy(function(record) {
            if (reg.test(record.get('courseName')) {
                    didMatch = true;
                    return didMatch;
                } else {
    
    
                    var hadManyArr = record.tests();
    
    
                    Ext.Array.each(hadManyArr, function(assRec, index, textfieldsItSelf) {
    
    
                        if (reg.test(assRec.get('testName')) || reg.test(assRec.get('testType'))) {
                            didMatch = true;
                            return didMatch;
                        }
    
    
                    });
    
    
                }
            }
    
    
            return didMatch;
        })
    }
    You could also check the example on my blog and handle the associates like separate fields http://abitofcoding.blogspot.ch/2014...archfield.html
    Mark thread as answered if your problem could be solved and leave a vote for helpful answers

    For more help check out my new blog: http://abitofcoding.blogspot.com

    T
    o get in contact with me just send me a message on Google+

  6. #5
    Sencha User
    Join Date
    Apr 2007
    Posts
    138
    Answers
    1

    Default

    Quote Originally Posted by u25771 View Post
    Thanks for the additional information.

    Does something like this help you?

    Code:
    if (queryString) {
    
    
        var didMatch = false;
        var reg = new RegExp(queryString, 'i');
    
    
        store.filterBy(function(record) {
            if (reg.test(record.get('courseName')) {
                    didMatch = true;
                    return didMatch;
                } else {
    
    
                    var hadManyArr = record.tests();
    
    
                    Ext.Array.each(hadManyArr, function(assRec, index, textfieldsItSelf) {
    
    
                        if (reg.test(assRec.get('testName')) || reg.test(assRec.get('testType'))) {
                            didMatch = true;
                            return didMatch;
                        }
    
    
                    });
    
    
                }
            }
    
    
            return didMatch;
        })
    }
    Based on your example, I think I'm heading in the right direction but I'm not quite there yet. I've put together a Fiddle to help illustrate the problem. The search field properly filters the courseName. Based on console logs, it even recognizes a proper input of the nested data options (testType or testName), but displays an empty store. For example, try to type the word "quiz" in the search field in the Fiddle example.


  7. #6
    Sencha Premium User lumberjack's Avatar
    Join Date
    Nov 2014
    Location
    Kansas
    Posts
    781
    Answers
    102

    Default

    Try updating your onCourseSearchKeyup function as shown below:

    Code:
    function(textfield, e, eOpts) {
        // Grab the value from the text field each time a key is pressed
        queryString = textfield.getValue();
        //console.log(queryString);
    
    
        // Get store and clear filters
        var store = Ext.getStore('test');
        store.clearFilter();
    
    
        if (queryString) {
            var reg = new RegExp(queryString, 'i');
    
    
            store.filterBy(function(record) {
                var result = false;
    
    
                if (reg.test(record.get('courseName'))) {
                    console.log('matched on course name');
                    result = true;
                } else {
                    Ext.Array.each(record.tests().getRange(), function(test) {
                        if (reg.test(test.get('testType'))) {
                            console.log('matched on test type');
                            result = true;
                        } else if (reg.test(test.get('testName'))) {
                            console.log('matched on test name');
                            result = true;
                        }
    
    
                        if (result) {
                            return false; // stops the Ext.Array.each loop
                        }
                    });
                }
    
    
                return result;
            });
        }
    }
    Regards,
    Brian


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •