Friday, October 15, 2021

Dynamic Actions on cards in Oracle APEX cards region


Oracle APEX card templates and card regions already have quite a few options you can set via the APEX Builder.

In a card region, a card can have an action, but the options are limited to a link to another page or URL. What if you just want to click on a card and draw attention to the selected card and set some item value based on that selection? Something like this:


This approach needs some custom Dynamic Actions and CSS.


Create the card region


Let's start with the card region.

First upload some images as static application files (or any other location).

Next create a page with a card region. For this example, I simply used this query for the card region:

select 'A' combi, '#APP_IMAGES#CombiA.png' image from dual union all
select 'B''#APP_IMAGES#CombiB.png' from dual union all
select 'C''#APP_IMAGES#CombiC.png' from dual union all
select 'D''#APP_IMAGES#CombiD.png' from dual

The cards only use the Media attributes:

Source: Image URL
URL: &IMAGE.

Next, you want to make the unselected cards a bit smaller, so a selected card can get the normal card size. For that, add this class to the card:

card-unselected

(the CSS will follow later).

Add an action to the cards:

Type: Full Card
Link: Redirect to URL
Target: #

Since we do not really want to go to some other page, we need to modify the behaviour of the link a bit. To do that, set this value in the Link Attributes:

data-action=select data-value=&COMBI.


Create a Dynamic Action


Now we can create a Click Dynamic Action.

Name: Get selected card
Selection Type: jQuery Selector
jQuery Selector: [data-action='select']

The first action is a Set Value to copy the card value to a page item.

Set Type: JavaScript Expression
JavaScript Expression:  this.triggeringElement.dataset.value

The second action increases the size of the selected card to the normal size, and to make it bit more fancy, also sets a drop shadow. This action also has to reset the previously selected card to the smaller size.

Action:Execute JavaScript Code
Code
:

$(".a-CardView").removeClass( "card-selected" );
$(".a-CardView").addClass( "card-unselected" );
this.triggeringElement.parentElement.classList.remove("card-unselected");
this.triggeringElement.parentElement.classList.add("card-selected");


Add the CSS


Define CSS on the page. The padding on a-CardView-items is needed to allow box-shadow on the right most card

.card-unselected {
   height:80%;
   border-style:none;
}
.card-selected {
   height:100%;
   border-style:none;
   box-shadow: 5px 10px 8px #888888;
}
.a-CardView-items{
  padding:10px;
}


9 comments:

  1. hi, thanks to create this guide. i am trying to use Cards region for a page consists of Items and want user to click on a Card and there should be an action/da to store the selected card value in a Collection. i asked in Oracle Apex forum and a senior @InoL post this article link. i failed to set value in a page item, in this article there is no page item exists (or i misunderstood, which page item you are setting the value) where mentioned "The first action is a Set Value to copy the card value to a page item.", please help me to achieve what i want to do. regards

    ReplyDelete
  2. Look above the cards. There is an item with label "You selected option". That is the item for the Set Value.

    ReplyDelete
  3. I have tried this solution. It works nicely in apex version 21.1.0 but no longer in version 21.2.0.

    ReplyDelete
  4. Hello. Great post.Howto get values as card_title?
    Regards

    ReplyDelete
    Replies
    1. If you need help on something else than the content this blog, you'd better ask this on an Oracle APEX forum.

      Delete
  5. I have tried the same but failed, Can i get the code?I tried but failed.

    ReplyDelete
  6. There is no code involved. All steps are described here.
    There was a bug in APEX 21.2, so you need to install the latest APEX patch to get it to work in 21.2.

    ReplyDelete
  7. Hi Ino, thanks for your update. To download latest patch need CSI number. I just want to use it for learning purpose. From where I can download latest patch for 21.2 oracle apex version without CSI number. If you have any link please share or send an email on anil1063@gmail.com.

    ReplyDelete
  8. You cannot download patches without a support contract. For learning purposes you can also use apex.oracle.com or register for the Always Free Autonomous Database.

    ReplyDelete