
All Snippets
Features
Sorting using radio or dropdown elements
Sort in ascending or descending order
New to our site? Please see our quick tutorial on how to work with our snippets
Setting | Description |
|---|---|
datasetId | Dataset id |
elementId | id of the input field. You can use a dropdown or radio buttons |
options | List of fields to sort. None is automatically added. |
option.label | Display text for option |
option.value | FieldIds of fields to sort. To sort in descending order, add __desc (double underscore desc) to the FieldId |
$w.onReady(() => {
DatasetSort({
elementId: '#dropdown1',
datasetId: '#dataset1',
options: [
{ label: 'First Name (A - Z)', value: 'firstName' },
{ label: 'First Name (Z - A)', value: 'firstName__desc' },
{ label: 'Last Name', value: 'lastName' },
{ label: 'Recently Added', value: '_createdDate' }
]
})
})
async function DatasetSort({datasetId:n,elementId:e,onChange:t=null,options:a}){
if(!e||!n)return;const o=$w(e);if(!o?.id)return;const s=$w(n);if(!s?.id)return
;await s.onReadyAsync();const i=require("wix-data");o.options=[...a,{
label:"None",value:"_none"}];var c=async()=>{const[n,e]=o.value.split("__")
;await l(s,n,e),"function"==typeof t&&t()}
;o.onChange(c),o.value&&o.value.length&&c();const l=async(n,e,t)=>{
let a=i.sort()
;if(e&&"_none"!==e)if("desc"===t)a=a.descending(e);else a=a.ascending(e)
;await n.setSort(a)}}

Title

Title
