Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Materialize chips initialization

I would like to add dynamically some materialize chips.

 $('.chips-initial').material_chip({
                        data: [{
                                tag: 'Apple'
                            }, {
                                tag: 'Microsoft'
                            }, {
                                tag: 'Google'
                            }]
                    });

Like above but the values I want to give are dynamic. How can I create an data object like above to pass it as parameter? Thank you in advance

like image 549
gf.gee Avatar asked Oct 18 '25 18:10

gf.gee


1 Answers

Basically what happens here is when the page loads it populates the text string values found in metaTags that were inserted into a hidden field from the database. The for loop iterates into the necessary chips-initial.

 var tagsMeta = [];
    //alert(tagsMeta);
    var tagsString = document.getElementById('metaTags').value;
    //alert(tagsString);
    var tagsArray = tagsString.split(',');
    for(i=0; i < tagsArray.length; i++) {
      tagsMeta.push({tag: tagsArray[i]});
    }

    $('.chips-initial').material_chip({
        data: tagsMeta
    });
like image 77
Michal Pina Avatar answered Oct 20 '25 09:10

Michal Pina