vendredi 15 avril 2016

Select 2 doesn't show the Selected Options. (Laravel)

I'm using select2 jquery plugin, and laravel form model binding to render the data from the server. While everything else works fine, it doesn't rendered the tags that has been attached to the post as selected option.

there must be something which I'm unaware of, here's my view part.

<div class="form-group">
    {!! Form::label('tag_list','Tags:') !!}
    {!! Form::select('tag_list[]', $tags,null,['id'=>'tag_list', 'class'=>'form-control','multiple']) !!}

</div>

// This is the select 2 script
$('#tag_list').select2({
    'placeholder':'Choose Tags',
    tags:true,
    tokenSeparators:[",", " "],
    createTag:function(newTag){
        return{
            id:'new:' + newTag.term,
            text:newTag.term + '(new)'
        };
    }
});

And this is a getTagListAtrribute function in Article model

// This is the getTagListAttribute function

 public function getTagListAttribute(){
        return $this->tags->lists('post_id')->all();
    }

And I load the edit form from the controller like this:

public function article_edit($slug){
        // fetch the articles.
        //$article = DB::table('articles')->where('slug',$slug)->first();

        $article = Article::where('slug',$slug)->first();

        /*echo '<pre>';
        print_r($article->title);
        die();*/
        $tags = DB::table('tags')->lists('name','tag_id');
        $categories=DB::table('categories')->lists('category_name','category_id');

        return view('admin.pages.edit', compact('article','tags','categories'));
    }

I just want the tags which are associated with article be selected while the page loads, and which I've been unable of. So I'm in the need of help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire