grid-layout-plus icon indicating copy to clipboard operation
grid-layout-plus copied to clipboard

Is there an easy way to auto resize a gridItem based on the contents inside?

Open lambda-thehelper opened this issue 1 year ago • 4 comments

Ex: a gridItem has contents inside that take 300px in height, i wanted the gridItem to resize itself to 300px while the resize feature wouldn't let me resize the gridItem bellow 300px as well.

lambda-thehelper avatar Oct 12 '23 08:10 lambda-thehelper

I was having the exact same question in a dashboard I built using grid-layout.

sCarita avatar Oct 13 '23 13:10 sCarita

news?

lucabecchetti avatar Mar 01 '24 14:03 lucabecchetti

Yup, this feature would be great

SamX23 avatar Apr 01 '24 05:04 SamX23

I've implemented this feature, but it is not perfect. At first, set 'height : auto!height' in css, the browser will resize the griditem to fit the contents, then we get the acutal height using element.offsetHeight, finally recreate style and layout all items again.

In GridLayout:

onMounted(()=>{
      // it's not good, but runnable
      setTimeout(
        () => {
          eventBus.emit('calcClientHeight') // catch this event in GridItem
          setTimeout(() => onWindowResize(), 200)
        },
        300
      );
})

In GridItem:

function calcClientHeight() {
  const size = useElementSize(this$refsItem); // get actual height when css's height is set as 'auto!important'
  const { w, h } = calcWH(size.height.value, size.width.value, true)
  emit('height-updated', props.i, h) // update item's data and the re-create style
  nextTick(() => {
    createStyle();
    if (this$refsItem.value) {
      this$refsItem?.value.classList.remove('autoHeight')
      this$refsItem?.value.classList.add('fixedHeight')
    }
  })
}

CSS:

<style scoped>
.autoHeight {
  height:auto !important;
}

.fixedHeight {
  height: v-bind('styleObj.height') !important;
}
</style>

touchrank-dev avatar Jul 03 '24 14:07 touchrank-dev