Преглед изворни кода

Add sorting by different categories

Lee Morgan пре 6 година
родитељ
комит
f6ba6286d6
2 измењених фајлова са 26 додато и 1 уклоњено
  1. 10 1
      views/dashboardPage/dashboard.ejs
  2. 16 0
      views/dashboardPage/ingredients.js

+ 10 - 1
views/dashboardPage/dashboard.ejs

@@ -63,7 +63,16 @@
                     </button>
                 </div>
 
-                <input id="ingredientSearch" type="text" placeholder="filter" oninput="ingredientsStrandObj.search()">
+                <div>
+                    <input id="ingredientSearch" type="text" placeholder="filter" oninput="ingredientsStrandObj.search()">
+
+                    <select onchange="ingredientsStrandObj.sort(this.value)">
+                        <option value="">Sort By:</option>
+                        <option value="_name">Ingredient</option>
+                        <option value="category">Category</option>
+                        <option value="_unit">Unit of Measurement</option>
+                    </select>
+                </div>
 
                 <div id="categoryList"></div>
             </div>

+ 16 - 0
views/dashboardPage/ingredients.js

@@ -102,5 +102,21 @@ window.ingredientsStrandObj = {
         }
 
         this.displayIngredientsOnly(matchingIngredients);
+    },
+
+    sort: function(sortType){
+        if(sortType === ""){
+            return;
+        }
+
+        document.querySelector("#ingredientSearch").value = "";
+
+        if(sortType === "category"){
+            this.populateIngredients();
+            return;
+        }
+
+        let sortedIngredients = this.ingredients.slice().sort((a, b)=> (a[sortType] > b[sortType]) ? 1 : -1);
+        this.displayIngredientsOnly(sortedIngredients);
     }
 }