data-manipulation.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <title>jsGrid - Data Manipulation</title>
  6. <link rel="stylesheet" type="text/css" href="demos.css" />
  7. <link href='http://fonts.googleapis.com/css?family=Open+Sans:300,600,400' rel='stylesheet' type='text/css'>
  8. <link rel="stylesheet" type="text/css" href="../css/jsgrid.css" />
  9. <link rel="stylesheet" type="text/css" href="../css/theme.css" />
  10. <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.2/themes/cupertino/jquery-ui.css">
  11. <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
  12. <script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
  13. <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
  14. <script src="db.js"></script>
  15. <script src="../src/jsgrid.core.js"></script>
  16. <script src="../src/jsgrid.load-indicator.js"></script>
  17. <script src="../src/jsgrid.load-strategies.js"></script>
  18. <script src="../src/jsgrid.sort-strategies.js"></script>
  19. <script src="../src/jsgrid.field.js"></script>
  20. <script src="../src/fields/jsgrid.field.text.js"></script>
  21. <script src="../src/fields/jsgrid.field.number.js"></script>
  22. <script src="../src/fields/jsgrid.field.select.js"></script>
  23. <script src="../src/fields/jsgrid.field.checkbox.js"></script>
  24. <script src="../src/fields/jsgrid.field.control.js"></script>
  25. <style>
  26. .ui-widget *, .ui-widget input, .ui-widget select, .ui-widget button {
  27. font-family: 'Helvetica Neue Light', 'Open Sans', Helvetica;
  28. font-size: 14px;
  29. font-weight: 300 !important;
  30. }
  31. .details-form-field input,
  32. .details-form-field select {
  33. width: 250px;
  34. float: right;
  35. }
  36. .details-form-field {
  37. margin: 30px 0;
  38. }
  39. .details-form-field:first-child {
  40. margin-top: 10px;
  41. }
  42. .details-form-field:last-child {
  43. margin-bottom: 10px;
  44. }
  45. .details-form-field button {
  46. display: block;
  47. width: 100px;
  48. margin: 0 auto;
  49. }
  50. input.error, select.error {
  51. border: 1px solid #ff9999;
  52. background: #ffeeee;
  53. }
  54. label.error {
  55. float: right;
  56. margin-left: 100px;
  57. font-size: .8em;
  58. color: #ff6666;
  59. }
  60. </style>
  61. </head>
  62. <body>
  63. <h1>Data Manipulation</h1>
  64. <div id="jsGrid"></div>
  65. <div id="detailsDialog">
  66. <form id="detailsForm">
  67. <div class="details-form-field">
  68. <label for="name">Name:</label>
  69. <input id="name" name="name" type="text" />
  70. </div>
  71. <div class="details-form-field">
  72. <label for="age">Age:</label>
  73. <input id="age" name="age" type="number" />
  74. </div>
  75. <div class="details-form-field">
  76. <label for="address">Address:</label>
  77. <input id="address" name="address" type="text" />
  78. </div>
  79. <div class="details-form-field">
  80. <label for="country">Country:</label>
  81. <select id="country" name="country">
  82. <option value="">(Select)</option>
  83. <option value="1">United States</option>
  84. <option value="2">Canada</option>
  85. <option value="3">United Kingdom</option>
  86. <option value="4">France</option>
  87. <option value="5">Brazil</option>
  88. <option value="6">China</option>
  89. <option value="7">Russia</option>
  90. </select>
  91. </div>
  92. <div class="details-form-field">
  93. <label for="married">Is Married</label>
  94. <input id="married" name="married" type="checkbox" />
  95. </div>
  96. <div class="details-form-field">
  97. <button type="submit" id="save">Save</button>
  98. </div>
  99. </form>
  100. </div>
  101. <script>
  102. $(function() {
  103. $("#jsGrid").jsGrid({
  104. height: "70%",
  105. width: "100%",
  106. editing: true,
  107. autoload: true,
  108. paging: true,
  109. deleteConfirm: function(item) {
  110. return "The client \"" + item.Name + "\" will be removed. Are you sure?";
  111. },
  112. rowClick: function(args) {
  113. showDetailsDialog("Edit", args.item);
  114. },
  115. controller: db,
  116. fields: [
  117. { name: "Name", type: "text", width: 150 },
  118. { name: "Age", type: "number", width: 50 },
  119. { name: "Address", type: "text", width: 200 },
  120. { name: "Country", type: "select", items: db.countries, valueField: "Id", textField: "Name" },
  121. { name: "Married", type: "checkbox", title: "Is Married", sorting: false },
  122. {
  123. type: "control",
  124. modeSwitchButton: false,
  125. editButton: false,
  126. headerTemplate: function() {
  127. return $("<button>").attr("type", "button").text("Add")
  128. .on("click", function () {
  129. showDetailsDialog("Add", {});
  130. });
  131. }
  132. }
  133. ]
  134. });
  135. $("#detailsDialog").dialog({
  136. autoOpen: false,
  137. width: 400,
  138. close: function() {
  139. $("#detailsForm").validate().resetForm();
  140. $("#detailsForm").find(".error").removeClass("error");
  141. }
  142. });
  143. $("#detailsForm").validate({
  144. rules: {
  145. name: "required",
  146. age: { required: true, range: [18, 150] },
  147. address: { required: true, minlength: 10 },
  148. country: "required"
  149. },
  150. messages: {
  151. name: "Please enter name",
  152. age: "Please enter valid age",
  153. address: "Please enter address (more than 10 chars)",
  154. country: "Please select country"
  155. },
  156. submitHandler: function() {
  157. formSubmitHandler();
  158. }
  159. });
  160. var formSubmitHandler = $.noop;
  161. var showDetailsDialog = function(dialogType, client) {
  162. $("#name").val(client.Name);
  163. $("#age").val(client.Age);
  164. $("#address").val(client.Address);
  165. $("#country").val(client.Country);
  166. $("#married").prop("checked", client.Married);
  167. formSubmitHandler = function() {
  168. saveClient(client, dialogType === "Add");
  169. };
  170. $("#detailsDialog").dialog("option", "title", dialogType + " Client")
  171. .dialog("open");
  172. };
  173. var saveClient = function(client, isNew) {
  174. $.extend(client, {
  175. Name: $("#name").val(),
  176. Age: parseInt($("#age").val(), 10),
  177. Address: $("#address").val(),
  178. Country: parseInt($("#country").val(), 10),
  179. Married: $("#married").is(":checked")
  180. });
  181. $("#jsGrid").jsGrid(isNew ? "insertItem" : "updateItem", client);
  182. $("#detailsDialog").dialog("close");
  183. };
  184. });
  185. </script>
  186. </body>
  187. </html>