# How do I modify this code to get it work with my repeater? - Collapsing empty dataset elements

**URL:** <https://forum.wixstudio.com/t/how-do-i-modify-this-code-to-get-it-work-with-my-repeater-collapsing-empty-dataset-elements/25397>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [July 27, 2020, 7:38pm UTC](https://forum.wixstudio.com/t/how-do-i-modify-this-code-to-get-it-work-with-my-repeater-collapsing-empty-dataset-elements/25397 "2020-07-27T19:38:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbloom](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@dbloom](https://forum.wixstudio.com/u/dbloom)\
**Post date:** [July 27, 2020, 7:38pm UTC](https://forum.wixstudio.com/t/how-do-i-modify-this-code-to-get-it-work-with-my-repeater-collapsing-empty-dataset-elements/25397/1 "2020-07-27T19:38:33Z")

</div>

Hi I have a dataset that is used by a dynamic page set and the below code for it which works perfectly well. If there is no data in each element, it is collapsed.

However, I have a need to use it with a new dynamic page set with one difference. I need to put all teh elements referenced in the code into a repeater now whereas before they were loose on the page. I’m not sure how as to modify it to make it work as the whitespace has returned. I have VERY limited experience but always want to be enabled to learn! Help modifying the code would be very appreciated! 😀

```javascript
$w.onReady(function () {
 // Assign the data set column and the matched element's id on the page
 let datasetMap = {
        greenBodyTitle1: "#text137",
        ctaSentance: "#text135",
        greenBodyTitle2: "#text152",
        bodyAnswerCta1: "#text139",
        greenBodyTitle3: "#text140",
        bodyAnswerCta3: "#text153",
        greenBodyTitle4: "#text154",
        bodyAnswerCta4: "#text141",
        greenBodyTitle5: "#text138",
        bodyAnswerCta5: "#text155",
        greenBodyTitle6: "#text142",
        bodyAnswerCta6: "#text143",
        greenBodyTitle7: "#text144",
        bodyAnswerCta7: "#text145",
        greenBodyTitle8: "#text146",
        bodyAnswerCta8: "#text147",
        greenBodyTitle9: "#text148",
        bodyAnswerCta9: "#text149",
        greenBodyTitle10: "#text150",
        bodyAnswerCta10: "#text151",
    };
 // Get the current data set on the page
 let currentItem = $w('#dataset1').getCurrentItem();
 // Iterate over the map, make the item collapsed to hide it
 for (var column in datasetMap) {
 if (currentItem.hasOwnProperty(column) && currentItem[column]) {
 // Ignore when the data item available
        } else {
            $w(datasetMap[column]).collapse();
        }
    }
});

```

---

<div class="post-metadata">

**Author:** ![darak](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/darak/32/21140_2.png) [@darak](https://forum.wixstudio.com/u/darak)\
**Post date:** [July 28, 2020, 9:34am UTC](https://forum.wixstudio.com/t/how-do-i-modify-this-code-to-get-it-work-with-my-repeater-collapsing-empty-dataset-elements/25397/2 "2020-07-28T09:34:17Z")

</div>

Hey,

If you want to use code to display database collection items on a repeater you can use the [query()](https://www.wix.com/corvid/reference/wix-data/query) function from the [wix-data](https://www.wix.com/corvid/reference/wix-data#top) API as well as the repeaters [onItemReady()](https://www.wix.com/corvid/reference/$w/repeater/onitemready) function.

I hope this helps!

Dara | Corvid Team

---

<div class="post-metadata">

**Author:** ![dbloom](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@dbloom](https://forum.wixstudio.com/u/dbloom)\
**Post date:** [July 28, 2020, 12:44pm UTC](https://forum.wixstudio.com/t/how-do-i-modify-this-code-to-get-it-work-with-my-repeater-collapsing-empty-dataset-elements/25397/3 "2020-07-28T12:44:05Z")

</div>

Thanks for replying Dara. Unfortunately, it doesn’t as I’m not too versed in this. I need it to hide text elements on my repeater when the columns they are linked to in my database are empty. Could you please offer me a line or two of code that I could then copy/paste to cover all of the other elements in the repeater? That would be a MAJOR help 🙂

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [July 28, 2020, 12:58pm UTC](https://forum.wixstudio.com/t/how-do-i-modify-this-code-to-get-it-work-with-my-repeater-collapsing-empty-dataset-elements/25397/4 "2020-07-28T12:58:16Z")

</div>

Hello Dara, hello dbloom,

when you follow one of Dara’s suggestions …

> ( you can use the [query()](https://www.wix.com/corvid/reference/wix-data/query) function)  
> … i think it would be the more simple way to get your aim.

You can query and filter a DATABASE like this example here…

```javascript
import wixData from 'wix-data';

function myFUNCTION() {
  wixData.query("myDATABASE_NAME")
  .isNotEmpty("yourREFERENCE_FIELD")
  .find()
  .then( (results) => {
    if(results.items.length > 0) {
      let firstItem = results.items[0]; //see item below
    } else {
      // handle case where no matching items found
    }
  } )
  .catch( (err) => {
    let errorMsg = err;
  } );
  }

```

This should find only fields which are not emply.

Then you take the RESULTS of the filter and put it to your REPEATER.  
Something like this…

```javascript
$w("#myRepeater").data = results

```

---

<div class="post-metadata">

**Author:** ![dbloom](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@dbloom](https://forum.wixstudio.com/u/dbloom)\
**Post date:** [July 28, 2020, 1:15pm UTC](https://forum.wixstudio.com/t/how-do-i-modify-this-code-to-get-it-work-with-my-repeater-collapsing-empty-dataset-elements/25397/5 "2020-07-28T13:15:49Z")

</div>

Thank you kindly for the reply! When I set the code as:

```javascript
import wixData from 'wix-data';

function myFUNCTION() {
  wixData.query("#dataset1")
  .isNotEmpty("#text139")
  .find()
  .then( (results) => {
 if(results.items.length > 0) {
 let firstItem = results.items[0]; //see item below
    } else {
 // handle case where no matching items found
    }
  } )
  .catch( (err) => {
 let errorMsg = err;
  } );
  }

  $w("#repeater1").data = results

```

to test if #text139 gets hidden or not, I get an error on the last line, the #repeater1 line - it says #repeater1 is not defined. Sadly, I don’t know how to define it! Also when I implement this for the 24 fields in the repeater, do I repeat the lines from wix.Data - \>Let errorMsg for all of the fields? I’m sure you can tell my ignorance by my questions - thanks so much!

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [July 28, 2020, 1:42pm UTC](https://forum.wixstudio.com/t/how-do-i-modify-this-code-to-get-it-work-with-my-repeater-collapsing-empty-dataset-elements/25397/6 "2020-07-28T13:42:13Z")

</div>

1. Of course you have to put the last code-line also into your function.
2. Your REPEATER is also called as → “repeater1” —\> element-ID.

```javascript
import wixData from 'wix-data';

function myFUNCTION() {
  wixData.query("#dataset1")
  .isNotEmpty("#text139")
  .find()
  .then( (results) => {
 if(results.items.length > 0) {
 let firstItem = results.items[0]; //see item below
  $w("#repeater1").data = results
    } else {
 // handle case where no matching items found
    }
  } )
  .catch( (err) => {
 let errorMsg = err;
  } );
  }

```

EDIT:  
I do not think that this will work, because you are mixing DATASET with DATABSE-QUERY.

But we will wait for a result 😁

---

<div class="post-metadata">

**Author:** ![dbloom](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@dbloom](https://forum.wixstudio.com/u/dbloom)\
**Post date:** [July 28, 2020, 3:47pm UTC](https://forum.wixstudio.com/t/how-do-i-modify-this-code-to-get-it-work-with-my-repeater-collapsing-empty-dataset-elements/25397/7 "2020-07-28T15:47:43Z")

</div>

Thank you! - Yes as predicted, after publishing with this code, it does not collapse the element and leaves whitespace.

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [July 28, 2020, 4:29pm UTC](https://forum.wixstudio.com/t/how-do-i-modify-this-code-to-get-it-work-with-my-repeater-collapsing-empty-dataset-elements/25397/8 "2020-07-28T16:29:00Z")

</div>

```javascript
import wixData from 'wix-data';

$w.onReady(function () { })

export function myButtonIdHere_click(event) {myFUNCTION()}

function myFUNCTION() {
  wixData.query("myDATABASE_NAME")//<-->ATTENTION DATABASE-NAME!
  .isNotEmpty("#COLUMN") //<-->ATTENTION REFERENCE-FIELD here!
  .find()
  .then( (results) => {
 if(results.items.length > 0) {
 let firstItem = results.items[0];
   console.log(results)
   console.log(firstItem)
   console.log(firstItem.title)
 $w("#repeater1").data = results
    } else {
 // handle case where no matching items found
    }
  } )
  .catch( (err) => {
 let errorMsg = err;
  } );
  }

```

This will still not do what you want, because you have to add more CODE to achieve the collapsing of elements.

What does this CODE do ?

1. Quering a DATA-Collection
2. Filtering all entries, which are NOT EMPTY.
3. Gives the RESULTS back.
4. Sets REPEATERS-DATA to founded results.

So, the REPEATER should now show all values, which are not empty in database in a selected column of your choice.

And now it’s your turn to expand end modify it to complete it like you want to have it.

---

<div class="post-metadata">

**Author:** ![dbloom](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@dbloom](https://forum.wixstudio.com/u/dbloom)\
**Post date:** [July 28, 2020, 6:19pm UTC](https://forum.wixstudio.com/t/how-do-i-modify-this-code-to-get-it-work-with-my-repeater-collapsing-empty-dataset-elements/25397/9 "2020-07-28T18:19:16Z")

</div>

I will troubleshoot this for a few days. I can’t thank you enough for your willingness to help!

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [July 28, 2020, 6:27pm UTC](https://forum.wixstudio.com/t/how-do-i-modify-this-code-to-get-it-work-with-my-repeater-collapsing-empty-dataset-elements/25397/10 "2020-07-28T18:27:58Z")

</div>

No problem we all were BEGINNER one day 😁
