Search code examples
vue.jsgrid-layoutvuejs2vue-component

How to pass component with content to GridItem in Vue-grid-layout?


Please help to understand how to pass a content to GridItems from Layout []'s hash value.

I think that GridItem.i is a key value for GridItem. But how to link "content" component for a cell accordingly ?

This way is non-working:

html:

<grid-item v-for="item in layout"
               :x="item.x"
               :y="item.y"
               :w="item.w"
               :h="item.h"
               :i="item.i">
             {{item.c}}
</grid-item>

js:

let a = `<item>There I want div from component</item>`;

Vue.component('item', {
    props: [],
    template: `<div>Primer</div>
    `
});

var testLayout = [
   {"x":0,"y":0,"w":2,"h":2,"i":"1","c":"content1"},
   {"x":2,"y":0,"w":2,"h":4,"i":"2","c":"<item>There I want div from component</item>"},    
   {"x":4,"y":0,"w":2,"h":5,"i":"3","c":a},
];

var GridLayout = VueGridLayout.GridLayout;
var GridItem = VueGridLayout.GridItem;

new Vue({
    el: '#app',
    components: {
        GridLayout,
        GridItem,
    },
    data: {
        layout: testLayout,
    },
});

https://jsfiddle.net/L2oh62tp/3/


Solution

  • Disclaimer: I'm the vue-grid-layout author.

    The grid item uses a slot to add content, so I don't think v-html works properly when used on grid-item. Just do it like this:

    <grid-item v-for="item in layout"
                   :x="item.x"
                   :y="item.y"
                   :w="item.w"
                   :h="item.h"
                   :i="item.i">
                 <div v-html="item.c"></div>
    </grid-item>
    

    fiddle: https://jsfiddle.net/gmsa/jw2mmmpq/1/