Vlookup equivalent code

Hi,

I am looking for an sample code to achieve the function similar to “vlookup” in excel, but couldn’t find any, was wondering if anyone could provide me with an example or direct me to an existing one? What I want to achieve is-

One the webpage, I have a “table” and a “dropdown” filter linked to the table.
when value A is selected in the dropdown, code will search in the “data collection” column “I” and pick up corresponding value B from column “II”. Value B will be used as a filter for the “table”.

Thanks.

Regards,
Stephen

You can filter a table by dropdowns as like in these tutorials.
https://codequeen.wixsite.com/dropdown
https://www.youtube.com/watch?v=EhXed0u6wh0

Repeater filtering.
https://www.youtube.com/watch?v=r0DLqkRDJ34

Many thanks for your reply. I’ve seen both in the past, but don’t think the “Vlookup” function is mentioned in either of them.

My filter is working fine.

@stephenxj I guess you can always use some JavaScript like:

//Let's say you want to find the last name of John:
const matchRow = $w("#myTable").rows.find( e => e.firstName === 'John' );
const requiredValue = matchRow.lastName;

@jonatandor35 very helpful thanks. A follow up question hope you have a bit time to help. my draft website is at https://stephenxj5.wixsite.com/yyeducation/copy-of-school-search

It’s designed to contain 2 steps.

  • Step 1 in the current school section which is linked to collection"Chin_School": choose the province then the school, the “vlookup” function you helped me earlier will pick up a value from the table - requiredValue = matchRow.cat
  • Step 2 in the target school section which is linked to dataset “dataset1”: apart from the visible filtering “Gender”, “Town”, “Postcode” as you can see, there is an additional hidden filtering - “Cat” equals to requiredValue from step1.

2 Questions:

  • the section where the requiredValue is retrieved in step1 might not be correct
  • when I refer to requiredValue in step2, it says “requiredValue is not defined”.

Please see the codes attached below.

import wixData from ‘wix-data’;

$w.onReady( function () {

uniqueDropDown1(); 

});

function uniqueDropDown1 (){

wixData.query("Chin_Schools") 

    .limit(1000) 

  .find() 

  .then(results => { 

const uniqueTitles = getUniqueTitles(results.items);

       $w("#province").options = buildOptions(uniqueTitles); 

  }); 

function getUniqueTitles(items) {

const titlesOnly = items.map(item => item.province);

return [… new Set(titlesOnly)];

} 

function buildOptions(uniqueList) {

return uniqueList.map(curr => {

return {label:curr, value:curr};

    }); 

} 

}

export function province_change(event,$w) {

uniqueDropDown2();

$w(“#schooldrop”).enable();

}

function uniqueDropDown2 (){

wixData.query("Chin_Schools") 

    .contains("province", $w("#province").value) 

    .limit(1000) 

  .find() 

  .then(results => { 

const uniqueTitles = getUniqueTitles(results.items);

       $w("#schooldrop").options = buildOptions(uniqueTitles); 

  }); 

function getUniqueTitles(items) {

const titlesOnly = items.map(item => item.schoolName);

return [… new Set(titlesOnly)];

} 

function buildOptions(uniqueList) {

return uniqueList.map(curr => {

return {label:curr, value:curr};

    }); 

} 

}

export function schooldrop_change(event,$w){

const matchRow = $w(“#Chin_Schools”).rows.find( e => e.firstName === $w(“#schooldrop”).value );
const requiredValue = matchRow.cat;

}

export function SearchButton_click(event) {
let searchGender = $w(“#GenderSelect”).value;
let searchTown = $w(“#Town”).value;
let searchPostcode = $w(“#Postcode”).value;
let searchcat = 1;
$w(“#dataset1”).setFilter(wixData.filter()
.contains(“gend1618”, searchGender)
.contains(“town”, searchTown)
.contains(“pcode”, searchPostcode)
.eq(“top3SubjectRankGroup”, searchcat)
);

}

@stephenxj what I wrote before was only an example.
There’s no point in using e.firstName if firstName is not a column key in your table.
My code actually saying:
Find element (i.e table row) where the value of field key X is Y.