Hi,
I am surprised how difficult it is to have that simple task performed.
I have a table connected to a dataset connected to a collection. Selecting a row in the table loads the item fields in entry fields (not connected to data, but connecting them does not change the behaviour) and allows to change item contents. A submit button replaces the values in the table (it does) and should update the collection, but it doesn’t despite a $w(‘#dataset’).save()
“Import wixUsers” is declared, the dataset has the R/W enabled, the user is recognized as a “member” who has the write, update and delete rights assigned in the roles screen for that collection.
Inserting a new item with wixData.insert does not work either, even if the dataset is set to “write only”.
Both insert and update work fine in preview, but not in live.
I am really surprised that 3 bound elements do not keep syncronized more easily… did I miss something ?
Webpage :
https://www.collongebasketclub.net/developpement
As far as I understand, you need to submit an item to the database and refresh your table?
Try this:
- submit to the database using wixData.insert() ( https://www.wix.com/corvid/reference/wix-data.html#insert )
- refresh your dataset ( https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#refresh )
Viktoriia, thank you…
I went already several times through these references with no success. Here is my code, as you can see it does use them (refresh() does only affect the dataset, not the collection, so I removed it : even without, the table gets properly refreshed when an item is modified and the save button hit) . And the insert() doesn’t go through the dataset, as far as I understand… I think that (#dataset).save could be used for insertion too from what I understand from the reference, but as the save doesn’t work for the updates, it won’t work for an insertion either.
export function update_new_click(event) {
$w('#button5').hide();
$w('#button7').hide();
let dtPick = $w('#datePicker1').value;
let tPick = $w('#timePicker1').value;
var dtD = dtPick.getDate();
var dtM = dtPick.getMonth(); // jan = 0
var dtY = dtPick.getFullYear();
var tH = tPick.substr(0,2);
var tM = tPick.substr(3,2);
var dt1 = new Date('2018-10-26');
var dt2 = new Date('2019-3-28')
if (dtPick > dt1 && dtPick < dt2) { // gestion heure été/hiver pour -1 ou -2h pour enregistrer
var TH = tH-1;}
else {
var TH = tH-2;}
var dt_finale = new Date(dtY,dtM,dtD,TH+2,tM); // TH +2 pour repasser à l'heure locale
if (save_toggle === 0) { // save mods
$w('#dataset1').setFieldValue('dateHeure',dt_finale);
$w('#dataset1').setFieldValue('heure',(tH + ':' + tM));
$w('#dataset1').setFieldValue('equipe',$w('#input1').value);
$w('#dataset1').setFieldValue('championnat',$w('#input2').value);
$w('#dataset1').setFieldValue('batBattu',$w('#input3').value);
$w('#dataset1').setFieldValue('advLieu',$w('#input4').value);
$w('#dataset1').setFieldValue('score',$w('#input5').value);
$w('#dataset1').save()
.catch( (err) => {
let errMsg = err;
console.log(errMsg);
} );
}
if (save_toggle === 1) { // insert
let toInsert = {
'dateHeure': dt_finale,
'heure': (tH + ':' + tM),
'equipe': $w('#input1').value,
'championnat': $w('#input2').value,
'batBattu': $w('#input3').value,
'advLieu': $w('#input4').value,
'score': $w('#input5').value};
wixData.insert('match', toInsert)
.then( (results) => {
let item = results;
console.log(results);
} )
.catch( (err) => {
let errMsg = err;
console.log(errMsg);
} );
} }
I can see that you insert your item to the database, but there is no code for dataset refresh:
let toInsert = {
'dateHeure': dt_finale,
'heure': (tH + ':' + tM),
'equipe': $w('#input1').value,
'championnat': $w('#input2').value,
'batBattu': $w('#input3').value,
'advLieu': $w('#input4').value,
'score': $w('#input5').value};
wixData.insert('match', toInsert)
.then( (results) => {
let item = results;
console.log(results);
$w('#dataset1').refresh();
} )
.catch( (err) => {
let errMsg = err;
console.log(errMsg);
} );
} }
I had already tried to add it after (outside of …) the wixData.insert function, so I just tried it in the function as you suggest, but it does not work either . I am presently trying other ways, but it’s a bit frustrating as this kind of work should be quite trivial !
Viktoriia,
I did finally manage it, after hours of trials.
First, permissions for the collection must be set to “custom, anyone, anyone, anyone”. Dataset role to R/W. This means that the page access should be restricted
Second, I gave away with dataset.save(). Doesn’t work here, for any unknown reason. I used wixData.update() instead (and wixData.remove() for deletion)
The only issue I am left with is that the table updates nicely for insert and update, but not for delete (strangely with the same table refresh function for all 3 tasks, involving a wixData.query on the collection !). The code is a bit long, but if you are interested, here it is :
export function update_new_click(event) {
$w('#button5').hide();
$w('#button7').hide();
let dtPick = $w('#datePicker1').value;
let tPick = $w('#timePicker1').value;
var dtD = dtPick.getDate();
var dtM = dtPick.getMonth(); // jan = 0
var dtY = dtPick.getFullYear();
var tH = tPick.substr(0,2);
var tM = tPick.substr(3,2);
var dt1 = new Date('2018-10-26');
var dt2 = new Date('2019-3-28')
if (dtPick > dt1 && dtPick < dt2) { // gestion heure été/hiver pour -1 ou -2h pour enregistrer
var TH = tH-1;}
else {
var TH = tH-2;}
var dt_finale = new Date(dtY,dtM,dtD,TH+2,tM); // TH +2 pour repasser à l'heure locale
DH = dt_finale;
// save mods
if (save_toggle === 0) {
var data = {
'dateHeure': dt_finale,
'heure': (tH + ':' + tM),
'equipe': $w('#input1').value,
'championnat': $w('#input2').value,
'batBattu': $w('#input3').value,
'advLieu': $w('#input4').value,
'score': $w('#input5').value,
'_id' : save_id };
wixData.update('match', data)
.then( (results) => {
let item = results;
search_dataset1('dateHeure', $w('#dropdown1').value); // reloads table
})
.catch( (err) => {
let errMsg = err;
console.log(errMsg);
});
}
if (save_toggle === 1) {
var data = {
'dateHeure': dt_finale,
'heure': (tH + ':' + tM),
'equipe': $w('#input1').value,
'championnat': $w('#input2').value,
'batBattu': $w('#input3').value,
'advLieu': $w('#input4').value,
'score': $w('#input5').value };
wixData.insert('match', data)
.then( (results) => {
let item = results;
search_dataset1('dateHeure', $w('#dropdown1').value); // reloads table
})
.catch( (err) => {
let errMsg = err;
console.log(errMsg);
});
}
}
delete function :
export function suppr_click(event) {
wixData.remove('match',save_id)
.then( (results) => {
let item = results;
} )
.catch( (err) => {
let errMsg = err;
});
search_dataset1('dateHeure', $w('#dropdown1').value);
}
and for the table refresh function :
function search_dataset1(tri, saison){
if (saison === '') {
$w("#dropdown1").selectedIndex = 0;
var dpd = $w('#dropdown1').value;
}
else {
var dpd = saison;
}
var lowerYear = dpd.substring(0,4);
var upperYear = dpd.substring(5,9);
lowerDate = new Date(lowerYear.concat('-','8','-','1'));
upperDate = new Date(upperYear.concat('-','7','-','30'));
if (tri==='dateHeure'|| tri==='') {
wixData.query('match')
.gt('dateHeure', lowerDate)
.lt('dateHeure', upperDate)
.descending(tri)
.limit(300)
.find()
.then( (res) => {
let tableRows = res.items;
$w('#table1').rows = tableRows;
})
}
else {
wixData.query('match')
.gt('dateHeure', lowerDate)
.lt('dateHeure', upperDate)
.ascending(tri)
.limit(300)
.find()
.then( (res) => {
let tableRows = res.items;
$w('#table1').rows = tableRows;
})
}
}
Thank you for the insight and help !