Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. <script>
  2.  
  3. export default {
  4. props: {
  5.  
  6. },
  7. mounted () {
  8.  
  9. },
  10. methods: {
  11.  
  12. /*
  13. * Reset previous validation error each time open a new Modal form
  14. * */
  15. resetValidation() {
  16. this.$validator.reset();
  17. },
  18.  
  19. showCreateForm() {
  20.  
  21. this.current_action = 'create';
  22.  
  23. // reset form to initial value
  24.  
  25. this.item_data = this.getDefaultItemData();
  26.  
  27. this.resetValidation();
  28.  
  29. // set submit label and modal title
  30. this.submit_label = 'Create';
  31. this.modal_title = 'New Invoice Item';
  32.  
  33. this.openModal();
  34. },
  35.  
  36. showEditForm(index) {
  37.  
  38. this.current_action = 'edit';
  39.  
  40. this.current_index = index;
  41.  
  42. // get item data from component invoice items array
  43.  
  44. let copied_item_data = Object.assign({}, this.component_invoice_items[index]);
  45.  
  46. this.item_data = copied_item_data;
  47.  
  48. // reset validation
  49.  
  50. this.resetValidation();
  51.  
  52. // set submit label and modal title
  53. this.submit_label = 'Update';
  54. this.modal_title = 'Edit Invoice Item';
  55.  
  56. this.openModal();
  57. },
  58.  
  59. openModal() {
  60. this.$refs.invoiceItemModal.show();
  61. },
  62.  
  63. closeModal() {
  64. this.$refs.invoiceItemModal.hide();
  65. },
  66.  
  67. handleOk(event) {
  68.  
  69. // prevent modal from closing
  70. // event.preventDefault();
  71.  
  72. this.handleSubmit(event);
  73. },
  74.  
  75. /*
  76. * Handle modal submission for New and Edit
  77. * */
  78. handleSubmit(e) {
  79.  
  80. this.component_submitted = true;
  81.  
  82. this.$validator.validate().then(valid => {
  83. if (valid) {
  84.  
  85. if (this.current_action === 'create') {
  86. this.store();
  87. }
  88. else if (this.current_action === 'edit') {
  89. this.update();
  90. }
  91. }
  92. else {
  93. // scroll to top
  94.  
  95. window.scrollTo(0,0);
  96. }
  97. });
  98.  
  99.  
  100. },
  101.  
  102. /*
  103. * New row created by v-for must be re-init for tooltip to work
  104. * */
  105.  
  106. reinitTooltip() {
  107. Vue.nextTick(function () {
  108. $('[data-toggle="tooltip"]').tooltip();
  109. });
  110. },
  111.  
  112. store() {
  113.  
  114. this.component_invoice_items.push(this.item_data);
  115.  
  116. this.reinitTooltip();
  117.  
  118. this.closeModal();
  119.  
  120. this.updateParent();
  121. },
  122.  
  123. update() {
  124.  
  125. Vue.set(this.component_invoice_items, this.current_index, this.item_data);
  126.  
  127. this.closeModal();
  128.  
  129. this.updateParent();
  130.  
  131. },
  132.  
  133. destroy() {
  134.  
  135. let deleted_item = this.component_invoice_items[this.current_index];
  136.  
  137. // remove item from component_invoice_items array
  138.  
  139. this.$delete(this.component_invoice_items, this.current_index);
  140.  
  141. // add the item into deleted_invoice_items array
  142.  
  143. this.deleted_invoice_items.push(deleted_item);
  144.  
  145. this.updateParent();
  146.  
  147. this.updateParentDeletedItems();
  148. },
  149.  
  150. /*
  151. * Update the parent component the value of latest component_invoice_items after Create / Update / Delete operation
  152. * */
  153. updateParent() {
  154. this.$emit('update:invoice_items', this.component_invoice_items);
  155. },
  156.  
  157. /*
  158. * Update the parent component the value of latest deleted invoice items id
  159. * */
  160. updateParentDeletedItems() {
  161.  
  162. this.$emit('update:deleted_invoice_item_ids', this.deleted_invoice_item_ids);
  163. }
  164. }
  165. }
  166. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement