Why add new item is added as second i row not first

Hi i have question. I have add new button and it adds item as secon in repeator not first. What can reason. I would like it shows as first
Code is

export function button11_click(event) {
$w(“#dataset1”). new ()
.then(() => {
let toastbox = $w(“#text60”).text
$w(“#dataset1”).setFieldValue(“wydarzenie”, toastbox);
$w(“#dataset1”).save();
console.log(“New item saved”)
. catch ( (err) => {
let errorMsg = err;
console.log(errorMsg);
}

)})}

Try to replace your save with this…

  $w("#dataset1").save(() => {
    // Data is saved now refresh the dataset1
    $w("#dataset1").refresh();
  })

And also make sure what kind of sorting you have in Dataset1, check it by clicking Manage Dataset and scroll down at the botton and make sure NEW → OLDEST is active. Then this should work.

Happy Coding!

not working. take a look. dodaj -meand add, usun means delete, its easy thing. You add events to dataset. when you add it puts it in differen rows and have now idea why not first
https://misiewicz3.wixsite.com/mysite/dekorac

log to test account info@ceglarnia.com zciweisim

first two and last event works

its so strange every time i try it puts in different place. then delete (usun) - delete those items and again it puts in different place. Than i log again and in the middle. Yhe only solusuin is to do like i made in edit button it goes to differne tpaga and back to my. Then its sorted. But question is why it puts itmes so strange :slight_smile: thanks for help

Your issue may due to the fact that using the new() function inserts a new row into your dataset. Calling the save() function inserts another row into the dataset rather than update the row your just created. First, try removing the new()function and just use the save() function. If that does not fix the issue, then try using the update() function instead of the save() function. To use this function, you do need to provide the system _id field and value along with the field and value you want to update. In my experience, when executing the update() function, you need to supply all the fields in the table and their associated values if those values exist. What that means is you cannot just update a single field in a table that has multiple fields if the other fields are contain values. The WixCodeAPI provides some reasonable code examples. I have not worked with repeaters so this suggestion may not work.

i made only new button without code but with wix option and is the same so its some probem with new funcion i n repeator. Save doesnt create new item but save what you inputed

Yes, the save function in the code above ~ $w(" #dataset1 “).save(); ~ saves what you inputed, but not into the blank row created with the code ~ $w(” #dataset1 “). new () ~. I was merely suggesting to comment out the lines $w(” #dataset1 "). new () .then(() => { along with the associated closing brackets to see if you get the desired result. If that does not fix your issue, I’m sorry, but I don’t have any other suggestions.

it doesnt work becuase it gives value to first item But i need new item with this value not change existing .

I’m sorry my suggestions did not help.

Krs - you need to follow the directions that Andreas proposed. When you add a new item you need to refresh the dataset. This forces the repeater to redraw and will order the rows based on the dataset sort configuration. So to summarize:

  1. You need to make sure you have your dataset sorted by DateCreated which you have.


2. You need to make sure that you refresh the dataset after you complete the save. So your code should look something like this.

export function button11_click(event) {
    $w("#dataset1").new()
    .then(() => {
        let toastbox = $w("#text60").text;
        $w("#dataset1").setFieldValue("wydarzenie", toastbox);
        return $w("#dataset1").save();
     })
     .then(() => {
         // Data is saved now refresh the dataset1     
         $w("#dataset1").refresh();
     })
     .catch( (err) => {
         let errorMsg = err;
         console.log(errorMsg);
     });
}

Having looked at your code you also seem to be confused about how to use the onReady() functions.

In general the $w.onReady() function registers a handler that ONLY runs when the page is loaded. Once the page loads the functions in it won’t be called. You shouldn’t call $w.onReady() inside of a function as it really won’t do anything after the page has loaded. The same is pretty much true for dataset.onReady(). These should be thought of as initialization handlers intended to set up page information as the page loads or after a dataset has loaded.

So an example of how to use onReady would be to disable the buttons you have create that change the dataset while the page is loading and enable them once you know the dataset has finished loading like this:

$w.onReady(() => {
    $w('#button11').disable();
    // Wait for dataset to finish loading
    $w('#dataset1').onReady(() => {
        // Dataset ready so we can allow the user to use the button
        $w('#button11').enable();
    });
})

In addition calling console.log() immediately before a .then() call is a syntax error. You can only call a function in the scope of a valid code block. This is normally between {} inside a function or at the top level of the page code. So your page code should look like this:

$w.onReady(() => {
  $w('#button11').disable();
  $w('#zimnybufet').disable();
  $w('#button29').disable();
  // Wait for dataset to finish loading
  $w('#dataset1').onReady(() => {
    // Dataset ready so we can allow the user to use the button 
    $w('#button11').enable();
    $w('#zimnybufet').enable();
    $w('#button29').enable();
  });
})

export function button11_click(event) {
  $w("#dataset1").new()
  .then(() => {  
    console.log("nowy item");
    let toastbox = $w("#text60").text;
    $w("#dataset1").setFieldValue("wydarzenie", toastbox);
    return $w("#dataset1").save();
  })
  .then(() => {
   $w("#dataset1").refresh();
   console.log("New item saved"); 
  })
}

export function zimnybufet_click(event) {
   $w("#dataset1").new()
   .then(() => {  
       let zimny = $w("#text76").text;
       $w("#dataset1").setFieldValue("wydarzenie", zimny);
       return $w("#dataset1").save();
    })
    .then(() => {
       $w("#dataset1").refresh();
       console.log("New item saved");
    })
    .catch( (err) => {
       let errorMsg = err;
       console.log(errorMsg);
    }); 
}

export function button29_click(event) {
  $w("#dataset1").new()
  .then(() => {  
    let zdjecie = $w("#text64").text;
    $w("#dataset1").setFieldValue("wydarzenie", zdjecie);
    return $w("#dataset1").save();
  })
  .then(() => {
    $w("#dataset1").refresh();
    console.log("New item saved");
  })
  .catch( (err) => {
    let errorMsg = err; 
    console.log(errorMsg);
  });         
}

export function button37_click(event) { 
  $w("#dataset1").refresh();
}

Hope this works out for you!

wow. in one post You explained me what i would learn few weeks myself. Whats more You helped me with few more things i will change in other codes on the page. Really Thank You for Your help.

@ceglarnia Not a problem. If this answers your question can you mark my answer with Top Comment so others can find it?

Many Thanks!
Steve

@stevendc I did just after i read it :slight_smile: Is it possible to help me one more thing. I want to change menu buttons depends on role. But only roles i can find is memebr, admin, visitor . I have roles like clients, workers, and others. Is it possible to find somewhere in code other rolels than member, visitor. Greetings Chris

@ceglarnia Check these articles out. They should help.

Steve

Maybe someone like it and use it. If You want have menu dopend on user. I made group of buttons for each role and hide it on site. When You log in depends on role You got different menu.

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

$w.onReady( () => {
setupPage();
wixUsers.onLogin( (user) => {
setupPage();
} );
} );

export function setupPage() {
let user = wixUsers.currentUser;
user.getRoles()
.then( (roles) => {
let firstRole = roles[0];
let roleName = firstRole.name; // “Role Name”
let roleDescription = firstRole.description; // “Role Description”

if (roleName === “role1”) {
$w(“#menu”).hide();
$w(“#menurol1”).show();
$w(“#menurole2”).hide();
}
if (roleName === “role2”) {
$w(“#menu”).hide();
$w(“#menurole2”).show();
$w(“#menurol1”).hide();
}
})}

@stevendc sorry for disturbing but You wrote me last time so clear and intersting the solution that i ask for some help. https://www.wix.com/code/home/forum/community-discussion/please-help-with-query-of-two-collecions-combined