Getting first row of a column

let filteredTable = $w(‘#dataset3’).setFilter(wixData.filter()
.le(“minColumn”, number)
.ge(“maxColumn”, number))

I want to get the first value of each of those columns.
So something like (aware this doesn’t work but just to show roughly what I’d like).
minColumnValue = filteredTable[“minColumn”][0]

Not 100% sure what you’re looking for, but to find the smallest value of a column (field in a collection), you can just sort ascending on that column - then the first row contains the smallest value for that column.

BTW - you should refer to the setFilter() API to see how to properly filter a dataset.

Bonus code showing how to find the min and max values of the field num (that’s the Field Key) in a collection:

$w.onReady(async function () {

    let results = await wixData.query("NumberList").limit(1000).find();
    let items = results.items;

    let min_num = getMinNum(items);
    console.log(min_num);

    let max_num = getMaxNum(items);
    console.log(max_num);
});

function getMinNum(items) {
    return items.reduce((min, item) => item.num < min ? item.num : min, items[0].num);
}

function getMaxNum(items) {
    return items.reduce((max, item) => item.num > max ? item.num : max, items[0].num);
}

Note 1: This might not be the most efficient method if you have a large number of items.
Note 2: You will need to have some programming knowledge for this method.

See the documentation for reduce() for more information.

Right now the table above is a table. When I set the filter I only have 1 row. I want to grab the first value (the only value) of two columns and set them as a variable. So the minColumnValue = 100 and maxColumnValue = 1000. Those are two of my column names. Seems like quite a bit of code to get something a bit straightforward. I just want the cell value.

@tomorrowviral Still not sure what you want, but maybe this is closer…

First, set the filter:

$w("#dataset3").setFilter(wixData.filter()
    .le("minColumn", number1)
    .ge("maxColumn", number2)
);

Then, get the current item from the dataset. If there is one item, then it gets that one. If there are more than one item, it will get the first:

let itemObj = $w("#dataset3").getCurrentItem();

Then from that item, get the values from the fields (columns):

let minVal = itemObj.minColumnValue;
let maxVal = itemObj.maxColumnValue;

Hopefully I got it right this time.

For more information, see the following:

  • setFilter()

  • getCurrentItem()

@yisrael-wix That seems more like it :slight_smile: I’ll give that a shot. Thanks for all the help! Much appreciated.