Data Driven CSS Grid Vue Component





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I want to create a Grid component that accepts the number of columns from the user, accepts data and renders all it's children into consecutive cells.



Something like this.



<Grid :cells="12" :columns="6">
<div>Child1 Cell1</div>
<div>Child2 Cell2</div>
<div>Child3 Cell3</div>
<div>Child4 Cell4</div>
<div>Child5 Cell5</div>
<div>Child6 Cell6</div>
</Grid>


In the Grid.vue component in the template, this is what I expect to do.



<div class="nugget-grid-item" v-for="cell of cells" :key="cell">
{cell}
</div>


This will render something like this on the UI.
Grids



The dashed border on each cell is due to the nugget-grid-item CSS class, but CSS is not relevant here, so let's ignore that.



What I am not able to figure out is how do I get this Grid component to display the following.
enter image description here



Isn't there something like this.children from React in Vue?










share|improve this question





























    0















    I want to create a Grid component that accepts the number of columns from the user, accepts data and renders all it's children into consecutive cells.



    Something like this.



    <Grid :cells="12" :columns="6">
    <div>Child1 Cell1</div>
    <div>Child2 Cell2</div>
    <div>Child3 Cell3</div>
    <div>Child4 Cell4</div>
    <div>Child5 Cell5</div>
    <div>Child6 Cell6</div>
    </Grid>


    In the Grid.vue component in the template, this is what I expect to do.



    <div class="nugget-grid-item" v-for="cell of cells" :key="cell">
    {cell}
    </div>


    This will render something like this on the UI.
    Grids



    The dashed border on each cell is due to the nugget-grid-item CSS class, but CSS is not relevant here, so let's ignore that.



    What I am not able to figure out is how do I get this Grid component to display the following.
    enter image description here



    Isn't there something like this.children from React in Vue?










    share|improve this question

























      0












      0








      0








      I want to create a Grid component that accepts the number of columns from the user, accepts data and renders all it's children into consecutive cells.



      Something like this.



      <Grid :cells="12" :columns="6">
      <div>Child1 Cell1</div>
      <div>Child2 Cell2</div>
      <div>Child3 Cell3</div>
      <div>Child4 Cell4</div>
      <div>Child5 Cell5</div>
      <div>Child6 Cell6</div>
      </Grid>


      In the Grid.vue component in the template, this is what I expect to do.



      <div class="nugget-grid-item" v-for="cell of cells" :key="cell">
      {cell}
      </div>


      This will render something like this on the UI.
      Grids



      The dashed border on each cell is due to the nugget-grid-item CSS class, but CSS is not relevant here, so let's ignore that.



      What I am not able to figure out is how do I get this Grid component to display the following.
      enter image description here



      Isn't there something like this.children from React in Vue?










      share|improve this question














      I want to create a Grid component that accepts the number of columns from the user, accepts data and renders all it's children into consecutive cells.



      Something like this.



      <Grid :cells="12" :columns="6">
      <div>Child1 Cell1</div>
      <div>Child2 Cell2</div>
      <div>Child3 Cell3</div>
      <div>Child4 Cell4</div>
      <div>Child5 Cell5</div>
      <div>Child6 Cell6</div>
      </Grid>


      In the Grid.vue component in the template, this is what I expect to do.



      <div class="nugget-grid-item" v-for="cell of cells" :key="cell">
      {cell}
      </div>


      This will render something like this on the UI.
      Grids



      The dashed border on each cell is due to the nugget-grid-item CSS class, but CSS is not relevant here, so let's ignore that.



      What I am not able to figure out is how do I get this Grid component to display the following.
      enter image description here



      Isn't there something like this.children from React in Vue?







      javascript reactjs vue.js vuejs2 vue-component






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 '18 at 4:31









      ShreerangShreerang

      12012




      12012
























          1 Answer
          1






          active

          oldest

          votes


















          1














          What you need are slots. See docs here. As you'll see slots allow a parent component to pass DOM elements into a child component. A basic look at them could go like this:



          //ChildComponent.vue
          <template>
          <div>
          <p>I'm the child component!</p>
          <!-- Content from the parent gets rendered here. -->
          <slot></slot>
          </div>
          </template>


          And then you inject content into the slot tags like this:



          //ParentComponent.vue
          <template>
          <div>
          <child-component>
          <p>I'm injected content from the parent!</p>
          <p>I can still bind to data in the parent's scope, like this! {{myVariable}}</p>
          </child-component>
          </div>
          </template>


          Slots can get pretty complex and do a lot of things so are well worth looking into.



          Further to your below comment, you can put a v-for in the grid. This outputs what you seem to be after. I've put an input in to accept the users number of columns as you said and it then renders that number of cells. You can of course use multiple slots and named slots and scoped slots but I'll leave it up to you how you expand on this.



          //Grid.vue
          <template>
          <div class="cell">
          <slot></slot>
          </div>
          </template>

          <script>
          export default {

          }
          </script>
          <style scoped>
          .cell {
          height: 40px;
          width: 60px;
          border: 1px solid gray;
          }
          </style>


          and parent:



          <template>
          <div class="content">

          <label>Enter number of columns</label>
          <input v-model.number="col" type="number">
          <Grid v-for="(n, i) in col" :key="i" >
          <div>Child{{n}} Cell{{n}}</div>
          </Grid>
          </div>
          </template>

          <script>

          import Grid from '@/components/admin/Grid'

          export default {
          layout: 'admin',
          components: {
          Grid
          },
          data: () => ({

          col: 4
          }),
          }
          </script>





          share|improve this answer


























          • I tried slot, but my guess it can be used only once. What if I do a v-for inside of the Grid component? Something like <Grid :cells="12" :columns="6"><div v-for="item in data">{{ item }}</div></Grid>

            – Shreerang
            Nov 17 '18 at 16:57













          • @shreerang, I have updated the answer

            – Andrew1325
            Nov 17 '18 at 22:26











          • Thanks Andrew. I actually used your suggestion/solution and used it to implement a Grid that I was expecting to render. I will get back again once I have that completed and then mark your solution as the answer with some things I had modified!

            – Shreerang
            Nov 20 '18 at 20:43











          • Ok thanks. Glad it helped.

            – Andrew1325
            Nov 21 '18 at 7:50












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348233%2fdata-driven-css-grid-vue-component%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          What you need are slots. See docs here. As you'll see slots allow a parent component to pass DOM elements into a child component. A basic look at them could go like this:



          //ChildComponent.vue
          <template>
          <div>
          <p>I'm the child component!</p>
          <!-- Content from the parent gets rendered here. -->
          <slot></slot>
          </div>
          </template>


          And then you inject content into the slot tags like this:



          //ParentComponent.vue
          <template>
          <div>
          <child-component>
          <p>I'm injected content from the parent!</p>
          <p>I can still bind to data in the parent's scope, like this! {{myVariable}}</p>
          </child-component>
          </div>
          </template>


          Slots can get pretty complex and do a lot of things so are well worth looking into.



          Further to your below comment, you can put a v-for in the grid. This outputs what you seem to be after. I've put an input in to accept the users number of columns as you said and it then renders that number of cells. You can of course use multiple slots and named slots and scoped slots but I'll leave it up to you how you expand on this.



          //Grid.vue
          <template>
          <div class="cell">
          <slot></slot>
          </div>
          </template>

          <script>
          export default {

          }
          </script>
          <style scoped>
          .cell {
          height: 40px;
          width: 60px;
          border: 1px solid gray;
          }
          </style>


          and parent:



          <template>
          <div class="content">

          <label>Enter number of columns</label>
          <input v-model.number="col" type="number">
          <Grid v-for="(n, i) in col" :key="i" >
          <div>Child{{n}} Cell{{n}}</div>
          </Grid>
          </div>
          </template>

          <script>

          import Grid from '@/components/admin/Grid'

          export default {
          layout: 'admin',
          components: {
          Grid
          },
          data: () => ({

          col: 4
          }),
          }
          </script>





          share|improve this answer


























          • I tried slot, but my guess it can be used only once. What if I do a v-for inside of the Grid component? Something like <Grid :cells="12" :columns="6"><div v-for="item in data">{{ item }}</div></Grid>

            – Shreerang
            Nov 17 '18 at 16:57













          • @shreerang, I have updated the answer

            – Andrew1325
            Nov 17 '18 at 22:26











          • Thanks Andrew. I actually used your suggestion/solution and used it to implement a Grid that I was expecting to render. I will get back again once I have that completed and then mark your solution as the answer with some things I had modified!

            – Shreerang
            Nov 20 '18 at 20:43











          • Ok thanks. Glad it helped.

            – Andrew1325
            Nov 21 '18 at 7:50
















          1














          What you need are slots. See docs here. As you'll see slots allow a parent component to pass DOM elements into a child component. A basic look at them could go like this:



          //ChildComponent.vue
          <template>
          <div>
          <p>I'm the child component!</p>
          <!-- Content from the parent gets rendered here. -->
          <slot></slot>
          </div>
          </template>


          And then you inject content into the slot tags like this:



          //ParentComponent.vue
          <template>
          <div>
          <child-component>
          <p>I'm injected content from the parent!</p>
          <p>I can still bind to data in the parent's scope, like this! {{myVariable}}</p>
          </child-component>
          </div>
          </template>


          Slots can get pretty complex and do a lot of things so are well worth looking into.



          Further to your below comment, you can put a v-for in the grid. This outputs what you seem to be after. I've put an input in to accept the users number of columns as you said and it then renders that number of cells. You can of course use multiple slots and named slots and scoped slots but I'll leave it up to you how you expand on this.



          //Grid.vue
          <template>
          <div class="cell">
          <slot></slot>
          </div>
          </template>

          <script>
          export default {

          }
          </script>
          <style scoped>
          .cell {
          height: 40px;
          width: 60px;
          border: 1px solid gray;
          }
          </style>


          and parent:



          <template>
          <div class="content">

          <label>Enter number of columns</label>
          <input v-model.number="col" type="number">
          <Grid v-for="(n, i) in col" :key="i" >
          <div>Child{{n}} Cell{{n}}</div>
          </Grid>
          </div>
          </template>

          <script>

          import Grid from '@/components/admin/Grid'

          export default {
          layout: 'admin',
          components: {
          Grid
          },
          data: () => ({

          col: 4
          }),
          }
          </script>





          share|improve this answer


























          • I tried slot, but my guess it can be used only once. What if I do a v-for inside of the Grid component? Something like <Grid :cells="12" :columns="6"><div v-for="item in data">{{ item }}</div></Grid>

            – Shreerang
            Nov 17 '18 at 16:57













          • @shreerang, I have updated the answer

            – Andrew1325
            Nov 17 '18 at 22:26











          • Thanks Andrew. I actually used your suggestion/solution and used it to implement a Grid that I was expecting to render. I will get back again once I have that completed and then mark your solution as the answer with some things I had modified!

            – Shreerang
            Nov 20 '18 at 20:43











          • Ok thanks. Glad it helped.

            – Andrew1325
            Nov 21 '18 at 7:50














          1












          1








          1







          What you need are slots. See docs here. As you'll see slots allow a parent component to pass DOM elements into a child component. A basic look at them could go like this:



          //ChildComponent.vue
          <template>
          <div>
          <p>I'm the child component!</p>
          <!-- Content from the parent gets rendered here. -->
          <slot></slot>
          </div>
          </template>


          And then you inject content into the slot tags like this:



          //ParentComponent.vue
          <template>
          <div>
          <child-component>
          <p>I'm injected content from the parent!</p>
          <p>I can still bind to data in the parent's scope, like this! {{myVariable}}</p>
          </child-component>
          </div>
          </template>


          Slots can get pretty complex and do a lot of things so are well worth looking into.



          Further to your below comment, you can put a v-for in the grid. This outputs what you seem to be after. I've put an input in to accept the users number of columns as you said and it then renders that number of cells. You can of course use multiple slots and named slots and scoped slots but I'll leave it up to you how you expand on this.



          //Grid.vue
          <template>
          <div class="cell">
          <slot></slot>
          </div>
          </template>

          <script>
          export default {

          }
          </script>
          <style scoped>
          .cell {
          height: 40px;
          width: 60px;
          border: 1px solid gray;
          }
          </style>


          and parent:



          <template>
          <div class="content">

          <label>Enter number of columns</label>
          <input v-model.number="col" type="number">
          <Grid v-for="(n, i) in col" :key="i" >
          <div>Child{{n}} Cell{{n}}</div>
          </Grid>
          </div>
          </template>

          <script>

          import Grid from '@/components/admin/Grid'

          export default {
          layout: 'admin',
          components: {
          Grid
          },
          data: () => ({

          col: 4
          }),
          }
          </script>





          share|improve this answer















          What you need are slots. See docs here. As you'll see slots allow a parent component to pass DOM elements into a child component. A basic look at them could go like this:



          //ChildComponent.vue
          <template>
          <div>
          <p>I'm the child component!</p>
          <!-- Content from the parent gets rendered here. -->
          <slot></slot>
          </div>
          </template>


          And then you inject content into the slot tags like this:



          //ParentComponent.vue
          <template>
          <div>
          <child-component>
          <p>I'm injected content from the parent!</p>
          <p>I can still bind to data in the parent's scope, like this! {{myVariable}}</p>
          </child-component>
          </div>
          </template>


          Slots can get pretty complex and do a lot of things so are well worth looking into.



          Further to your below comment, you can put a v-for in the grid. This outputs what you seem to be after. I've put an input in to accept the users number of columns as you said and it then renders that number of cells. You can of course use multiple slots and named slots and scoped slots but I'll leave it up to you how you expand on this.



          //Grid.vue
          <template>
          <div class="cell">
          <slot></slot>
          </div>
          </template>

          <script>
          export default {

          }
          </script>
          <style scoped>
          .cell {
          height: 40px;
          width: 60px;
          border: 1px solid gray;
          }
          </style>


          and parent:



          <template>
          <div class="content">

          <label>Enter number of columns</label>
          <input v-model.number="col" type="number">
          <Grid v-for="(n, i) in col" :key="i" >
          <div>Child{{n}} Cell{{n}}</div>
          </Grid>
          </div>
          </template>

          <script>

          import Grid from '@/components/admin/Grid'

          export default {
          layout: 'admin',
          components: {
          Grid
          },
          data: () => ({

          col: 4
          }),
          }
          </script>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 17 '18 at 22:25

























          answered Nov 17 '18 at 6:11









          Andrew1325Andrew1325

          697313




          697313













          • I tried slot, but my guess it can be used only once. What if I do a v-for inside of the Grid component? Something like <Grid :cells="12" :columns="6"><div v-for="item in data">{{ item }}</div></Grid>

            – Shreerang
            Nov 17 '18 at 16:57













          • @shreerang, I have updated the answer

            – Andrew1325
            Nov 17 '18 at 22:26











          • Thanks Andrew. I actually used your suggestion/solution and used it to implement a Grid that I was expecting to render. I will get back again once I have that completed and then mark your solution as the answer with some things I had modified!

            – Shreerang
            Nov 20 '18 at 20:43











          • Ok thanks. Glad it helped.

            – Andrew1325
            Nov 21 '18 at 7:50



















          • I tried slot, but my guess it can be used only once. What if I do a v-for inside of the Grid component? Something like <Grid :cells="12" :columns="6"><div v-for="item in data">{{ item }}</div></Grid>

            – Shreerang
            Nov 17 '18 at 16:57













          • @shreerang, I have updated the answer

            – Andrew1325
            Nov 17 '18 at 22:26











          • Thanks Andrew. I actually used your suggestion/solution and used it to implement a Grid that I was expecting to render. I will get back again once I have that completed and then mark your solution as the answer with some things I had modified!

            – Shreerang
            Nov 20 '18 at 20:43











          • Ok thanks. Glad it helped.

            – Andrew1325
            Nov 21 '18 at 7:50

















          I tried slot, but my guess it can be used only once. What if I do a v-for inside of the Grid component? Something like <Grid :cells="12" :columns="6"><div v-for="item in data">{{ item }}</div></Grid>

          – Shreerang
          Nov 17 '18 at 16:57







          I tried slot, but my guess it can be used only once. What if I do a v-for inside of the Grid component? Something like <Grid :cells="12" :columns="6"><div v-for="item in data">{{ item }}</div></Grid>

          – Shreerang
          Nov 17 '18 at 16:57















          @shreerang, I have updated the answer

          – Andrew1325
          Nov 17 '18 at 22:26





          @shreerang, I have updated the answer

          – Andrew1325
          Nov 17 '18 at 22:26













          Thanks Andrew. I actually used your suggestion/solution and used it to implement a Grid that I was expecting to render. I will get back again once I have that completed and then mark your solution as the answer with some things I had modified!

          – Shreerang
          Nov 20 '18 at 20:43





          Thanks Andrew. I actually used your suggestion/solution and used it to implement a Grid that I was expecting to render. I will get back again once I have that completed and then mark your solution as the answer with some things I had modified!

          – Shreerang
          Nov 20 '18 at 20:43













          Ok thanks. Glad it helped.

          – Andrew1325
          Nov 21 '18 at 7:50





          Ok thanks. Glad it helped.

          – Andrew1325
          Nov 21 '18 at 7:50




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348233%2fdata-driven-css-grid-vue-component%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python