Changing from filtering a "tags" field to a "multi-reference" field

This page queries my LocalBusiness table, the Tags field (field type = tags). gets it’s content from my Business Categories table.
I used the following code on the page.

Then I decided I would rather use a multi-reference field called Tags2, but now the code won’t filter the businesses based on their categories.
Working page https://www.1425club.com/localbusinesses-1
Can anyone see what is wrong in my code?

Thank you Sylvia


Tags CODE

   import wixData from "wix-data";

let lastFilterBusinessCategories = [];
$w.onReady(() => {
 // handle each suggestion repeater1 item
    $w("#businessCategories").onItemReady(($item, itemData, index) => {
        $item('#optionCheckbox').onChange(() => {
            filterByCheckboxes()
        })
        $item("#optionText").text = itemData.value;
    });
    loadBusinessCategories();
});

function loadBusinessCategories() {
    wixData.query('BusinessCategories')
        .ascending('title')
        .find()
        .then(results => {
            $w("#businessCategories").data = [];
 // Create and map an array for the Business Category Tag options
 let businessDropdownOptions = [];
            businessDropdownOptions.push(...results.items.map(tags => {
 return { "_id": tags._id, "value": tags.title, "label": tags.title };
            }));
            $w('#businessCategories').data = businessDropdownOptions;
        });
}

function filterByCheckboxes() {
 let businesses = [];
    $w("#businessCategories").forEachItem(($item, itemData, index) => {
 if ($item('#optionCheckbox').checked) {
            businesses.push($item("#optionText").text);
            $w('#numBusinessCategoriesText').text = $item("#optionText").text;
        }
    });
    filter(businesses);
}

function filter(businesses) {
 let newBusinessesFilterCheck = compareArrays(businesses, lastFilterBusinessCategories);
 if (!newBusinessesFilterCheck) {
 let newFilter = wixData.filter();
        newFilter = newFilter.hasSome('tags', businesses)
        $w('#dynamicDataset').setFilter(newFilter).then(function () {
 let count = $w("#dynamicDataset").getTotalCount();
        });
        lastFilterBusinessCategories = businesses;
    }
}

// Code to compare string arrays
// Used to compare the current and previous business selections
function compareArrays(newFilterArray, lastFilterArray) {
 if (newFilterArray.length !== lastFilterArray.length) { return false } //Checking if the number of items in the filter arrays match
 //Sort both arrays for easy comparison and checking if the items match
 let sortedNewFilterArray = newFilterArray.concat().sort()
 let sortedLastFilterArray = lastFilterArray.concat().sort()
 for (let index in sortedNewFilterArray) {
 if (sortedNewFilterArray[index] !== sortedLastFilterArray[index]) return false
    }
 return true
}

Tags2 Multi-reference CODE
I have changed the Tags to Tags2

// For full API documentation, including code examples, visit https://wix.to/94BuAAs

import wixData from "wix-data";

let lastFilterBusinessCategories = [];

$w.onReady(() => {
 // handle each suggestion repeater1 item
 
    $w("#businessCategories").onItemReady(($item, itemData, index) => {
        $item('#optionCheckbox').onChange(() => {
            filterByCheckboxes()
        })
        $item("#optionText").text = itemData.value;
    });
    loadBusinessCategories();
});

function loadBusinessCategories() {
    wixData.query('BusinessCategories')
        .ascending('title')
        .find()
        .then(results => {
            $w("#businessCategories").data = [];
 // Create and map an array for the Business Category Options
 let businessDropdownOptions = [];
            businessDropdownOptions.push(...results.items.map(tags2 => {
 return { "_id": tags2._id, "value": tags2.title, "label": tags2.title };
            }));
 
            $w('#businessCategories').data = businessDropdownOptions;
        });
}

function filterByCheckboxes() {
 let businesses= []; 
    $w("#businessCategories").forEachItem(($item, itemData, index) => {
 if ($item('#optionCheckbox').checked) {
        businesses.push($item("#optionText").text); 
        $w('#selectedCategories').text = $item("#optionText").text;
        }   
    });
    filter(businesses) 
}
 

function filter(businesses) {
 let newBusinessFilterCheck = compareArrays(businesses, lastFilterBusinessCategories);
 if (!newBusinessFilterCheck) {
 let newFilter = wixData.filter();
        newFilter = newFilter.hasSome('tags2', businesses)
        $w('#dynamicDataset').setFilter(newFilter).then(function () {
 let count = $w("#dynamicDataset").getTotalCount();
        });
        lastFilterBusinessCategories = businesses;
    }
}

// Code to compare string arrays
// Used to compare the current and previous category selections
function compareArrays(newFilterArray, lastFilterArray) {
 if (newFilterArray.length !== lastFilterArray.length) { return false } //Checking if the number of items in the filter arrays match
 //Sort both arrays for easy comparison and checking if the items match
 let sortedNewFilterArray = newFilterArray.concat().sort()
 let sortedLastFilterArray = lastFilterArray.concat().sort()
 for (let index in sortedNewFilterArray) {
 if (sortedNewFilterArray[index] !== sortedLastFilterArray[index]) return false
    }
 return true
}

Can anyone help me?
From what I can tell the issue is in this area:

function filter(businesses) {
 let newBusinessFilterCheck = compareArrays(businesses, lastFilterBusinessCategories);
 if (!newBusinessFilterCheck) {
 let newFilter = wixData.filter();
        newFilter = newFilter.hasSome('tags2', businesses)
        $w('#dynamicDataset').setFilter(newFilter).then(function () {
 let count = $w("#dynamicDataset").getTotalCount();
        });
        lastFilterBusinessCategories = businesses;
    }
}

Why you decided to use a multireference-field?
What are you trying to achieve?

Not sure, but when you have changed from Tags to Tags2 the id should still be “tags” , right?

Thank you for getting back to me I had given up!

I had to change it to Tags2 it wouldn’t let me change the type without wiping out
The field “Tags” field was a field type of Tags.
The field “Tags2” is now a field type of Multi-reference.

Objective

I am trying to make a relationship between the “Local Bussiness” collection and the “Business Categories” collection.
It should be a “mandy to many” relationship. For every business category will have many “Local Businesses” and "Every "Local Business will have many “Business Categories”.
As in this page. https://support.wix.com/en/article/working-with-multiple-item-reference-fields .

The only difference from this example is I am using the left-hand checkboxes as filters for the repeater content on the page.

I have been wondering if I need to use the query referenced? https://support.wix.com/en/article/corvid-querying-items-that-reference-other-items

I did try just using the Tags field and changing it to multi-reference, clearing the content, and adding in new content. But I got the same result it loads ok but when I click the filter button I get blank info in the repeaters.

any other ideas?
my backup file id = 1425 Club 08 24tb 20

Hello again, i did not forget you… :grin:

I can’t give you really a solution using multiferefenres, because i am still not very familiar with this, but you could probably use — > .include( ) as an alternative way.

Take a look at this example…
https://russian-dima.wixsite.com/meinewebsite/how2-include

And YES…
I have been wondering if I need to use the query referenced?
You could also use this way.

No post here…?

Sorry I didn’t see your reply. I did try include but it didn’t work.

I could try query referenced. But the query does load both the categories on the left and the business repeaters on the right.

The problem is when I click to filter the business repeaters. Shouldn’t we be looking at the " function filter ( businesses ) "?

ok @russian-dima I’m lost with query referenced. I am trying to get my title field in BusinessCategories to load into the businessCategories checkboxes.

I’m using the example from this page

import wixData from "wix-data";

 $w("#businessCategories").forEachItem(($item, itemData, index) => {
 let categoryCheckbox = $item("#optionCheckbox");
//let categoryID = itemData._id

 // wixData.isReferenced("BusinessCategories", "title",  checked)
                         wixData.isReferenced("BusinessCategories", "title")
                        .then((result) => {
 let isReferenced = result;
 
 if (isReferenced === true) {
                                categoryCheckbox.checked = true
 //filterByCheckboxes()
                            } else { categoryCheckbox.checked = false }
                        })
 })



file: 1425 Club Aug30ta 20

@digital-edge Hi Scott, sorry to bother you. I have been using your articles on multi-reference fields to build this page.

https://support.wix.com/en/article/about-reference-fields-in-database-collections
https://support.wix.com/en/article/referencing-multiple-items-in-one-database-field
https://support.wix.com/en/article/working-with-multiple-item-reference-fields

I am having problems getting my checkboxes to filter the multi-reference “Title” field in my BusinessCategories field. Do you have any idea why?