Guest User

Untitled

a guest
Jan 15th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 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. if(name.endsWith("*")){
  42. name=name.substring(0,name.length()-1)
  43. }
  44.  
  45. def conn = Sql.newInstance("jdbc:postgresql://localhost:5432/mydb","postgres","password","org.postgresql.Driver")
  46.  
  47. def results =conn.rows("SELECT id, name FROM states WHERE name ILIKE '"+name+"%'")
  48.  
  49. def states = []
  50. results.each(){
  51. states.add(id:it.id.toString(),name:it.name)
  52. }
  53.  
  54. render(contentType: "text/json") {
  55. identifier="id"
  56. label= "name"
  57. items=states
  58. }
  59. }
  60.  
  61. }
  62.  
  63. //index.gsp
  64.  
  65. <%@ page contentType="text/html;charset=UTF-8" %>
  66. <html>
  67. <head>
  68. <title>Autocomplete</title>
  69. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  70. <script type="text/javascript" src="js/dojo/dojo.js" djConfig="parseOnLoad:true, isDebug:false"></script>
  71. <style type="text/css">
  72. @import "js/dojo/resources/dojo.css";
  73. @import "js/dijit/themes/tundra/tundra.css";
  74. </style>
  75. <script type="text/javascript">
  76. dojo.require("dijit.form.Form");
  77. dojo.require("dijit.form.Button");
  78. dojo.require("dijit.form.ValidationTextBox");
  79. dojo.require("dijit.form.FilteringSelect");
  80. dojo.require("dojox.data.JsonRestStore");
  81. </script>
  82. </head>
  83. <body class="tundra">
  84. <div dojoType="dijit.form.Form" id="myForm" data-dojo-id="myForm" encType="multipart/form-data" action="" method="">
  85. <table style="border: 1px solid #9f9f9f;" cellspacing="10">
  86. <tr>
  87. <td>
  88. <label for="name">
  89. Test:
  90. </td>
  91. <td>
  92. <input type="text" id="name" name="name" required="true" dojoType="dijit.form.ValidationTextBox"/>
  93. </td>
  94. </tr>
  95. <tr>
  96. <td>
  97. <label for="state">
  98. State:
  99. </td>
  100. <td>
  101. <input id="stateSelect">
  102. </td>
  103. </tr>
  104. </table>
  105. <button dojoType="dijit.form.Button" type="reset">
  106. Reset
  107. </button>
  108. </div>
  109. </body>
  110. <script type="text/javascript">
  111. dojo.addOnLoad(function() {
  112. var stateStore = new dojox.data.JsonRestStore({target:"<g:createLink controller="autocomplete" action="test"/>"});
  113. new dijit.form.FilteringSelect({
  114. id: "stateSelect",
  115. name: "state",
  116. store: stateStore,
  117. searchAttr: "name"
  118. },
  119. "stateSelect");
  120. });
  121. </script>
  122. </html>
Add Comment
Please, Sign In to add comment