Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grabbing data from multiple select #351

Closed
rockomatthews opened this issue Aug 13, 2017 · 14 comments
Closed

Grabbing data from multiple select #351

rockomatthews opened this issue Aug 13, 2017 · 14 comments

Comments

@rockomatthews
Copy link

rockomatthews commented Aug 13, 2017

I'm using angular4 and have my user register has multiple select filed. Its works and looks good, but I don't know how to capture the checked options. How would I group them into an array that is empty with the user I create? Here's my user-new.component.html and my user-new.component.ts...

I am guessing I should declare an empty array in the component to push the selected options to but I'm not sure... Any help would be great. Thanks!

user-new.component.html

<h4>UserNewComponent</h4>
<div class="row">
  <form materialize class="col s12" (submit)="create()">
    <div class="row">
      <div class="input-field col s12">
        <input id="username" name="username" type="text" class="validate" [(ngModel)]="newUser.username">
        <label for="username">USERNAME?</label>
      </div>
    </div>
    <div class="row">
      <div class="input-field col s12">
        <textarea id="textarea1" name="motto" class="materialize-textarea" [(ngModel)]="newUser.motto"></textarea>
        <label for="textarea1">YOUR MOTTO. THE REASON YOU ARE HERE?</label>
      </div>
    </div>
    <div class="input-field col s12">
      <select multiple>
        <option value="" disabled selected>GENRES THAT YOU FANCY</option>
        <option value="acoustic">ACOUSTIC</option>
        <option value="alternative_rock">ALTERNATIVE ROCK</option>
        <option value="blues">BLUES</option>
        <option value="classic_rock">CLASSIC ROCK</option>
        <option value="classical">CLASSICAL</option>
        <option value="comedy">COMEDY</option>
        <option value="country">COUNTRY</option>
        <option value="electronic">ELECTRONIC</option>
        <option value="experiemntal">EXPERIMENTAL</option>
        <option value="jazz">JAZZ</option>
        <option value="metal">METAL</option>
        <option value="pop">POP</option>
        <option value="raggae">RAGGAE</option>
        <option value="rap">RAP</option>
        <option value="rock">ROCK</option>
        <option value="r_and_b">R&B</option>
      </select>
      <label for="genres">GENRE(S)</label>
    </div>
    <div class="row">
        <div class="input-field col s12">
          <input id="password" name="password" type="password" class="validate" [(ngModel)]="newUser.password">
          <label for="password">PASSWORD</label>
        </div>
    </div>
    <div class="row">
      <div class="input-field col s12">
        <input id="confirmPassword" name="confirmPassword" type="password" class="validate" [(ngModel)]="newUser.confirmPassword">
        <label for="confirmPassword">CONFIRM THIS</label>
      </div>
    </div>
    <input type="submit" value="SIGN UP" class="waves-effect waves btn-large red col s12">
  </form>
</div>

user-new.component.ts

import { User } from "./../user";
import { Component, OnInit, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-user-new',
  templateUrl: './user-new.component.html',
  styleUrls: ['./user-new.component.css']
})
export class UserNewComponent implements OnInit {
  newUser = new User();
  @Output() createNewUserEvent = new EventEmitter();

  constructor() { }

  ngOnInit() {
  }

  create(){
    // call server to save
    this.createNewUserEvent.emit(this.newUser);
    this.newUser = new User();
  }
  
}
@rubyboy
Copy link
Contributor

rubyboy commented Aug 13, 2017

Try this:

<select name="multiSelect" multiple materialize="material_select" [(ngModel)]="multiSelectedOptions" [materializeSelectOptions]="selectOptions">
  <option value="" disabled selected>Choose your option</option>
  <option *ngFor="let option of selectOptions" [value]="option.value">{{option.name}}</option>
</select>
<label>Materialize Multi Select ({{multiSelectedOptions}})</label>
multiSelectedOptions = "";
selectOptions = [
          {value:1,name:"Option 1"},
          {value:2,name:"Option 2"},
          {value:3,name:"Option 3"}
        ]

@rockomatthews
Copy link
Author

rockomatthews commented Aug 15, 2017

thanks for the response. this is now my error message now... I installed using the instructions found in the tutorial and am using Angular CLI

core.es5.js:1020 ERROR Error: Couldn't find materialize function ''material_select' on element or the global Materialize object.
    at HTMLDocument.<anonymous> (materialize-directive.js:261)
    at n (px-jquery-1.7.1.min.js:2)
    at Object.add (px-jquery-1.7.1.min.js:2)
    at init.ready (px-jquery-1.7.1.min.js:2)
    at MaterializeDirective.webpackJsonp.../../../../angular2-materialize/dist/materialize-directive.js.MaterializeDirective.performLocalElementUpdates (materialize-directive.js:228)
    at materialize-directive.js:85
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)
    at Object.onInvokeTask (core.es5.js:3881)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
    at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask (zone.js:191)

I slightly altered the code you gave me to accommodate my User being created. This is now my user-new.component.html followed by my user-new.component.ts.

<select name="multiSelect" multiple materialize="material_select" [(ngModel)]="newUser.genres" [materializeSelectOptions]="selectOptions">
        <option value="" disabled selected>Choose your option</option>
        <option *ngFor="let option of selectOptions" [value]="option.value">{{option.name}}</option>
      </select>
      <label>Materialize Multi Select ({{newUser.genres}})</label>
import { User } from "./../user";
import { Component, OnInit, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-user-new',
  templateUrl: './user-new.component.html',
  styleUrls: ['./user-new.component.css']
})
export class UserNewComponent implements OnInit {
  newUser = new User();
  multiSelectedOptions = "";
  selectOptions = [
            {value:1,name:"ACOUSTIC"},
            {value:2,name:"ALTERNATIVE ROCK"},
            {value:3,name:"BLUES"},
            {value:4,name:"CLASSIC ROCK"},
            {value:5,name:"CLASSICAL"},
            {value:6,name:"COMEDY"},
            {value:7,name:"COUNTRY"},
            {value:8,name:"ELECTRONIC"},
            {value:10,name:"EXPERIMENTAL"},
            {value:11,name:"JAZZ"},
            {value:12,name:"METAL"},
            {value:13,name:"POP"},
            {value:14,name:"RAGGAE"},
            {value:15,name:"ROCK"},
            {value:2,name:"R&B"}, 
          ];
  @Output() createNewUserEvent = new EventEmitter();

  constructor() { }

  ngOnInit() {
  }

  create(){
    // call server to save
    this.createNewUserEvent.emit(this.newUser);
    this.newUser = new User();
  }
  
}
  

@rockomatthews
Copy link
Author

rockomatthews commented Aug 15, 2017

@rubyboy (sorry am I supposed to include this. If not I won't in future) beside the error. it seems like its working. I'm wondering if I even need to worry about it!

@rockomatthews
Copy link
Author

rockomatthews commented Aug 16, 2017

my project is right here if that helps.
https://github.com/rockomatthews/banded.git
Everything works fine. Until I click con an option in the multiple select... Then the error pops up:

core.es5.js:1020 ERROR Error: Couldn't find materialize function ''material_select' on element or the global Materialize object.
    at HTMLDocument.<anonymous> (materialize-directive.js:261)

I installed with the Angular CLI instructions... Any help would be awesome!

@rubyboy
Copy link
Contributor

rubyboy commented Aug 17, 2017

@rockomatthews I've downloaded your project, npm installed and served. All looking great, with multi selected working as expected. What am I missing?
image

@rockomatthews
Copy link
Author

@rubyboy It works for me as well... at least to me it seems its working... but when I click on the initial option, I get this error message
screen shot 2017-08-17 at 1 51 35 am

I just tried the tooltip and that seems to work...

@rockomatthews
Copy link
Author

Is it that I am not importing MaterializeDirective anywhere? I feel like I keep coming across examples that contain this import within the app.module.ts but it isn't necessary import MaterializeDirective for CLI, right?

import { MaterializeDirective } from 'angular2-materialize';
with

@NgModule({
  declarations: [
    ...
    ...
    MaterializeDirective
    ...
    ...
  ],

@rockomatthews
Copy link
Author

@rubyboy can you see anywhere that might be causing this for me and not you? I want to make sure this isn't an issue before I move forward.

@rockomatthews
Copy link
Author

@rubyboy sorry to bug but I think I have advanced. I cloned the project I provided a link to and reinstalled everything in a new folder. Now the material_select is flashing no error but it is closing after every time I select an option... is this a a familiar issue to you?

@rockomatthews
Copy link
Author

UPDATE
screenflow

its very strange. If I am one user signed into chrome on my main google account, it works perfectly yet flashes this error and reloads all the time without intention to reload. This is the error.

core.es5.js:1020 ERROR Error: Couldn't find materialize function ''material_select' on element or the global Materialize object.
    at HTMLDocument.<anonymous> (materialize-directive.js:261)
    at n (px-jquery-1.7.1.min.js:2)
    at Object.add (px-jquery-1.7.1.min.js:2)
    at init.ready (px-jquery-1.7.1.min.js:2)
    at MaterializeDirective.webpackJsonp.../../../../angular2-materialize/dist/materialize-directive.js.MaterializeDirective.performLocalElementUpdates (materialize-directive.js:228)
    at materialize-directive.js:85
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)
    at Object.onInvokeTask (core.es5.js:3881)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
    at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask (zone.js:191)
  | defaultErrorLogger | @ | core.es5.js:1020
-- | -- | -- | --
  | webpackJsonp.../../../core/@angular/core.es5.js.ErrorHandler.handleError | @ | core.es5.js:1080
  | next | @ | core.es5.js:4503
  | schedulerFn | @ | core.es5.js:3635
  | webpackJsonp.../../../../rxjs/Subscriber.js.SafeSubscriber.__tryOrUnsub | @ | Subscriber.js:238
  | webpackJsonp.../../../../rxjs/Subscriber.js.SafeSubscriber.next | @ | Subscriber.js:185
  | webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber._next | @ | Subscriber.js:125
  | webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber.next | @ | Subscriber.js:89
  | webpackJsonp.../../../../rxjs/Subject.js.Subject.next | @ | Subject.js:55
  | webpackJsonp.../../../core/@angular/core.es5.js.EventEmitter.emit | @ | core.es5.js:3621
  | (anonymous) | @ | core.es5.js:3912
  | webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke | @ | zone.js:391
  | webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run | @ | zone.js:141
  | webpackJsonp.../../../core/@angular/core.es5.js.NgZone.runOutsideAngular | @ | core.es5.js:3844
  | onHandleError | @ | core.es5.js:3912
  | webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.handleError | @ | zone.js:395
  | webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask | @ | zone.js:194
  | webpackJsonp.../../../../zone.js/dist/zone.js.ZoneTask.invokeTask | @ | zone.js:498
  | ZoneTask.invoke | @ | zone.js:487
  | timer | @ | zone.js:1829
  | setTimeout (async) |   |  
  | scheduleTask | @ | zone.js:1839
  | webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.scheduleTask | @ | zone.js:410
  | onScheduleTask | @ | zone.js:300
  | webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.scheduleTask | @ | zone.js:404
  | webpackJsonp.../../../../zone.js/dist/zone.js.Zone.scheduleTask | @ | zone.js:235
  | webpackJsonp.../../../../zone.js/dist/zone.js.Zone.scheduleMacroTask | @ | zone.js:258
  | (anonymous) | @ | zone.js:1865
  | proto.(anonymous function) | @ | zone.js:1227
  | webpackJsonp.../../../../angular2-materialize/dist/materialize-directive.js.MaterializeDirective.ngOnChanges | @ | materialize-directive.js:85
  | checkAndUpdateDirectiveInline | @ | core.es5.js:10840
  | checkAndUpdateNodeInline | @ | core.es5.js:12339
  | checkAndUpdateNode | @ | core.es5.js:12278
  | debugCheckAndUpdateNode | @ | core.es5.js:13139
  | debugCheckDirectivesFn | @ | core.es5.js:13080
  | View_UserNewComponent_0._co | @ | UserNewComponent.html:18
  | debugUpdateDirectives | @ | core.es5.js:13065
  | checkAndUpdateView | @ | core.es5.js:12245
  | callViewAction | @ | core.es5.js:12610
  | execComponentViewsAction | @ | core.es5.js:12542
  | checkAndUpdateView | @ | core.es5.js:12251
  | callViewAction | @ | core.es5.js:12610
  | execComponentViewsAction | @ | core.es5.js:12542
  | checkAndUpdateView | @ | core.es5.js:12251
  | callViewAction | @ | core.es5.js:12610
  | execComponentViewsAction | @ | core.es5.js:12542
  | checkAndUpdateView | @ | core.es5.js:12251
  | callWithDebugContext | @ | core.es5.js:13465
  | debugCheckAndUpdateView | @ | core.es5.js:13005
  | webpackJsonp.../../../core/@angular/core.es5.js.ViewRef_.detectChanges | @ | core.es5.js:10176
  | (anonymous) | @ | core.es5.js:4807
  | webpackJsonp.../../../core/@angular/core.es5.js.ApplicationRef_.tick | @ | core.es5.js:4807
  | (anonymous) | @ | core.es5.js:4684
  | webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke | @ | zone.js:391
  | onInvoke | @ | core.es5.js:3890
  | webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke | @ | zone.js:390
  | webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run | @ | zone.js:141
  | webpackJsonp.../../../core/@angular/core.es5.js.NgZone.run | @ | core.es5.js:3821
  | next | @ | core.es5.js:4684
  | schedulerFn | @ | core.es5.js:3635
  | webpackJsonp.../../../../rxjs/Subscriber.js.SafeSubscriber.__tryOrUnsub | @ | Subscriber.js:238
  | webpackJsonp.../../../../rxjs/Subscriber.js.SafeSubscriber.next | @ | Subscriber.js:185
  | webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber._next | @ | Subscriber.js:125
  | webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber.next | @ | Subscriber.js:89
  | webpackJsonp.../../../../rxjs/Subject.js.Subject.next | @ | Subject.js:55
  | webpackJsonp.../../../core/@angular/core.es5.js.EventEmitter.emit | @ | core.es5.js:3621
  | checkStable | @ | core.es5.js:3855
  | onLeave | @ | core.es5.js:3934
  | onInvokeTask | @ | core.es5.js:3884
  | webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask | @ | zone.js:423
  | webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask | @ | zone.js:191
  | webpackJsonp.../../../../zone.js/dist/zone.js.ZoneTask.invokeTask | @ | zone.js:498
  | invokeTask | @ | zone.js:1370
  | globalZoneAwareCallbac

Then occassionally this error will pop restricting anything on the page from loading..

localhost/:13 GET http://localhost:4200/vendor.bundle.js net::ERR_CONTENT_LENGTH_MISMATCH
bootstrap a60cf88f7eb3d05f0b9f:54 Uncaught TypeError: Cannot read property 'call' of undefined
    at __webpack_require__ (bootstrap a60cf88f7eb3d05f0b9f:54)
    at Object.0 (main.ts:11)
    at __webpack_require__ (bootstrap a60cf88f7eb3d05f0b9f:54)
    at webpackJsonpCallback (bootstrap a60cf88f7eb3d05f0b9f:25)
    at main.bundle.js:1
__webpack_require__ @ bootstrap a60cf88f7eb3d05f0b9f:54
0 @ main.ts:11
__webpack_require__ @ bootstrap a60cf88f7eb3d05f0b9f:54
webpackJsonpCallback @ bootstrap a60cf88f7eb3d05f0b9f:25
(anonymous) @ main.bundle.js:1

However, when I go to localhost:4200 in a new chome window that has no extension or into the "brave" browser, the mulitSelect doesn't stay open upon selecting an option. Yet it works and there aren't any errors like the above... It also doesn't reload without intending to make it do so. I think it's safe to say this is the method I should rely on... it's also is acting like this in Firefox. I think I'm close!!!! Thanks for looking into with me. Extremely appreciated.

@rockomatthews
Copy link
Author

rockomatthews commented Aug 18, 2017

@rubyboy any ideas.. I'm hoping to clear this today so I can keep rocking through this project. As I mentioned before, now it appears to be working fine but still closes after every option select forcing you to reopen the select options and click the next one... then the same effect. It closes as if you clicked outside the select drop down. I remember seeing an issue that I think concerned the same issue. I just wasn't experiencing it at the time and can't find it again!

Also

if, for instance, the example above's error message, becomes irrelevant... should I edit it out of my comment? It was due to an extension called "page x-ray" that I had that was providing that error. Interestingly enough though, that was the example that looked like it worked perfectly but threw the above error...

@rubyboy
Copy link
Contributor

rubyboy commented Aug 19, 2017

The problem with multi select closing upon each selection is a known (unsolved yet) one: #337

@rockomatthews
Copy link
Author

@rubyboy ah ok. Cool. I will just keep moving forward pretending it works. I believe its only a visual issue. God, I knew I saw something somewhere that described this issue. I searched all over with keywords like "multi_select clsoing" etc. Don't know how I missed that thread. Sorry. Thanks for the heads up

@rubyboy
Copy link
Contributor

rubyboy commented Aug 20, 2017

No worries. Thanks for the feedback.

@rubyboy rubyboy closed this as completed Aug 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants