angular-json-table.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { CommonModule } from '@angular/common';
  2. import { Component, EventEmitter, Input, Output, NgModule } from '@angular/core';
  3. import { FormsModule } from '@angular/forms';
  4. /**
  5. * @fileoverview added by tsickle
  6. * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
  7. */
  8. class DataTableComponent {
  9. constructor() {
  10. this.deleteRow = new EventEmitter();
  11. this.updateRow = new EventEmitter();
  12. this.data = {};
  13. this.displayed = [];
  14. this.perpage = 10;
  15. this.page = 1;
  16. this.checked = [];
  17. this.checkAll = false;
  18. this.dialogue = false;
  19. this.dialogeData = {};
  20. }
  21. /**
  22. * @return {?}
  23. */
  24. ngOnInit() {
  25. this.processData();
  26. }
  27. /**
  28. * @param {?} e
  29. * @param {?} id
  30. * @return {?}
  31. */
  32. selected(e, id) {
  33. if (e.target.checked) {
  34. this.checked.push(id);
  35. }
  36. }
  37. /**
  38. * @return {?}
  39. */
  40. deleteSelected() {
  41. if (this.checked.length > 0) {
  42. this.deleteRow.emit(this.checked);
  43. this.dataSource = this.dataSource.filter((value, index, array) => {
  44. return !this.checked.includes(value.id);
  45. });
  46. this.checked = [];
  47. this.paginate(this.page);
  48. }
  49. }
  50. /**
  51. * @return {?}
  52. */
  53. totalItems() {
  54. if (this.total > this.perpage) {
  55. return Math.ceil(this.total / this.perpage);
  56. }
  57. else {
  58. return 1;
  59. }
  60. }
  61. /**
  62. * @param {?} page
  63. * @return {?}
  64. */
  65. paginate(page) {
  66. /** @type {?} */
  67. const start = (this.perpage * page) - this.perpage;
  68. /** @type {?} */
  69. const end = (this.perpage * page);
  70. // console.log(start, end);
  71. this.data['data'] = this.dataSource.slice(start, end);
  72. // console.log(this.data);
  73. }
  74. /**
  75. * @param {?} perPage
  76. * @return {?}
  77. */
  78. pageOnChange(perPage) {
  79. this.perpage = perPage;
  80. this.page = 1;
  81. //console.log(perPage);
  82. this.paginate(this.page);
  83. }
  84. /**
  85. * @return {?}
  86. */
  87. previousPage() {
  88. if (this.page > 1) {
  89. this.page--;
  90. }
  91. this.paginate(this.page);
  92. }
  93. /**
  94. * @return {?}
  95. */
  96. nextPage() {
  97. if (this.page + 1 <= this.totalItems()) {
  98. this.page++;
  99. }
  100. this.paginate(this.page);
  101. }
  102. /**
  103. * @param {?} id
  104. * @return {?}
  105. */
  106. showUpdate(id) {
  107. this.dialogue = true;
  108. // console.log('Showing the updates');
  109. this.data.data.forEach((value) => {
  110. if (value.id === id) {
  111. this.dialogeData = value;
  112. }
  113. });
  114. }
  115. /**
  116. * @return {?}
  117. */
  118. closeDialogue() {
  119. this.dialogue ? this.dialogue = false : this.dialogue = true;
  120. this.dialogeData = {};
  121. }
  122. /**
  123. * @param {?} dialogeData
  124. * @return {?}
  125. */
  126. submitUpdateRow(dialogeData) {
  127. this.updateRow.emit(dialogeData);
  128. }
  129. /**
  130. * @return {?}
  131. */
  132. processData() {
  133. if (this.dataSource && this.dataSource.length > 0) {
  134. this.total = this.dataSource.length;
  135. if (this.headers.thead && this.headers.thead.length > 0) {
  136. this.data['headers'] = this.headers.thead;
  137. this.displayed = this.headers.displayed;
  138. }
  139. else {
  140. console.warn('No headers data for table provided');
  141. }
  142. this.paginate(this.page);
  143. }
  144. else {
  145. console.warn('No data for table provided');
  146. }
  147. }
  148. }
  149. DataTableComponent.decorators = [
  150. { type: Component, args: [{
  151. selector: 'json-table',
  152. template: "<div *ngIf=\"data\" class=\"data-table\">\n <div *ngIf=\"false\">\n <input type=\"text\" class=\"search\" placeholder=\"Search for details\">\n </div>\n <table>\n <thead>\n <tr>\n <th style=\"width: 4%\">&nbsp;</th>\n <th style=\"width: 4%\">#</th>\n <th *ngFor=\"let header of data.headers\" style=\"text-align:left\">{{header}}</th>\n <th *ngIf=\"update\">&nbsp;</th>\n </tr>\n </thead>\n <tbody>\n\n <tr *ngFor=\"let row of data.data; let i = index\">\n <td><input type=\"checkbox\" [checked]=\"checkAll\" (change)=\"selected($event,row.id)\"></td>\n <td>{{ i+1 }}</td>\n <td *ngFor=\"let attr of displayed\" >{{row[attr]}}</td>\n <td *ngIf=\"update\" style=\"text-align:center\">\n <span (click)=\"showUpdate(row.id)\">\uD83D\uDD89</span>\n </td>\n </tr>\n\n </tbody>\n <tfoot></tfoot>\n </table>\n\n <div class=\"footer\">\n <div class=\"delete\">\n <button (click)=\"deleteSelected()\">Delete {{checked.length}} rows</button>\n </div>\n\n <div class=\"spacer\">\n\n </div>\n\n <div class=\"data-pagination\">\n <select (change)=\"pageOnChange(device.value)\" #device>\n <option value=\"10\">10</option>\n <option value=\"15\">15</option>\n <option value=\"20\">20</option>\n <option value=\"100\">100</option>\n </select>\n | Showing {{page}} of {{totalItems()}} |\n <button class=\"link-button\" (click)=\"previousPage()\">Previous</button>\n -\n <button class=\"link-button\" (click)=\"nextPage()\">Next</button>\n </div>\n </div>\n\n\n <div *ngIf=\"update && dialogue\" class=\"update\" (click)=\"closeDialogue()\">\n\n <div class=\"update-box\" (click)=\"$event.stopPropagation()\">\n <h2>Edit the Row</h2>\n <form #customForm=\"ngForm\" novalidate (ngSubmit)=\"submitUpdateRow(dialogeData); closeDialogue()\">\n <div class=\"f-x\">\n <ng-container *ngFor=\"let attr of displayed; let i = index\">\n <div *ngIf=\"attr!='id' \" class=\"f-y\">\n <label>{{data.headers[i]}}</label>\n <input type=\"text\" [value]=\"dialogeData[attr]\" [placeholder]=\"attr\" [name]=\"attr\"\n [(ngModel)]=\"dialogeData[attr]\">\n </div>\n </ng-container>\n </div>\n <footer>\n <br>\n <button type=\"submit\">Update the row</button>\n <button type=\"button\" (click)=\"closeDialogue()\">Cancel</button>\n </footer>\n </form>\n </div>\n </div>\n\n</div>\n",
  153. styles: [".data-table{box-shadow:0 5px 5px -3px rgba(0,0,0,.2),0 8px 10px 1px rgba(0,0,0,.14),0 3px 14px 2px rgba(0,0,0,.12);margin:10px;border-radius:8px;background:#fff;padding-top:8px}.data-table .search{width:100%;height:42px;border:none;outline:0;text-align:left;border-bottom:1px solid rgba(0,0,0,.12)}.data-table table{display:table;border-collapse:separate;border-spacing:2px;border-color:grey;width:100%;background:#fff}.data-table table tbody td:first-of-type,.data-table table tbody th:first-of-type,.data-table table thead td:first-of-type,.data-table table thead th:first-of-type{padding-left:24px!important;padding-right:24px!important}.data-table table thead{display:table-header-group;vertical-align:middle;border-color:inherit}.data-table table thead tr{height:56px}.data-table table tbody{display:table-row-group;vertical-align:middle;border-color:inherit}.data-table table tbody tr{height:48px;color:rgba(0,0,0,.87)}.data-table table th{padding:0;color:rgba(0,0,0,.54);border-bottom:1px solid rgba(0,0,0,.12)}.data-table table tfoot{display:table-footer-group;vertical-align:middle;border-color:inherit}.data-table .footer{display:flex;height:40px;border-top:1px solid rgba(0,0,0,.12)}.data-table .footer .spacer{flex-grow:1}.data-table .footer .delete{width:120px;background:0 0}.data-table .footer .delete button{height:80%;background:0 0;border:none;outline:0;cursor:pointer;transition:.25s linear;border-radius:4px;margin-left:5px;margin-top:4px}.data-table .footer .delete button:hover{background-color:#fbab33;color:#fff}.data-table .footer .data-pagination{padding-right:20px;padding-top:4px}.data-table .footer .data-pagination select{outline:0;background:0 0;padding:4px}.data-table .footer .data-pagination .link-button{border:none;outline:0;background:0 0}.data-table .footer .data-pagination .link-button:hover{text-decoration:underline}.data-table .update{transition:.5s linear;position:fixed;height:100vh;width:100vw;background:rgba(212,212,212,.51);top:0;left:0;display:flex;align-items:center;justify-content:center}.data-table .update .update-box{box-shadow:0 5px 5px -3px rgba(0,0,0,.2),0 8px 10px 1px rgba(0,0,0,.14),0 3px 14px 2px rgba(0,0,0,.12);background:#fff;padding:8px;border-radius:4px;width:250px}.data-table .update .f-x{display:flex;flex-direction:column}.data-table .update .f-y{display:flex;flex-direction:column;margin-top:6px}.data-table .update input[type=text]{margin-right:4px;padding:4px}"]
  154. }] }
  155. ];
  156. /** @nocollapse */
  157. DataTableComponent.ctorParameters = () => [];
  158. DataTableComponent.propDecorators = {
  159. dataSource: [{ type: Input }],
  160. headers: [{ type: Input }],
  161. update: [{ type: Input }],
  162. deleteRow: [{ type: Output }],
  163. updateRow: [{ type: Output }]
  164. };
  165. /**
  166. * @fileoverview added by tsickle
  167. * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
  168. */
  169. class JSONTableModule {
  170. }
  171. JSONTableModule.decorators = [
  172. { type: NgModule, args: [{
  173. imports: [
  174. CommonModule,
  175. FormsModule
  176. ],
  177. declarations: [DataTableComponent],
  178. exports: [DataTableComponent]
  179. },] }
  180. ];
  181. /**
  182. * @fileoverview added by tsickle
  183. * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
  184. */
  185. /**
  186. * @fileoverview added by tsickle
  187. * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
  188. */
  189. export { JSONTableModule, DataTableComponent as ɵa };
  190. //# sourceMappingURL=angular-json-table.js.map