0

I'm trying to use a directive twice and filter using the options created by the directives. For some reason the chemicalsFilter isn't working as expected. If I use either of the filters on the ng-repeat it works but using both together isn't working. I'm wondering if this could be a scope issue within the directives but each seem to return the correct data. It outputs in the console as I expect but the filtering just doesn't work as expected.

Any ideas? If I can get this sorted it will be so much easier creating multiple combo boxes in my project.

This is my example plnkr: http://plnkr.co/edit/FTPdSSiKSOZiWptfJzuC?p=preview

Original example (both examples do the same thing): http://democode.6te.net/filterusingdirectives/

        <div ng-controller="resultsCtrl">

            <div ng-controller="chemicals">

                <combo-box url="/filterusingdirectives/php/getChemicals.php" list-name="Select Chemicals" loading-message="loading chemicals ..." combo-box-directive-selected-values="getSelectedChemicalValues"></combo-box>

            </div>
            <div ng-controller="letters">

                <combo-box url="/filterusingdirectives/php/getLetters.php" list-name="Select Letters" loading-message="loading letters ..." combo-box-directive-selected-values="getSelectedLetterValues"></combo-box>

            </div>

            <div ng-repeat="result in results | chemicalsFilter:selectedChemicalValues | lettersFilter:selectedLetterValues">
                letterId:   {{result.letterId}}
                <br />
                chemicalId: {{result.chemId}}
                <br />
                name: {{result.name}}
                <br /><br />
            </div>
        </div>  
user1532669
  • 2,288
  • 4
  • 36
  • 72

1 Answers1

2

inner loops must be replaced with respective selected("selectedLetterValues and selectedChemicalValues") values instead of results.

lettersFilter :

for (var i = 0; i < results.length; i++) {
 for (var j = 0; j < selectedLetterValues.length; j++) {
    .......
 }
}

chemicalsFilter :

for (var i = 0; i < results.length; i++) {
 for (var j = 0; j < selectedChemicalValues.length; j++) {
    .......
 }
}

Updated : I don't see any other issue apart from logic. May be you have debugged it and thought why filters is executing multiple times, might be this is an issue. here is the explanation

Why your code is not working?

when you click Acetic acid ( for sake of comfort we will go with letterId only)

in chemicalsFilter(results, selectedChemicalValues):
input  results=[letterID:10, letterId:20, litterId:30]
       selectedChemicalValues=[letterId:20, litterId:30]

       for (var i = 0; i < results.length; i++) {
        for (var j = 0; j < results.length; j++) {
           .......
        }
       }

output filteredResults = [letterId:20, litterId:30]

in lettersFilter(filteredResults, selectedLetterValues):
input results=[letterId:20, litterId:30]
      selectedLetterValues=[letterID:10, letterId:20, litterId:30]

      for (var i = 0; i < results.length; i++) {
       for (var j = 0; j < results.length; j++) {
           .......
       }
      }

output filteredResults = [letterId:20]

In lettersFilter both outer loop and inner loop are checking size on results.length so it will never get chance to compare 3rd index, so it retruns 2nd index which contains Acetone

Community
  • 1
  • 1
Pavan Kumar Jorrigala
  • 3,085
  • 16
  • 27
  • I don't think that's the problem as the second filter wouldn't work and it does. There is something deeper wrong with it I think. Thanks for taking a look, any other suf – user1532669 Aug 17 '15 at 06:53
  • if you flip the order of filters in ng-repeat you will see the issue with second filter too. – Pavan Kumar Jorrigala Aug 19 '15 at 04:24
  • Yeah I noticed flipping the order yields the same issue ..... I'll have a go at implementing your suggestion when I have time ..... Hopefully sometime this evening. I have used my existing code elsewhere although not within a directive and that works fine. I really hope your suggestion works:) .... Dying to sort this – user1532669 Aug 19 '15 at 06:39
  • Yeah, I banged my head against the wall for quite a while and Pavan is correct. – bobleujr Aug 19 '15 at 13:41
  • @bobleujr yeah I now see Pavan is totally correct. Thank you both very much for looking at this for me. I was so sure it wasn't that .... but it was. It now works exactly as I want. Just in time too as I've been asked to do something which includes lots of filtering so this will definitely help. Thank you again so much .... 50 points to Pavan :) – user1532669 Aug 19 '15 at 19:46
  • @PavanKumarJorrigala thank you so much, that's been a great help :) – user1532669 Aug 19 '15 at 19:54
  • @PavanKumarJorrigala Sorry just awarded your bounty. I thought accepting the answer would automatically award it but it doesn't. You should have it now. Thanks again :) – user1532669 Aug 20 '15 at 16:39