0

I am using wenzhixin/multiple-select as it has select all option. I'm trying to get selected items ticked that were selected while creating the post.

This is what i have done but it doesn't work.

{{ Form::label('tags', 'Tags:') }}
<select name="tags[]" multiple="multiple" id="tags">
    @foreach($tags as $tag)
        <option value="{{$tag->id}}">{{$tag->name}}</option>
    @endforeach
</select>

<script>
    $("#tags").multipleSelect({
        placeholder: "Tags please"
    });
    $('#tags').multipleSelect().val({!!json_encode($post->tags()->getRelatedIds() )!!}).trigger('change');
</script>

I would be thakful if anyone can help me.

Zachary Dale
  • 739
  • 4
  • 11
  • 30
  • Possible duplicate of [Getting selected values from a multiple select form in Laravel](http://stackoverflow.com/questions/15358227/getting-selected-values-from-a-multiple-select-form-in-laravel) – Dipiks Jan 23 '17 at 14:35
  • I think you should use something like `$('#tags').multipleSelect('setSelects', [1, 3]);` instead of `$('#tags').multipleSelect().val()` – Tuğca Eker Jan 23 '17 at 14:41
  • @GilleQ. the thread you have linked hasn't used jquery plugin. – Zachary Dale Jan 23 '17 at 14:43

1 Answers1

1

At the end of the day, it's just an HTML select element. Given the name of tags[], this will submit within the form and be accessible through a controller as $request->get('tags') or if using the helper, then request()->get('tags');

Furthermore, you can pass the selected items to the next page in this way:

public function myFunctionToHandleTheForm()
{
    $tags = App\Tag::all(); //for example, no idea what you use
    $selectedTags = request()->get('tags');

    return view('my.view', compact('selectedTags', 'tags');
}

Now, $tags will be a numerically ordered array (ascending) containing the ID's that were submitted from your form. You can set the defaults that were checked just like this:

{{ Form::label('tags', 'Tags:') }}
<select name="tags[]" multiple="multiple" id="tags">
    @foreach($tags as $tag)
        <option value="{{$tag->id}}" {{ in_array($tag->id, $selectedTags) ? 'selected="selected"' : null }}>{{$tag->name}}</option>
    @endforeach
</select>

In the above, we're checking if the tag id is in the list of selected tag id's, and if so, we're setting the selected attribute. Your plugin will understand this and translate it correctly.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • thanks for your suggestion. Your code shows only selected tags only. I want all tags to be displayed and tick the selected ones only. If incase user wants to user wants to change tags for the post. – Zachary Dale Jan 23 '17 at 14:49
  • Previously i used select2 jquery plugin for handeling tags. And above mentioned code worked perfectly with it. I switched to multiple-select as it has select all option. – Zachary Dale Jan 23 '17 at 14:52
  • @ZacharyDale My code does exactly what you state. It loops `all tags` and selects `only the ones that were previously selected`. This leaves the ones that were *not* selected *unchecked*. – Ohgodwhy Jan 23 '17 at 14:55