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

From select with multiple option #185

Closed
mmiraglia opened this issue Mar 9, 2018 · 3 comments
Closed

From select with multiple option #185

mmiraglia opened this issue Mar 9, 2018 · 3 comments

Comments

@mmiraglia
Copy link

Hi everyone!
How can I select multiple options in a form?
In the next example I need to select "Teacher 3" and "Teacher 11" (selected attribute)
Thanks in advance!

XML form to edit a subject

	<form action="" method="post" accept-charset="utf-8">
	<input type="text" name="name" />
	<select name="year">
		<option value="1">1#</option>
		<option value="2">2#</option>
		<option value="3">3#</option>
	</select>
	<select name="teachers[]" id="teachers" multiple>
		<option value="1">T1</option>
	</select>

	<button type="submit">Save</button>
	</form>

Dump of the data

object(stdClass)[34]
  public 'subject' => 
    array (size=4)
      'id' => string '11' (length=2)
      'name' => string 'Subject 5' (length=9)
      'year' => string '2' (length=1)
      'teachers' => 
        array (size=2)
          0 => string 'Teacher 3' (length=9)
          1 => string 'Teacher 11' (length=10)
 public 'teachers' => 
    array (size=3)
      0 => 
        array (size=2)
          'id' => string '1' (length=1)
          'name' => string 'Teacher 1' (length=9)
      1 => 
        array (size=2)
          'id' => string '2' (length=1)
          'name' => string 'Teacher 3' (length=9)
      3 => 
        array (size=2)
          'id' => string '5' (length=1)
          'name' => string 'Teacher 11' (length=10)

TSS

select#teachers option {repeat: data(teachers); content: iteration(name)}
select#teachers option:attr(value) {content: iteration(id)}
form input[name="name"]:attr(value) {content: data(subject.name)}
form select[name="year"] option[value=data(subject.year)]:attr(selected) { content: 'selected'; }
@TRPB
Copy link
Member

TRPB commented Mar 9, 2018

The logic for Loop through $subject['teachers'] and look for one that matches $teacher['name'] is fairly complex and not something Transphporm is currently capable of.

I'm trying to think how something like that could be implemented, perhaps something like

select#teachers option:iteration[name in data(subject.teachers)]{ content: 'selected'}

Where in (or some other label for the operator) is shorthand for in_array.

At the moment, you would need to change the data such that the selected teachers were flagged in the teachers array:

 public 'teachers' => 
    array (size=3)
      0 => 
        array (size=2)
          'id' => string '1' (length=1)
          'name' => string 'Teacher 1' (length=9)
      1 => 
        array (size=2)
          'id' => string '2' (length=1)
          'name' => string 'Teacher 3' (length=9)
          'selected' => boolean 'true' 
      3 => 
        array (size=2)
          'id' => string '5' (length=1)
          'name' => string 'Teacher 11' (length=10)
          'selected' => boolean 'true' 

Then check of it to select the relevant options:

select#teachers option:iteration[selected]:attr(selected) { content: 'selected'}

This works, but I'll look at implementing the in operator to make this a little easier.

@mmiraglia
Copy link
Author

mmiraglia commented Mar 9, 2018

Thanks Tom for the workaround, It's worked perfectly!
The in operator could be a great enhancement

TRPB added a commit that referenced this issue Mar 12, 2018
TRPB added a commit that referenced this issue Mar 12, 2018
@TRPB
Copy link
Member

TRPB commented Mar 12, 2018

You can now use in as above and not need to in include the selected option for each teacher. :)

@TRPB TRPB closed this as completed Mar 12, 2018
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