Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. export class HomeComponent implements OnInit {
  2. survey: FormGroup;
  3. user: User[] = [
  4. {
  5. id: 1,
  6. question: 'Question 1',
  7. choice: [
  8. {
  9. ChoceId: 1,
  10. Value: '1choice 1'
  11. },
  12. {
  13. ChoceId: 2,
  14. Value: '1choice 2'
  15. },
  16. {
  17. ChoceId: 3,
  18. Value: '1choice 3'
  19. },
  20. {
  21. ChoceId: 4,
  22. Value: '1choice 4'
  23. }
  24.  
  25. ]
  26. }, {
  27. id: 2,
  28. question: 'Question 2',
  29. choice: [
  30. {
  31. ChoceId: 1,
  32. Value: '2choice 1'
  33. },
  34. {
  35. ChoceId: 2,
  36. Value: '2choice 2'
  37. },
  38. {
  39. ChoceId: 3,
  40. Value: '2choice 3'
  41. }
  42. ]
  43. }, {
  44. id: 3,
  45. question: 'Question 3',
  46. choice: [
  47. {
  48. ChoceId: 1,
  49. Value: '3choice 1'
  50. },
  51. {
  52. ChoceId: 2,
  53. Value: '3choice 2'
  54. },
  55. {
  56. ChoceId: 3,
  57. Value: '3choice 3'
  58. }
  59. ]
  60. }
  61. , {
  62. id: 4,
  63. question: 'Question 4',
  64. choice: [
  65. {
  66. ChoceId: 1,
  67. Value: '4choice 1'
  68. },
  69. {
  70. ChoceId: 2,
  71. Value: '4choice 2'
  72. },
  73. {
  74. ChoceId: 3,
  75. Value: '4choice 3'
  76. },
  77. {
  78. ChoceId: 4,
  79. Value: '4choice 4'
  80. }
  81. ]
  82. }
  83. , {
  84. id: 5,
  85. question: 'Question 5',
  86. choice: [
  87. {
  88. ChoceId: 1,
  89. Value: '5choice 1'
  90. },
  91. {
  92. ChoceId: 2,
  93. Value: '5choice 2'
  94. },
  95. {
  96. ChoceId: 3,
  97. Value: '5choice 3'
  98. },
  99. {
  100. ChoceId: 4,
  101. Value: '5choice 4'
  102. }
  103. ]
  104. },
  105. {
  106. id: 6,
  107. question: 'Question 6',
  108. choice: [
  109. {
  110. ChoceId: 1,
  111. Value: '6choice 1'
  112. },
  113. {
  114. ChoceId: 2,
  115. Value: '6choice 2'
  116. },
  117. {
  118. ChoceId: 3,
  119. Value: '6choice 3'
  120. },
  121. {
  122. ChoceId: 4,
  123. Value: '6choice 4'
  124. }
  125. ]
  126. },
  127. {
  128. id: 7,
  129. question: 'Question 7',
  130. choice: [
  131. {
  132. ChoceId: 1,
  133. Value: '7choice 1'
  134. },
  135. {
  136. ChoceId: 2,
  137. Value: '7choice 2'
  138. },
  139. {
  140. ChoceId: 3,
  141. Value: '7choice 3'
  142. },
  143. {
  144. ChoceId: 4,
  145. Value: '7choice 4'
  146. }
  147. ,
  148. {
  149. ChoceId: 5,
  150. Value: '7choice 4'
  151. }
  152. ]
  153. }
  154. ]
  155.  
  156. constructor() {
  157.  
  158. }
  159.  
  160. ngOnInit() {
  161.  
  162.  
  163. this.survey = new FormGroup({
  164. sections: new FormArray([
  165. this.initSection(),
  166. ]),
  167. });
  168.  
  169. for (let i = 0; i < this.user.length; i++) {
  170.  
  171. this.addQuestion(0, this.user[i].question)
  172. this.add(0, i+1,this.user[i].choice);
  173. this.removeOption(0, i, 0)
  174. }
  175. this.removeQuestion(0);
  176. }
  177.  
  178. initSection() {
  179. return new FormGroup({
  180. questions: new FormArray([
  181. this.initQuestion('questionTitle')
  182. ])
  183. });
  184. }
  185.  
  186. initQuestion(val: string) {
  187. return new FormGroup({
  188. questionTitle: new FormControl(val),
  189. options: new FormArray([
  190. this.initOptions('')
  191. ])
  192. });
  193. }
  194.  
  195. initOptions(val: string) {
  196. return new FormGroup({
  197. optionTitle: new FormControl(val)
  198. });
  199. }
  200.  
  201. addSection() {
  202. const control = <FormArray>this.survey.get('sections');
  203. control.push(this.initSection());
  204. }
  205.  
  206. addQuestion(j, val: string) {
  207. console.log(j);
  208. const control = <FormArray>this.survey.get('sections').controls[j].get('questions');
  209. control.push(this.initQuestion(val));
  210.  
  211. }
  212.  
  213. add(i, j, choice: Choices[]) {
  214.  
  215. const control = <FormArray>this.survey.get('sections').controls[i].get('questions').controls[j].get('options');
  216. if (choice) {
  217. for (j = i; j < choice.length; j++) {
  218. control.push(this.initOptions(choice[j] .Value));
  219. }
  220. }else{
  221. control.push(this.initOptions(''));
  222. }
  223. }
  224.  
  225. getSections(form) {
  226. //console.log(form.get('sections').controls);
  227. return form.controls.sections.controls;
  228. }
  229. getQuestions(form) {
  230. //console.log(form.controls.questions.controls);
  231. return form.controls.questions.controls;
  232. }
  233. getOptions(form) {
  234. //console.log(form.get('options').controls);
  235. return form.controls.options.controls;
  236.  
  237. }
  238.  
  239. removeQuestion(j) {
  240. const control = <FormArray>this.survey.get('sections').controls[j].get('questions');
  241. control.removeAt(j);
  242. }
  243.  
  244. removeSection(i) {
  245. const control = <FormArray>this.survey.get('sections');
  246. control.removeAt(i);
  247.  
  248. }
  249.  
  250. removeOption(i, j, k) {
  251. // debugger;
  252. console.log(i, j, k);
  253. const control = <FormArray>this.survey.get(['sections', i, 'questions', j, 'options']); // also try this new syntax
  254. control.removeAt(k);
  255. }
  256.  
  257. remove(i, j) {
  258. const control = <FormArray>this.survey.get(['sections', i, 'questions', j, 'options']);
  259. control.removeAt(0);
  260. control.controls = [];
  261. }
  262.  
  263. onSubmit(form:NgForm) {
  264. debugger;
  265.  
  266. console.log(this.survey.value.optionTitle);
  267. console.log(form);
  268.  
  269. }
  270.  
  271. }
  272.  
  273. <form [formGroup]="survey" novalidate (ngSubmit)="onSubmit(survey)">
  274. <!---Survey Section -->
  275. <div formArrayName="sections">
  276. <div class="section-container" *ngFor="let section of getSections(survey); let i = index">
  277. <div class="ui raised segments" [formGroupName]="i">
  278. <h4 class="ui header">User likes and choices</h4>
  279. <!-- <input type="text" placeholder="Untitled form" formControlName="sectionTitle">
  280. <input type="text" placeholder="Form Description" formControlName="sectionDescription"> -->
  281. <!-- Question segment -->
  282. <!---Survey Section -->
  283. <hr>
  284. <div class="question-container" formArrayName="questions">
  285. <div [formGroupName]="j" *ngFor="let question of getQuestions(section); let j = index">
  286. <input type="text" placeholder="Untitled Question" formControlName="questionTitle">
  287. <!-- <select formControlName="questionType">
  288. <option></option>
  289. <option>Check Boxes</option>
  290. <option>Free Text</option>
  291. <option>Rating</option>
  292. <option>Date</option>
  293. <option>Time</option>
  294. </select> -->
  295.  
  296. <div *ngIf="survey.errors" class="alert alert-danger">
  297. {{ survey.errors }}
  298. </div>
  299. <div>
  300. <a (click)="add(i,j)">Add_Option</a>||
  301. <a (click)="remove(i,j)">Remove_whole_options</a>
  302. </div>
  303. <!-- Option Addation -->
  304. <div formArrayName="options">
  305. <div [formGroupName]="k" *ngFor="let option of getOptions(question); let k=index">
  306.  
  307. <input type="radio" id="{{ 'acceptable' + i}}" >
  308. <input type="text" placeholder="Option 1" formControlName="optionTitle">
  309.  
  310.  
  311. <!-- <a (click)="remove(i,j)">Option</a> -->
  312. <span *ngIf="getOptions(question).length > 1" (click)="removeOption(i,j,k)">Remove_Option</span>
  313. </div>
  314. <!-- End Option Addition -->
  315. <!-- Option Addtion -->
  316. <!-- End Option Addition -->
  317.  
  318. </div><br>
  319. <hr>
  320. <div>
  321. <a (click)="addQuestion(i)">Add Question...</a>
  322. <span *ngIf="getQuestions(section).length > 1" (click)="removeQuestion(i)">Remove Question</span>
  323. </div>
  324. </div><br>
  325. </div>
  326. <!-- End Question -->
  327.  
  328. </div>
  329. <br>
  330. <button (click)="addSection()" class="point">Add Section </button>
  331. <span *ngIf="getSections(survey).length > 1" (click)="removeSection(i)">Remove Section</span>
  332. </div>
  333. </div>
  334. <!-- End Section -->
  335. <button type="submit">Get-Link</button>
  336. </form>
  337.  
  338. <pre> {{survey.value | json}} </pre>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement