How to check number of items in Lookup field?

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
SRanade
User
Posts: 95

How to check number of items in Lookup field?

Post by SRanade »

I'm showing a lookup field using Radio buttons instead of Dropdown Select as this is easier to change on mobile phones.

When there are some options to show it appears fine. But when there are no options available to select, then only the help string appears in place of radio buttons. I want to place a friendly message instead. So I search for how many radio buttons are available on the page, like this:

$(document).ready(function() {
   	if ($('[name^="x_timeslot_id_"]').length == 0) {
   		console.log("No Options!");
   	};
});

But no radio buttons are found here. It seems the actual list of radio buttons is loaded by Ajax, so the list is not available from inside $(document).ready. I can find the list if I search when some item is clicked, so the code is fine.

How to check in Document Ready function (or elsewhere) if there are zero options, so that I can put a friendly message?

What would be the best way to place a message there to state that there are no options available?

Thank you for help!


mobhar
User
Posts: 11660

Post by mobhar »

I don't think it's a good idea to display the form to end-users with empty options/values in one of the fields.

So, my closest approach is to prevent end-users see that form in when they are trying to open it from List Page.

Let's say, we want to check the number of items in categories table from products table. If no items available in categories table, then display failure message to end-users, and redirect it back to List Page of products table.

You may simply put this following code in Page_Render server event that belongs to the Add Page of products table:

    $lookup_count = ExecuteScalar("SELECT COUNT(*) FROM categories"); // check the number of items
    if (empty($lookup_count)) { // if empty, then display message
    	$this->setFailureMessage("Category items are still empty in its table, please enter it first from the related menu!");
    	$this->terminate("productslist"); // redirect back to List page
    }

SRanade
User
Posts: 95

Post by SRanade »

Thank you! That was most helpful.

When using a Dropdown Select, one can choose NONE as an option, and so set the option to NULL value in the field of the Lookup table.

But when using Radio buttons, is there a way to add NONE option or to uncheck the Radio button so as to put NULL value in the Lookup table?


mobhar
User
Posts: 11660

Post by mobhar »

You may actually add special option into your Lookup Table, for example, value = "NONE", string = "None", and use it as your default value, after that, in Row_Inserted server event, you update this field to NULL only if the selected option is "NONE".


SRanade
User
Posts: 95

Post by SRanade »

Now when I want it to appear on Radio Buttons, I add include the item "NONE" using a special filter.

  1. But how to make this NONE item selected during initial display?

  2. In which event to check for this selection to make the field NULL again?


mobhar
User
Posts: 11660

Post by mobhar »

SRanade wrote:

  1. But how to make this NONE item selected during initial display?

Simply enter this following string into Fields setup -> Add Page -> Default Value (make sure with double quotes characters):

"NONE"

SRanade wrote:

In which event to check for this selection to make the field NULL again?

mobhar wrote:

in Row_Inserted server event, you update this field to NULL only if the selected option is "NONE".


SRanade
User
Posts: 95

Post by SRanade »

mobhar wrote:

    $lookup_count = ExecuteScalar("SELECT COUNT(*) FROM categories"); // check the number of items
    if (empty($lookup_count)) { // if empty, then display message
    	$this->setFailureMessage("Category items are still empty in its table, please enter it first from the related menu!");
    	$this->terminate("productslist"); // redirect back to List page
    }

This is working very well. But when I need to call the same Page from two different lists, is there a way to detect from which list the call came so that the terminate command can return to the correct list page?


mobhar
User
Posts: 11660

Post by mobhar »

You may post a real example so others could comprehend the situation straightforward.


SRanade
User
Posts: 95

Post by SRanade »

I'm calling a modal Options edit page from a special button in ProductsList1. Whenever the Options are not available, I cancel the Options page from inside Page_Render of Edit page event with:

if (empty($lookup_count)) { // if empty, then display message
    	$this->setFailureMessage("Category items are still empty in its table, please enter it first from the related menu!");
   	$this->terminate("productslist1"); // redirect back to ProductsList 1 page
}

But I have a separate ProductsList2 for Admins from where also I can call the modal Options page via special button. In this case when the cancellation takes place inside Page_Render of Edit page, I need to use redirect to the Admins list #2 and not the list #1:

 	$this->terminate("productslist2"); // redirect to *Admins ProductsList 2* page

The question is: how can the Options Page_Render event detect from which ProductsList page the Options edit was called?


mobhar
User
Posts: 11660

Post by mobhar »

Before system redirect to another page by using teminate method, you may save the identity of current page to a session variable, after that, use that session variable in the destination page to distinguish which page the call came from.


SRanade
User
Posts: 95

Post by SRanade »

I've tried setting a global variable as well as setting a session variable in Page_Load as well as in Page_Render.

Then in Page_Render when I read the global variable or the session variable, I always get a blank value.

Code for sessions variable:

function Page_Load()
{
$session["current_list_page"] = "productslist1";
$current_list_page = $session["current_list_page"];
log("Page_Load set current_list_page=$current_list_page");
}

In Page_Render I check:

$current_list_page = $session["current_list_page"];
log("Page_Render found current_list_page==$current_list_page");

and get empty value.

What am I doing wrong?


SRanade
User
Posts: 95

Post by SRanade »

Found my mistake. To use $session to access variables one has to first

$session = Session(); // get the session helper object

So better way is to access sessions variables directly with:

Session("current_list_page", "productslist1"); //set
$current_list_page = Session("current_list_page"); //get

Working now! :D


Post Reply