Guest User

Untitled

a guest
Jan 16th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. //PostgreSQL:
  2.  
  3. CREATE DATABASE mydb
  4. WITH OWNER = postgres
  5. ENCODING = 'UTF8'
  6. TABLESPACE = pg_default
  7. LC_COLLATE = 'ru_RU.UTF-8'
  8. LC_CTYPE = 'ru_RU.UTF-8'
  9. CONNECTION LIMIT = -1;
  10.  
  11. CREATE TABLE states
  12. (
  13. id serial NOT NULL,
  14. "name" character varying(30) NOT NULL,
  15. CONSTRAINT states_pkey PRIMARY KEY (id)
  16. )
  17. WITH (
  18. OIDS=FALSE
  19. );
  20.  
  21. INSERT INTO states("name") VALUES('Colorado');
  22. INSERT INTO states("name") VALUES('California');
  23. INSERT INTO states("name") VALUES('Alabama');
  24. INSERT INTO states("name") VALUES('Alaska');
  25.  
  26. //Grails 1.3.7:
  27.  
  28. package myapplication
  29.  
  30. import groovy.sql.Sql
  31. import grails.converters.*
  32.  
  33. class AutocompleteController {
  34.  
  35. def index = { }
  36.  
  37. def test = {
  38.  
  39. String name=params.name
  40.  
  41. def states = []
  42.  
  43. if(name.endsWith("*")){
  44.  
  45. name=name.substring(0,name.length()-1)
  46.  
  47. def conn = Sql.newInstance("jdbc:postgresql://localhost:5432/mydb","postgres","password","org.postgresql.Driver")
  48.  
  49. def results =conn.rows("SELECT id, name FROM states WHERE name ILIKE '"+name+"%'")
  50.  
  51. results.each(){
  52. states.add(id:it.id,name:it.name)
  53. }
  54.  
  55. }
  56.  
  57. render(contentType: "text/json") {
  58. states
  59. }
  60.  
  61. }
  62. }
  63.  
  64.  
  65. //index.gsp
  66.  
  67. <%@ page contentType="text/html;charset=UTF-8" %>
  68. <html>
  69. <head>
  70. <title>Autocomplete</title>
  71. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  72. <script type="text/javascript" src="js/dojo/dojo.js" djConfig="parseOnLoad:true, isDebug:false"></script>
  73. <style type="text/css">
  74. @import "js/dojo/resources/dojo.css";
  75. @import "js/dijit/themes/tundra/tundra.css";
  76. </style>
  77. <script type="text/javascript">
  78. dojo.require("dijit.form.Form");
  79. dojo.require("dijit.form.Button");
  80. dojo.require("dijit.form.ValidationTextBox");
  81. dojo.require("dijit.form.FilteringSelect");
  82. dojo.require("dojox.data.JsonRestStore");
  83. </script>
  84. </head>
  85. <body class="tundra">
  86. <div dojoType="dijit.form.Form" id="myForm" data-dojo-id="myForm" encType="multipart/form-data" action="" method="">
  87. <table style="border: 1px solid #9f9f9f;" cellspacing="10">
  88. <tr>
  89. <td>
  90. <label for="name">
  91. Test:
  92. </td>
  93. <td>
  94. <input type="text" id="name" name="name" required="true" dojoType="dijit.form.ValidationTextBox"/>
  95. </td>
  96. </tr>
  97. <tr>
  98. <td>
  99. <label for="state">
  100. State:
  101. </td>
  102. <td>
  103. <input id="stateSelect">
  104. </td>
  105. </tr>
  106. </table>
  107. <button dojoType="dijit.form.Button" type="reset">
  108. Reset
  109. </button>
  110. </div>
  111. </body>
  112. <script type="text/javascript">
  113. dojo.addOnLoad(function() {
  114. var stateStore = new dojox.data.JsonRestStore({target:"<g:createLink controller="autocomplete" action="test"/>"});
  115. new dijit.form.FilteringSelect({
  116. id: "stateSelect",
  117. name: "state",
  118. store: stateStore,
  119. searchAttr: "name"
  120. },
  121. "stateSelect");
  122. });
  123. </script>
  124. </html>
Add Comment
Please, Sign In to add comment