Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. var list = [1, 2, 3, 4, 5];
  2. var items = 1;
  3. $(function () {
  4. //All attribute validationgroupname that equals "All Validators"
  5. $('[validationgroupname="AllValidators"]').each(function () {
  6. //Assigning jquery $(this) to a variable
  7. var $this = $(this);
  8. //Check if the current element has the class 'hasonchangeevent'
  9. if (!$this.hasClass('hasonchangeevent')) {
  10. $this.on('change', function () {
  11. //Attach change event on the element with a function custom_validate
  12. return Custom_Validate($this);
  13. });
  14. //If it does not have the class then add then bind
  15. $this.addClass('hasonchangeevent');
  16. }
  17. });
  18. //On button clicked fire this function
  19. $("#addItemButton").click(function () {
  20. //Finding the minimum number in the array list
  21. var minimum = Math.min.apply(null, list);
  22. //As long as the list length is larger than 0 keep allowing to add dropdown
  23. if (list.length > 0) {
  24. //Appending the container that holds all the Item dropdowns
  25. $("#ContentPlaceHolder1_AddMealContainer")
  26. .append(
  27. $("<div class='form-group' id='mealItem_" +
  28. minimum +
  29. "'> " +
  30. "<div class='row'>" +
  31. "<label ID='addItemLabel_" +
  32. minimum +
  33. "' class='col-xs-4 control-label'>Item</label>" +
  34. "<div class='col-xs-4'>" +
  35. "<select ID ='addItemDropdownList_" +
  36. minimum +
  37. "'class='form-control' validationgroupname='AllValidators' data-describeby='helpBlock_" + minimum + "'></select>" +
  38. "<span class='help-block' id='helpBlock_" + minimum + "' style='display:none'>" +
  39. "</span>" +
  40. "</div>" +
  41. "<span class = 'col-xs-4'>" +
  42. "<input type='button' value='Remove' id='addMealItemRemove_" +
  43. minimum +
  44. "' onclick='removeDropdown(" + minimum + ")' class='btn btn-default btn-sm'" +
  45. "</span>" +
  46. "</div>" +
  47. "</div>")
  48. );
  49. //Same as above but this will apply to all dynamicly created form-groups.
  50. $('[validationgroupname="AllValidators"]').each(function () {
  51. var $this = $(this);
  52. //This will check for the 'hasonchangeevent' so that it does not double apply the same event
  53. if (!$this.hasClass('hasonchangeevent')) {
  54. //Attach change event on the element with a function custom_validate
  55. $this.on('change', function () {
  56. return Custom_Validate($this);
  57. });
  58. //If it does not have the class then add then bind
  59. $this.addClass('hasonchangeevent');
  60. }
  61. });
  62. //Apply tooltips to all the item dropdown fields
  63. $('#addItemDropdownList_' + minimum).tooltip({
  64. title: "<span style ='color:red'>*</span> Select a meal for your training",
  65. html: true
  66. });
  67. //Use a get request to gather the data from the web api foods
  68. $.get("http://localhost:63591/api/foods",
  69. function (data, status, xhr) {
  70. //Check to see if the ready state is Done, the status is 200, and that there is data
  71. if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200 && data) {
  72. //Start a for loop to populate the dropdown menu of food
  73. for (i = 0; i < data.length; i++) {
  74. //Add the default selector --select-- with a option value of 0
  75. if (i == 0) {
  76. $('#addItemDropdownList_' + minimum).append(
  77. "<option value='0'>--Select--</option>"
  78. );
  79. //After the first element then we add the FoodID as the value and the FoodName as the string
  80. } else {
  81. $('#addItemDropdownList_' + minimum).append(
  82. "<option value = " + data[i].FoodID + ">" + data[i].FoodName + "</option>"
  83. );
  84. }
  85. }
  86. }
  87. });
  88. //Find the index of the minimum in the list
  89. var index = list.indexOf(minimum);
  90. //If the index is found we will splice it and remove it from the list, if it is not found it will return -1
  91. if (index > -1)
  92. list.splice(index, 1);
  93. } else {
  94. //If the length is greater than the amoutn provided then it will throw a javascript error
  95. alert("You can only add 5 food items to a meal");
  96. }
  97. });
  98. //Append the item button on the asp.net side to be displayed
  99. $("#addItemButton").append(
  100. "<input type='button' value='Add Food Item' id='addMealItem' class='btn btn-default btn-sm'>"
  101. );
  102. });
  103.  
  104. //When the user clicks on the remove dropdown button and it passes the current element number
  105. function removeDropdown(minimum_value) {
  106. //Find the closest meal item and remove from it
  107. $('#addMealItemRemove_' + minimum_value).closest('#mealItem_' + minimum_value)
  108. .remove('#mealItem_' + minimum_value);
  109. //Add the minimum value that is attached to the object back in to the list so it can be added again
  110. list.push(minimum_value);
  111. }
  112.  
  113. //Function being used to validate on change and on submit
  114. function Custom_Validate(element) {
  115. //Set the current validate to true
  116. var validate = true;
  117. //Checks to see there is an element attached to it so it can check that only event and not every single one, but if there is not it will check everything due to it thinking it is a form submission
  118. $(element ? element : '[validationgroupname="AllValidators"]').each(function () {
  119. //Assigns $(this) to a variable
  120. var $this = $(this);
  121. //Takes the poroperty tagName and puts it lower case
  122. switch ($this.prop('tagName').toLowerCase()) {
  123. case 'input':
  124. {
  125. //This will check to see if the textbox has any value and if not it willput validate to false and show the error
  126. if ($this.val().length == 0) {
  127. $('#' + $this.data('describeby')).text('Meal Name is Not Valid').show();
  128. validate = false;
  129. return;
  130.  
  131. }
  132. //This will check the regex expression for special characters and show the error if there is some found and set validate to false
  133. else if (!/^[a-zA-Z0-9-/ ]*$/.test($this.val())) {
  134. $('#' + $this.data('describeby')).text('Meal Name Contains an Invalid Character').show();
  135. validate = false;
  136. return;
  137. }
  138. //If both properties do not match then it will hide the error
  139. else
  140. {
  141. $('#' + $this.data('describeby')).hide();
  142. return;
  143. }
  144. }
  145. case 'select':
  146. {
  147. //Checks to see if the value is not 0 which is the orginal select statement. If it is it will show the error and set the validate to false
  148. if ($this.val() == 0) {
  149. $('#' + $this.data('describeby')).text('Please Choose an Item Category').show();
  150. validate = false;
  151. return;
  152. } else {
  153. $('#' + $this.data('describeby')).hide();
  154. return;
  155. }
  156. }
  157. }
  158. });
  159. //Return the validate
  160. return validate;
  161. }
  162.  
  163. function Form_Validate() {
  164. //When this is called by the onClientClick button it will send a function call to validate all fields and return the update
  165. var validated = Custom_Validate();
  166. //If the update is true, then it will continue to send the ajax call
  167. if (validated) {
  168. //Make the postData look similar to the meal food view model
  169. var postData = {
  170. MealName: $('#ContentPlaceHolder1_AddMealName').val(),
  171. MealCategoryID: $('#ContentPlaceHolder1_AddCategoryName').val(),
  172. FoodID: []
  173. };
  174. //Search through each of thsese IDS
  175. $('#addItemDropdownList_1, #addItemDropdownList_2, #addItemDropdownList_3, #addItemDropdownList_4, #addItemDropdownList_5')
  176. .each(function () {
  177. //On each ID push the value with aradix of 10
  178. postData.FoodID.push(parseInt($(this).val(), 10));
  179. });
  180. //Send the ajax call through AddMeal.aspx
  181. $.ajax({
  182. url: "http://localhost:63591/AddMeal.aspx",
  183. method: 'POST',
  184. data: { data: JSON.stringify(postData) }
  185. });
  186. }
  187. //This will return false on all, to avoid a double submissions of viewstate
  188. return false;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement