Getting an array with getSelectedRecords() from a foundset

Hello,

I am using a foundset and I need to get a single array from it. Apparently foundset.getSelectedRecords() would return an array provided I set foundset.multiSelect=true; for my foundset. However, when I run the code

var my_array = my_foundset.getSelectedRecords()

my_array contains only one record. Any help?

I assume you have more than one record selected when you call your method?

Just did a short test here and it works just fine.

Yes, my_foundset has already been populated and contains more than 5,000 lines when the method is called.

Perhaps there is something I miss. If the same matrix of data is retrieved from the database and inserted into a dataset object, I can run the following code

var my_array = my_dataset.getColumnAsArray(i)

where (i) is the field I want, and it works fine (I get a column vector of my matrix of data as an array). Here I fail to get the same result.

I am using Servoy 5.2

my impression is that you are not selecting anything. One record is always the selected record. If you have multi select turned on, you actually need to select more than one record! You can do that by control-clicking (or Apple-clicking) several rows in your table. Just like any multi-select…

To be honest, I do not understand your reply. I do not need to select anything with my keybord, I have a foundset object that contains several rows, each row comprised of several fields, and I need to select multiple rows from it. Of course the code is supposed to do that for me.

Thank you anyways, but I solved the problem with a loop… :| . The following code works, meaning that it accomplishes what I wanted to obtain. However, it requires more code than what could possibly (?) done with getSelectedRecords().

var my_array=new Array
for (line=1;line<=max_line_number;line++){
	my_array[line-1]=my_foundset.getRecord(line).my_field
}

getSelectedRecords() returns an Array of the records that are selected in the foundset. If you load 50 records in a form, only one is selected. You (the user) can select more than that one by ctrl-clicking on some others. Then, getSelectedRecords() will return more that one record.

After having read your initial question a bit better, I see that you don’t want the selected records really but one value of all records in an array. getSelectedRecords() is the wrong approach for that. Imagine you want to allow your user to delete several records at once. Then getSelectedRecords() comes in. It will return all the records that the user has selected and you can then remove these.

Your code is the way to go, basically. It will fail, however, if your foundset has more than 200 records, because the method will stop at max_line_number, although there might be more records that have just not been transferred to the client yet. The way to go is

var myArray = new Array();
for ( var i = 1 ; i <= foundset.getSize() ; i ++ ) {
myArray.push(foundset.getRecord(i).my_field);
}

The relevant difference is that foundset.getSize() is used to limit the loop and not a static value. The neat thing about foundset.getSize() is that it updates itself once the end of the fetch size has been reached. Servoy always fetches data in chunks of 200 records. If you scroll down, more records are loaded. In that scenario, the size of the foundset is initially 200 and once the 200 is reached, is updated to 400, 600, etc. But if you do

var max_line_number = foundset.getSize()

your max_line_number will be 200 and never being updated. So you will miss all records that come after record 200.

Cool! You got what I wanted and I got your reply now :D . I will do as you suggest.

It works perfectly. Thank you Patrick!