Page 1 of 1
How do I make a subform behave as a Browse, not an Edit form?
Posted: Sat Mar 25, 2023 9:22 am
by DEMandell
Greetings,
The functionality I'm looking for is something that I thought I remembered doing in NuBuilderPro but I can't seem to figure it out in Forte.
I have a Browse Form used to select an item, and I want to pass the key for that item into a second Browse Form, having it end up in the WHERE clause in the SQL query for the second Browse Form. This is similar to what happens automatically for Subforms, but in Forte those are only Edit Forms and not Browse Forms. I could use a Run iFrame, but so far I haven't figured out how to pass the key into it. Maybe with a hash cookie?
I have lots of SQL and other programming experience, but relatively little PHP or Javascript! All assistance gratefully accepted.
Thanks,
--Douglas
Re: How do I make a subform behave as a Browse, not an Edit form?
Posted: Sat Mar 25, 2023 9:54 am
by kev1n
Hi,
What should happen when a row of the 1st Browse is clicked? Should the 2nd browse opened in the same breadcrumb or another tab?
Re: How do I make a subform behave as a Browse, not an Edit form?
Posted: Sat Mar 25, 2023 11:44 pm
by DEMandell
Either one would work, but I suppose another tab might be even more useful, particularly if previousy-clicked rows remained in open tabs, but this isn't essential.
I'm working on an application to track real estate construction and repair, and the app contains a simple accounting core within it. What I was trying to do here was to click on an account from the chart of accounts, and use the account ID to select the list of transactions associated with that account to display a register including a running balance. Being able to have the tabs for previously opened account registers stick around would be handy but not essential. The challenge with using an edit form as a grid is I don't see how to compute the running balance under those circumstances. With a browse form, it's easy using SQL.
This same pattern would be used to track lists of anything within the project-- material, labor, whatever, but those could most likely use the grid subform.
Incidentially, I had this app nearly finished in NuBuilderPro a number of years ago, but was running into some problems, and then NuBuilderForte came out. Since there wasn't an easy upgrade path, I put the project on the shelf as I had other projects to do. Now I'm back to re-writing it in Forte.
Re: How do I make a subform behave as a Browse, not an Edit form?
Posted: Sun Mar 26, 2023 6:59 am
by kev1n
In nuBuilder Forte, you can override the nuSelectBrowse(event) function to get the primary key of the selected row and perform your custom actions. Here's how you can do that:
1. Create a hash cookie to store the selected value:
In the first Browse Form, add a Custom Code with the following JavaScript:
Code: Select all
function nuSelectBrowse(e) {
const cell = $(e.target).closest('div'); // Find the closest div/cell to the clicked element
const primaryKey = cell.attr('data-nu-primary-key'); // Get the row's primary Key
nuSetProperty('selected_item_id', primaryKey); // Save the selected ID in a hash cookie
nuForm('6b4f921bb31e163', '', '', '', '1'); // Open the other Browse form in the same Tab. Replace 6b4... with your form Id
}
This code will override the default nuSelectBrowse(event) function. When you click on a row in the first Browse Form, the function will find the closest tr element to the clicked element, get the primary key from the data- attribute. Then the selected primary key is stored in a Hash Cookie and the 2nd Browse form is opened.
2. Modify the second Browse Form's SQL query:
In the second Browse Form, modify the SQL query to include the WHERE clause with the stored value from the hash cookie.
Code: Select all
SELECT *
FROM your_table
WHERE your_key_column = '#selected_item_id#'
Replace your_table and your_key_column with the appropriate table and column names from your database.
Now, when you click the custom button in the first Browse Form, it will store the selected row's key in a hash cookie and open the second Browse Form, filtering the results based on the stored key.
Re: How do I make a subform behave as a Browse, not an Edit form?
Posted: Thu Mar 30, 2023 9:07 pm
by DEMandell
OK, I'm getting closer!
I wasn't sure what you meant by "add a custom button", but I added the above function to Custom Code / Browse & Edit for to first Browse Form, and clicking on a row in the form does now bring up the second Browse form, but the selected row's primary key isn't ending up in the variable primaryKey. The second Browse form ends up with no data. However, if, for testing, I manually substitute the ID of a known row from the first form using, for example:
nuSetProperty('selected_item_id', '640d50de1ec4dce');
then the second browse forms select does correctly end up with all entries belonging to that id. This tells me that the hash cookie is getting through to the second browse, but that in the custom code it isn't picking up the selected row's primary key.
So what am I doing wrong?
Thanks!
Re: How do I make a subform behave as a Browse, not an Edit form?
Posted: Thu Mar 30, 2023 9:09 pm
by kev1n
I updated the nuSelectBrowse() function above.