About Merging
Sometimes certain data is collected as individual fields but then this data may need to be combined to be displayed together. For example, you may collect firstName and lastName but would like to display fullName.
There are copious ways of scenarios and combinations to accomplish this but we will show you the most popular codes used on the Wix platform using Corvid.
Good to Know #1
Combine data from a dynamic dataset
Our example assumes your dynamic dataset is for a single item details page instead of a dynamic category or multi item dataset. We will be getting the current item to obtain a few pieces of values from certain fields. We will combine those values to display a custom string of text on the page.
The elements we used in this example:
1 Dataset / #dynamicDataset
1 Text element not connected to dataset / #titleLine
1 field from the database used to search / 'firstName'
1 field from the database used to search / 'lastName'
1 field from the database used to search / 'specialty'
Code for the single item dynamic page
$w.onReady(function () {
const currentItem = $w('#dynamicDataset').getCurrentItem();
$w('#titleLine').text = `Meet ${currentItem.firstName} ${currentItem.lastName}. ${currentItem.firstName} specializes in ${currentItem.specialty}. `;
});
Alternate way of writing the same code
$w.onReady(function () {
const currentItem = $w('#dynamicDataset').getCurrentItem();
let first = currentItem.firstName;
let last = currentItem.lastName;
let special = currentItem.specialty;
$w('#titleLine').text = "Meet " + first + " " + last + ". " + first + " specializes in " + special + ". ";
});
Good to Know #2
Combine data from a repeater connected to a dataset
We will be getting the current repeater item to obtain a few pieces of values from certain fields We will combine those values to display a custom string of text on the page.
The elements we used in this example:
1 Dataset / #dataset1
1 Repeter element connected to a dataset / #repeater1
1 Text element in a repeater not connected to dataset / #titleLine
1 field from the database used to search / 'firstName'
1 field from the database used to search / 'lastName'
1 field from the database used to search / 'specialty'
Code for single repeater item
$w.onReady(() => {
$w("#dataset1").onReady(() => {
$w("#repeater1").onItemReady(($item, itemData, index) => {
$item('#titleLine').text = `Meet ${itemData.firstName} ${itemData.lastName}. ${itemData.firstName} specializes in ${itemData.specialty}. `;
});
});
});
Alternate way of writing the same code
$w.onReady(() => {
$w("#dataset1").onReady(() => {
$w("#repeater1").onItemReady(($item, itemData, index) => {
let first = itemData.firstName;
let last = itemData.lastName;
let special = itemData.specialty;
$w('#titleLine').text = "Meet " + first + " " + last + ". " + first + " specializes in " + special + ". ";
});
});
});
Good to Know #3
Combine data to set as value before Dataset saves
Our example assumes you are creating or updating a record in your database collection. Please make sure your permissions and logic matches our code example, otherwise modify the code as needed. For example, you may or may not want to connected each individual text input element to the dataset. Or you may be combining data obtained from storage session or other value that is not located in a text input element. We will be getting certain values from multiple input text elements. We will combine those values to set them as a value in the dataset before it saves. These values will not be displayed on the page.
The elements we used in this example:
1 Dataset / #dynamicDataset
1 Text input element / #firstNameText
1 Text input element / #lastNameText
1 field from the database to save combined value / 'fullName'
The Code
$w.onReady(() => {
$w("#dataset1").onBeforeSave(() => {
let first = $w('#firstNameText').value;
let last = $w('#lastNameText').value;
let full = first + " " + last
$w("#dataset1").setFieldValue("fullName", full);
});
});
If you need to save more than 1 value, please read the Corvid Reference to view the correct syntax and example code for that: https://www.wix.com/corvid/reference/wix-dataset/dataset/setfieldvalues
Author
by Code Queen
Stuck on a project? Hire Code Queen, LLC!
Schedule a phone call or video call directly online. In a different time zone? No problem! Code Queen currently has clients around the world.
Online Booking: Discovery Session
Contact Form: Send project details
Comments