@russian-dima
Thank you so much for your help. I’m still confused but I will try to understand your templates.
So just to confirm. I need to make 2 queries (in a loop) and assign it to my search button to get my results? Or do I need to make a function to call first?
searchButton is my main search and click
tagSearch is the field where you can input the keywords.
So I need to make something like under searchButton onclick?
I’m also assuming I should remove the tagSearch original code from the first query since that will be in the second one.
query1 = wixData.query('articlesList')
wixWindow.getCurrentGeolocation()
.then((obj)=>{
let latitude = obj.coords.latitude;
let longitude = obj.coords.longitude;
wixData.query("Articles")
.contains("authorCountry", $w("#iCity").value).contains("businessCategory", $w("#dropBusinessType1").value).contains("state", $w("#iState").value).contains("tags", $w("#tagSearch").value)
.find()
.then((results) => {
let props = results.items;
for (var i = 0; i < props.length; i++) {
let lat1 = latitude //current latitudo of the user
let lon1 = longitude //current longitude of the user
let lat2 = props[i].propslat; //property latitude
let lon2 = props[i].propslon; //property longitude
let distance = dist(lat1, lon1, lat2, lon2);
// new property in object array to store the distance
props[i].distance = distance;
.eq('category', selectedCategory)
.contains('keywords', keyword1);
and then I add this after?
query2 = wixData.query('articles')
export function searchTags_click(event) {
let searchValue = $w('#tagSearch').value;
let searchWords = searchValue.split(' ');
let query = wixData.query('articles')
.descending("created");
//add a "contains" condition to the query for each word:
//assumes we search in the field 'myField'
//CHANGE THIS TO YOUR FIELD NAME
for (let b=0; i < searchWords.length; i++)
{
query = query.contains('tags', searchWords[i])
console.log(query)
}
//actually run the query:
query.find()
.then(res => {
console.log(res)
$w('#propertiesRepeater').data = res.items;
});
}
and finally loop them?
let full_query = query1.or(query2);
full_query.find().then(...)
I know the above code is wrong but I’m just trying to understand how to place them.
Also is the last part of the above code correct. is it really .then(…)
what should I put in or after the then. is that the push data to repeater?
This is my full page code below but I have not tried doing the loop yet as I’m still trying to put it together.
import wixData from "wix-data";
import wixLocation from 'wix-location';
import wixWindow from 'wix-window';
let location = wixWindow.getCurrentGeolocation();
$w.onReady(function () {
setUpRepeater();
});
function dist(lat1, lon1, lat2, lon2) {
function rad(x) { return x * Math.PI / 180; }
let R = 6371;
let dLat = rad(lat2 - lat1);
let dLong = rad(lon2 - lon1);
let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(rad(lat1)) * Math.cos(rad(lat2)) * Math.sin(dLong / 2) * Math.sin(dLong / 2);
let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c * 0.621371;
//console.log(d.toFixed(3));
return d.toFixed(1);
}
async function setUpRepeater() {
$w('#articlesList').onItemReady(($item, itemData, index,) => {
/// when repeater is ready get users current geolocation
wixWindow.getCurrentGeolocation()
.then((obj) => {
let latitude = obj.coords.latitude;
let longitude = obj.coords.longitude;
//console.log("my location is: " + latitude + longitude)
let lat1 = latitude //current latitudo of the user
let lon1 = longitude //current longitude of the user
let lat2 = itemData.propslat; //get the latitude of each property
let lon2 = itemData.propslon; //get the longitude of each property
let distance = dist(lat1, lon1, lat2, lon2) // call the fucntion dist to calculate the distance between the curren user and the properties
//$item("#txtDistance").text = distance + " Miles Away";
$item("#txtDistance").text = distance + " miles";
})
.catch((error) => {
let errorMsg = error;
});
})
}
$w.onReady(() => {
loadStateList();
});
export function button5_click(event) {
$w('#iCity').value = "";
// filters reset
$w('#dropBusinessType1').value = "Select";
$w('#iState').value = "";
$w('#tagSearch').value = "";
$w('#iCity').resetValidityIndication();
$w('#dropBusinessType1').resetValidityIndication();
$w('#iState').resetValidityIndication();
$w('#tagSearch').resetValidityIndication();
$w("#dataset1").setFilter(wixData.filter());
}
function loadStateList() {
wixData.query('stateID')
.find()
.then(res => {
let options = [{"value": '', "label": 'All States'}];
options.push(...res.items.map(state => {
return {"value": state.stateList, "label": state.stateList};
}));
$w('#iState').options = options;
});
}
export function distanceButton_click(event) {
wixWindow.getCurrentGeolocation()
.then((obj)=>{
let latitude = obj.coords.latitude;
let longitude = obj.coords.longitude;
wixData.query("Articles")
.find()
.then((results) => {
let props = results.items;
for (var i = 0; i < props.length; i++) {
let lat1 = latitude //current latitudo of the user
let lon1 = longitude //current longitude of the user
let lat2 = props[i].propslat; //property latitude
let lon2 = props[i].propslon; //property longitude
let distance = dist(lat1, lon1, lat2, lon2);
// new property in object array to store the distance
props[i].distance = distance;
}
// now sort
props.sort((a, b) => (a.distance > b.distance) ? 1 : -1);
console.log(props);
// lastly, connect it to the repeater
$w("#articlesList").data = props;
})
})
}
export function searchButton_click_1(event) {
wixWindow.getCurrentGeolocation()
.then((obj)=>{
let latitude = obj.coords.latitude;
let longitude = obj.coords.longitude;
wixData.query("Articles")
.contains("authorCountry", $w("#iCity").value).contains("businessCategory", $w("#dropBusinessType1").value).contains("state", $w("#iState").value).contains("tags", $w("#tagSearch").value)
.find()
.then((results) => {
let props = results.items;
for (var i = 0; i < props.length; i++) {
let lat1 = latitude //current latitudo of the user
let lon1 = longitude //current longitude of the user
let lat2 = props[i].propslat; //property latitude
let lon2 = props[i].propslon; //property longitude
let distance = dist(lat1, lon1, lat2, lon2);
// new property in object array to store the distance
props[i].distance = distance;
}
// now sort
props.sort((a, b) => (a.distance < b.distance) ? -1 : 1);
console.log(props);
// lastly, connect it to the repeater
$w("#articlesList").data = props;
})
})
}
export function sortDistance_click(event) {
wixWindow.getCurrentGeolocation()
.then((obj)=>{
let latitude = obj.coords.latitude;
let longitude = obj.coords.longitude;
wixData.query("Articles")
.contains("authorCountry", $w("#iCity").value).contains("pickupDelivery", $w("#iPickupdelivery").value).contains("businessCategory", $w("#dropBusinessType1").value).contains("state", $w("#iState").value).contains("tags", $w("#tagSearch").value)
.find()
.then((results) => {
let props = results.items;
for (var i = 0; i < props.length; i++) {
let lat1 = latitude //current latitudo of the user
let lon1 = longitude //current longitude of the user
let lat2 = props[i].propslat; //property latitude
let lon2 = props[i].propslon; //property longitude
let distance = dist(lat1, lon1, lat2, lon2);
// new property in object array to store the distance
props[i].distance = distance;
}
// now sort
props.sort((a, b) => (a.distance > b.distance) ? 1 : -1);
console.log(props);
// lastly, connect it to the repeater
$w("#articlesList").data = props;
})
})
}