Permalink
Browse files

Working ajax request returning JSON with Items matches. Non-working J…

…Query UI autocomplete
  • Loading branch information...
1 parent 265d19f commit 1b9e52871612259170dd181c7b703bc2dc3a1091 @rajish committed Jun 14, 2011
Showing with 44 additions and 10 deletions.
  1. +8 −0 app/controllers/Invoices.java
  2. +1 −1 app/views/Invoices/_itemsTable.html
  3. +35 −9 app/views/Invoices/soldItem.js
@@ -3,6 +3,7 @@
import java.util.List;
import models.Invoice;
+import models.Item;
import models.SoldItem;
import play.cache.Cache;
import play.data.validation.Valid;
@@ -90,4 +91,11 @@ public static void saveItem(@Valid SoldItem item) {
render(newItems);
}
+
+ public static void getCompletions(String startsWith, int maxRows) {
+ System.out.println("Invoices.getCompletions() startsWith:" + startsWith);
+ List<Item> completions = Item.find("byNameIlike", startsWith + "%").fetch(maxRows);
+ System.out.println("Invoices.getCompletions() number of completions = " + completions.size());
+ renderJSON(completions);
+ }
}
@@ -49,7 +49,7 @@
<td>
<div class="item">
- <input class="itemDesc" type="text" size="30" name="item.item.name" placeholder="&{'invoice.item.name'}" />
+ <input class="itemDesc" id="itemName" type="text" size="30" name="item.item.name" placeholder="&{'invoice.item.name'}" />
</div>
<div class="item">
<textarea class="itemDesc" rows="2" cols="30" name="item.item.description" placeholder="&{'invoice.item.description'}" ></textarea>
@@ -10,6 +10,39 @@
this.get('#/', function(context) {
context.log('main');
+ $('#itemName').autocomplete({
+ source: function (request, response) {
+ Sammy.log("autocomplete::source req:" + request);
+ var url = #{jsAction @Invoices.getCompletions() /};
+ var data = {
+ startsWith: request.term,
+ maxRows: 12
+ };
+ return context.send($.post, url(), data)
+ .then(function(contents) {
+ response($.map(contents.name, function(item) {
+ return {
+ label: item.name + item.description,
+ value: item.name
+ };
+ }));
+ });
+ },
+
+ minLength: 0,
+
+ select: function (event, ui) {
+ Sammy.log("autocomplete::select");
+ },
+
+ open: function () {
+ Sammy.log("autocomplete::open");
+ },
+
+ close: function () {
+ Sammy.log("autocomplete::close");
+ }
+ });
});
this.post('#/saveItem', function (context) {
@@ -28,15 +61,8 @@
});
$(function( ) {
+
+
app.run('#/');
});
})(jQuery);
-/*
-var event = jQuery.Event("saveItem");
-$("#saveItem").click(function(e) {
- //e.stopImmediatePropagation();
- this.trigger(event);
- log('clicked');
- //return false;
-});
-*/

0 comments on commit 1b9e528

Please sign in to comment.