Servoy 7 Conditional HTML in LIST or TABLE?

I know that I’m definitely getting older - as I know that there is a way to do this - but can’t quite seem to figure it out…

I have either a LIST or TABLE - and I want to conditionally show a button based on whether or not the current record is the first one - using a HTML calculation.

In the “old days” - you could just reference the controller.getSelectedIndex() and then if it was not the first row, then return some HTML, if it was - then return null.

However, since 7.x “tightened up” access to objects in the calc engine - it doesn’t seem to work.

I’m looking to do something like this:

if(controller.getSelectedIndex() > 1) {
		return '<html><head></head><body><img src="media:///cross-button.png" border="0" width="16" height="16"></body></html>'
	} else {
		return null;
	}

I know that the onRender will work on formatting and the row/selected index color works in CSS… but this one has me at a loss.

You could probably use a global to get the selected index:

function getIndex() {
return forms.myForm.controller.getSelectedIndex();
}

and your calculation would look like:

if(globals.getIndex() > 1) {
return ‘
} else {
return null;
}

Thanks, Peter!

However, it won’t work. The reason is that is has no context - so you get the last number that the controller drew. So if you have 10 records - then the result (for each row) is “10”. If you have 2 rows, then they all evaluate to “2”.

I appreciate the reply, though! :D

Bob

Heh, yeah now I can see how that wouldn’t make much sense. What if you passed the pk of that record and did a lookup of its index?

you would call getIndex(rowPK) and getIndex would look more like:

function getIndex(pk) {
forms.myForm.foundset.selectRecord(pk);
return forms.myForm.foundset.getSelectedIndex();
}

Again - thanks, Peter!

However, this will ALWAYS return “1” - since you are telling the foundset to go to that row, and then get the number of the row in that foundset (which is always 1). :D

Bob

selectRecord doesn’t actually do what I thought it did. :oops:

If you don’t have to use an HTML calculation, you can put a method on onRecordSelection that does the check for you then hides/shows the button. But beyond that, I am not sure how else to accomplish this. Sorry I couldn’t be of more help Bob.

Peter - no problem! I really appreciate you taking the “head time” to offer suggestions! :D

It does need to be a HTML calculation - because I want to conditionally show a graphic. The way list/tableview works is only one “model” is created for every row. If you “hide” a field - it’s hidden for the entire list/table, not just a single row. If you use the calculation workaround - then you can conditionally make a graphic “appear” on selected rows (as long as the calculation is based on some other criteria than the number of the current record in the foundset).

Bob

Hi Bob,

I think you need to use the onRender method for this in combination with a in-memory column (calc that returns itself).
Create an un-stored calc like so:

function c_myBtn() {
     return; // this will make it an in-memory column
}

Now go to your form and place a label on the body form-part and lets say we name it ‘lblBtn’ and add the calc as a data provider.

Then add the following onRender method to the onRender form event:

function onRender(event) {
    // check if we are rendering the field/label we want to render
    if (event.getRenderable().getName() == 'lblBtn') {
        var _rc = event.getRecord();
        // reset the value in the in-memory column
        _rc[event.getRenderable().getDataProviderID()] = '';
        if (event.isRecordSelected() && event.getRecordIndex() > 1) {
            // selected record is not the first row
            _rc[event.getRenderable().getDataProviderID()] = '<html><head></head><body><img src="media:///cross-button.png" border="0" width="16" height="16"></body></html>';
        }
    }
}

This will then draw the cross-button on the selected row for all but the first row.

Hope this helps.

Related to this thread, please vote up: https://support.servoy.com/browse/SVY-4775

I don’t see how that issue of hiding/showing a whole column is in anyway related to getting a calculation to produce HTML depending on the selected record.

Still something to vote for though.

Ah yea, you are correct. Well, here’s an onRender related ticket we have open as well: https://support.servoy.com/browse/SVY-4729. Also not very related but if you put both together…? :)

Thanks for the vote.

onRender is very problematic indeed. I also filed an issue for it. It’s being worked on as we speak (at least it’s marked as in-progress).
https://support.servoy.com/browse/SVY-4757

Lets hope this will then fix some other (perhaps) related issues as well.

Good one.

Thanks very much guys!

It never ceases to amaze me how AWESOME this community is! Hopefully, in the future, it will be easier to do than go through all those gyrations… :shock:

Again - many thanks, guys! :D

I found another way

return foundset && foundset.getSelectedIndex() > 1 ? '<html><head></head><body><img src="media:///cross-button.png" border="0" width="16" height="16"></body></html>' : null; 

Only problem is you need a databaseManager.recalculate(foundset); in the onRecordSelection, so maybe it’s not a very good idea.
It shows a warning when using foundset inside the calculation, but it works.

ROCLASI:
onRender is very problematic indeed. I also filed an issue for it. It’s being worked on as we speak (at least it’s marked as in-progress).
https://support.servoy.com/browse/SVY-4757

Lets hope this will then fix some other (perhaps) related issues as well.

FYI, a fix was committed today to trunk (7.2) and 6.1.x

Thanks, Robert - good to know!