Not too much of a coder so sorry for any lack of knowledge but I am currently looking for a way to have a field (code) in a dataset (Attributes) increase by 1 for each submission. I have tried my hand at it so here is my code:
import wixData from 'wix-data';
export function Attributes_beforeInsert(item, code)
{
let numberOfItems;
let hookContext = code;
wixData.query("Attributes")
.find()
.then((results) => {
numberOfItems = results.items.length // this represents the number of items in the database collection.
item.code = (numberOfItems + 1 ).toString();
});
return item;
}
I, unfortunately, get this in the dataset column called “code”:
{
"isFulfilled": false,
"isRejected": false
}
There my searching I found code to generate a random number in a field and that is what my own code is based on. That code is from here:
In short, need help in making a column increase by 1 with each form submission.
After reading your issued situation teile i did not really understand what is exactly your aim?
All i understood ist that you need a column-increment by 1, after each submission-process. But did not get any explanation, why you need this function.
I assume that you want to write data instead of doing it verticaly, you want to write data horizontaly, right?
So than i did not understand why you are counting rows.
How ever, if you want to write data horizontaly, you will need for example the insert-method.
But before i continue, i will first await your reply.
Sorry, sorry I will explain as best I can.
So I have a dynamic dataset call Attributes that comes with 3 fields (or columns) those fields are “Attribute Name”, “Health Modifer”, and “Passive”. I also have a form that certain users can use to submit a new Attribute by filling out those input boxes and submitting. The new Attribute will then be displayed on a dynamic page. All of this works as intended.
The challenge is I want each Attribute to be given a number when submitted. This number should be based on the number of the Attribute before it. For example, if there are currently 3 Attributes in the dataset the next Attribute to be added will be numbered 4 in a text or number field/column.
(The circled column but automatically.)
Having attribute numbers update automatically if one is deleted is also on my mind but not a high priority. Again, I have limited coding knowledge so I have been scouring the internet and puling parts that work while using my current skills to try and make a stitch job.
Then i understood your situation the wrong way.
Take a look onto this one…
Autoincrement dataset column
This is an example, if you want to go the DATASET-way.
$w.onReady( () => {
$w("#myDatasetIDhere").onReady(()=> {
$w('#myButtonIDhere').onClick(()=>{
let numberOfItems = $w("#myDatasetIDhere").getTotalCount();
numberOfItems = numberOfItems+1; console.log(numberOfItems)
$w("#myDatasetIDhere").setFieldValue("attnum", numberOfItems);
$w("#myDatasetIDhere").save();
});
});
});
Perhaps it could be, that you will need this part, too…
$w("#myDataset").add()
.then( ( )=> {
console.log("New item added");
})
.catch( (err) => {let errMsg = err;});
Normaly i would use an INSERT(), or SAVE(), or something like UPDATE(),
but here you are using a HOOK (which i am not really familiar with).
Perhaps this one will work, who knows.
import wixData from 'wix-data';
export function Attributes_beforeInsert(item, code) {
let numOfItems;
let hookContext = code;
wixData.query("Attributes")
.find()
.then((results) => {
let items = results.items; console.log(items);
let firstItem = items[0]; console.log(firstItem);
let numOfItems = results.items.length; console.log(numOfItems);
numOfItems = numOfItems+1 console.log(numOfItems);
item.attnum = numberOfItems
return item;
});
}
@russian-dima So I have tried using both ways and the DATASET-way works.
Got pretty excited at least seeing progress
. Now I just need to have them combine properly. You said you would normally use an INSERT(), or SAVE(), or something like UPDATE() so I shall try using those.
So I have decided to use code behind the form instead of the standard Wix data connections! It all works as I want to now! Thanks a bunch for your help and here is a pic of what I have done:
import wixData from 'wix-data';
$w.onReady( () => {
$w("#dataset5").onReady(()=> {
$w('#button5').onClick(()=>{
let numberOfItems = $w("#dataset5").getTotalCount();
numberOfItems = numberOfItems+1; console.log(numberOfItems)
$w("#dataset5").setFieldValue("attnum", numberOfItems);
$w("#input32").onInput( (event) => {
let newValue = event.target.value;
$w("#dataset5").setFieldValue("attributeName", newValue);})
$w("#input33").onInput( (event) => {
let newValue2 = event.target.value;
$w("#dataset5").setFieldValue("healthModifier", newValue2);})
$w("#input30").onInput( (event) => {
let newValue3 = event.target.value;
$w("#dataset5").setFieldValue("passive", newValue3);})
$w("#dataset5").save();
});
});
});
EDIT: Does not work completely but I can work around it relatively well.