Dynamic field content

Hello, I am trying to let the user choose which fields he wants to have on a form (in a certain area). For example, one user wants to see the total sales, another wants to see the birthday.

I think I have done that before, but see no way right now. The idea is to let the user store up to e.g. 5 field names in his preferences and those five fields are then populated with the data of the fields that the user chose.

Any idea on how to this?

Thanks
Patrick

Oops, just found a way: if you have a calculation of the type

var field_1 = data_to_user.field_choice_1
return [field_1];

some sort of java object is returned. But when you use a self-relation like this

var field_1 = data_to_user.field_choice_1
return data_to_data[field_1];

then it works. Why isn’t the first approach coming up with the same result?

Thanks
Patrick[/quote]

why should it?
you do there something completely different!

What are you exactly returning?

Which colum of which related data are you trying to show?

Let’s say you have 3 fields “name”, “street”, “city”. In a user table you have a field “field_choice_1”. In that field the user chooses “street”. I now want to be able to display the content of the column “street” in a field. The problem is, that I have no option to dynamically set the dataprovider of a field.

First I tried with [field_choice_1] and was trying to get Servoy to display the contents of the field “street”.

Since that didn’t worked, I made a self-relation on my table from pk to pk. Then I tried with self-relation[field_choice_1] and now am able to see the field contents based on what the user chose to see.

My question was: what is the difference between [field_choice_1] and self-relation[field_choice_1].

Did I make myself understandable?

Thanks
Patrick

ahh i see. in a calculation you don’t have the controller.getDataProviderId().

But just calling [name] is illegal. That is not possible you have to have a context. maybe return this[name] works but i am not sure.

this[name] gives you

Exception executing field_choice: Cannot convert null to an object.

So the way I do it (using the self-relation) is the way to do it?

Thanks
Patrick

hmm. just saw that “my way” throws the same error but shows you the correct result. Should I worry about the error :wink:

Would this work for you?

return currentcontroller.getDataProviderValue(field_choice1)

Another way would be of course writing an event script attached to onShow ect.

That is a very interesting suggestion, because probably nobody knew, that you could “talk” with a controller from a calculation. But yes, you can. It does work this way as well!

But it also throws an error “cannot convert…” as my way above. Does this need a fix from Servoys side or can I do something to avoid the error? The results are great, anyway :P

try this:
if(field_choice1)
{
return currentcontroller.getDataProviderValue(field_choice1)
}

this checks of the field: field_choice1 is empty or null
than I think, you don’t get the error!

yes I tried. The problem is that the error is only thrown if field_choice has a value. If it is null there is no error.

first of all
using currentcontroller is very dangerous.
Because what happens if that calculation is on another form (completely not displayed or displayed in a tab??)

Then the currentcontroller doesn’t point to the table of the calculation.

about that error. I don’t know exactly when that error is exactly generated. Because you do say that it works?? You do see the right value?
Then this is very strange.

i need to have/make an example to see that.

can’t you try a eval function for this? (but i don’t know if eval will return something.. have to check:

return eval(field_choice1)
(something like that)

I thought that you didn’t enjoy me discover that you could use currentcontroller :wink:. I thought there was a good reason you left controller methods out of the calculation area and I do see the point.

But then: eval also works. But also throws an error. I will make an example solution so you can see what happens.

Thanks.
Patrick

Just figured out that the error thrown just occurs in conjunction with the method I use to choose the field. I just changed something there and now it works like a charm.

Thanks for your input. Basically all of the suggestions (including my own) seem to work smoothly.

Patrick