Ok, let’s do it.
Some comments related to your code.
- Please use CODE-Tags/Snipets to show code in your post like this…
import {local} from 'wix-storage';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixUsers from 'wix-users';
var roleName, currentUser
//-----START------------------
$w.onReady(function () {
currentUser = wixUsers.currentUser;
currentUser.getRoles()
.then( (roles) => {
let firstRole = roles[0];
console.log(firstRole)
roleName = firstRole.name;
console.log(roleName)
});
$w("#resultsTable").columns = [
{"id": "DNI", "dataPath": "dni", "label": "DNI", "width": 100, "type": "text"},
{"id": "Nombre", "dataPath": "nombre", "label": "Nombre", "width": 100, "type": "text"},
{"id": "Apellido", "dataPath": "apellido1", "label": "Apellido", "width": 100, "type": "text"},
{"id": "Apellido 2", "dataPath": "apellido2", "label": "Apellido 2", "width": 100, "type": "text"},
{"id": "Edad", "dataPath": "fechaNacimiento", "label": "Edad", "width": 100, "type": "date"},
{"id": "Abono", "dataPath": "Abono", "label": "Abono", "width": 100, "type": "text"},
];
//--- Button-Events/Actions ------------------
$w('#searchBox').onKeypress(()=> {search();})
$w('#input1').onKeypress(()=> {search();})
$w('#page1').onViewportEnter(()=> {search();})
$w('#searchButton').onClick(()=> {search();})
$w('#searchBox').onChange(()=> {search();})
$w('#dropdown1').onChange(()=> {search();})
$w('#input1').onChange(()=> {search();})
$w('#').onKeypress(()= {search();})
});
//--- Search/Filter-Function ---------------------------
function search() {
let myQuery = wixData.query('invitados')
if (roleName==="Ho") {
// wixData.query('invitados')
myQuery
.contains('dni', $w("#searchBox").value)
.or(wixData.query('invitados').contains('nombre', $w("#searchBox").value))
.or(wixData.query('invitados').contains('apellido1', $w("#searchBox").value))
.and(wixData.query('invitados').contains('evento',"Ho"))
.find()
.then(res => {
$w('#resultsTable').rows = res.items; // see below
})
}
if (roleName==="Ri") {
// wixData.query('invitados')
myQuery
.contains('dni', $w("#searchBox").value)
.or(wixData.query('invitados').contains('nombre', $w("#searchBox").value))
.or(wixData.query('invitados').contains('apellido1', $w("#searchBox").value))
.and(wixData.query('invitados').contains('evento',"Ri"))
.find()
.then(res => {
$w('#resultsTable').rows = res.items; // see below
})
}
if (roleName==="Co") {
// wixData.query('invitados')
myQuery
.contains('dni', $w("#searchBox").value)
.or(wixData.query('invitados').contains('nombre', $w("#searchBox").value))
.or(wixData.query('invitados').contains('apellido1', $w("#searchBox").value))
.and(wixData.query('invitados').contains('evento',"Co"))
.find()
.then(res => {
$w('#resultsTable').rows = res.items; // see below
})
}
if (roleName==="Ki") {
// wixData.query('invitados')
myQuery
.contains('dni', $w("#searchBox").value)
.or(wixData.query('invitados').contains('nombre', $w("#searchBox").value))
.or(wixData.query('invitados').contains('apellido1', $w("#searchBox").value))
.and(wixData.query('invitados').contains('evento',"Ki"))
.find()
.then(res => {
$w('#resultsTable').rows = res.items; // see below
})
}
if (roleName==="Zo") {
// wixData.query('invitados')
myQuery
.contains('dni', $w("#searchBox").value)
.or(wixData.query('invitados').contains('nombre', $w("#searchBox").value))
.or(wixData.query('invitados').contains('apellido1', $w("#searchBox").value))
.and(wixData.query('invitados').contains('evento',"Zo"))
.find()
.then(res => {
$w('#resultsTable').rows = res.items; // see below
})
}
if (roleName==="Conc") {
$w('#input1').show()
// wixData.query('invitados')
myQuery
.contains('dni', $w("#searchBox").value)
.or(wixData.query('invitados').contains('nombre', $w("#searchBox").value))
.or(wixData.query('invitados').contains('apellido1', $w("#searchBox").value))
.and(wixData.query('invitados').contains('evento', $w("#input1").value))
.find()
.then(res => {
$w('#resultsTable').rows = res.items; // see below
})
}
}
export function resultsTable_rowSelect(event, $w) {
let rowData = event.rowData;
let rowIndex = event.rowIndex;
const myRow = event.target.rows[rowIndex];
wixLocation.to(`${myRow["link-invitados-nombre"]}`)
}
-
You do not have to query everytime again and again your collection (see code-improvement).
-
Try to code more the —> “onReady-Style” instead of using “export-functions”!
-
This code can be modified even much more.