Added typeahead through materialize

This commit is contained in:
Brian McGonagill 2024-07-06 18:19:38 -05:00
parent 909030ea0f
commit cba5026ee4
3 changed files with 39 additions and 14 deletions

View file

@ -1,10 +1,12 @@
import { ListItems } from '../../imports/api/listItems.js'
import { Lists } from '../../imports/api/lists.js';
import { Products } from '../../imports/api/products.js';
import { typeahead } from 'typeahead-standalone';
Template.listItemsForm.onCreated(function() {
this.subscribe("myListItems", Session.get("listId"));
this.subscribe("myLists");
// this.subscribe("listProducts", Session.get("listItemVal"));
this.subscribe("myProducts");
});
@ -17,12 +19,16 @@ Template.listItemsForm.onRendered(function() {
Session.set("itemReqErr", false);
Session.set("showReceivedItems", false);
Session.set("filtering", false);
this.autorun(() => {
$('input.autocomplete').autocomplete({
data: Session.get("findListItems"),
});
});
});
Template.listItemsForm.helpers({
selListName: function() {
let selListId = Session.get("listId");
console.log("List ID in Helper is: " + selListId);
let listInfo = Lists.findOne({ _id: selListId });
return listInfo.listName;
},
@ -40,7 +46,7 @@ Template.listItemsForm.helpers({
Template.listItemsForm.events({
'click .saveListItem' (event) {
event.preventDefault();
let item = $("#listItems").val();
let item = $("#findListItems").val();
let listId = Session.get("listId");
if (item == null || item == "") {
Session.set("itemReqErr", true);
@ -50,14 +56,14 @@ Template.listItemsForm.events({
console.log(" ERROR adding item to list: " + err);
} else {
console.log(" SUCCESS adding item to list.");
$("#listItems").val("");
$("#findListItems").val("");
}
});
}
},
'keydown #listItems' (event) {
'keydown #findListItems' (event) {
if (event.which === 13) {
let item = $("#listItems").val();
let item = $("#findListItems").val();
let listId = Session.get("listId");
if (item == null || item == "") {
Session.set("itemReqErr", true);
@ -67,7 +73,7 @@ Template.listItemsForm.events({
console.log(" ERROR adding item to list: " + err);
} else {
console.log(" SUCCESS adding item to list.");
$("#listItems").val("");
$("#findListItems").val("");
}
});
}
@ -86,6 +92,22 @@ Template.listItemsForm.events({
Session.set("searchVal", searchVal);
}
},
'keyup #findListItems' (event) {
if (event.which !== 13) {
let listItemObj = {};
let findItemVal = $("#findListItems").val();
Session.set("listItemVal", findItemVal);
let listItemInfo = Products.find({ prodName: {$regex: findItemVal + '.*', $options: 'i'}}, { limit: 5 }).fetch();
if (typeof listItemInfo != 'undefined' && listItemInfo != "" && listItemInfo != null) {
for (i=0; i < listItemInfo.length; i++) {
let item = listItemInfo[i].prodName;
let store = listItemInfo[i].prodStore;
listItemObj[item] = store;
}
}
Session.set("findListItems", listItemObj);
}
},
'click #filterOn' (event) {
event.preventDefault();
Session.set("filtering", true);