Master Details Hide Second Tab on Condition

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

Master Details Hide Second Tab on Condition

Post by johnberman »

Hi

So I have a Master Details, Details setup

If in the master lookup I add to Client Scripts \ List Page \ Client Script

$(document).ready(function() {
$('[data-table="boam"]').hide();
});

The Tab is hidden so far so good

Now I only want the tab hidden if there are no records so I need some for of

If no record then

$(document).ready(function() {
$('[data-table="boam"]').hide();
}

Else

Do nothing

);

Pointers appreciated

Regards
John B


mobhar
User
Posts: 11660

Post by mobhar »

I've just found out the solution, but in this case I am using "orders" (master table), "orderdetails" and "order details extended" (detail tables) from demo project. I've already tried this, and it works properly as expected.

How it works? When you click on "+" (preview) button, then it will hide the second tab (which is "order details extended" table) only if that detail table does not contain any records.

Simply put the following code in "Startup Script" of "List Page" that belongs to "orders" table:

// this is when the preview area is being displayed
$(document).on("preview", function(e, args) {
var $tabpane = args.$tabpane;
var s = $tabpane.find("div:has(.ewAddEditOption)").find("a").attr("href");
var dt = $(".ewTablePreviewRow td ul li a").attr("data-target"); // we need this since each preview row area has a unique key
if (s) { // only if s is not an empty string, it means it returns the tabs
var myKey = s.substr(s.lastIndexOf("=") + 1); // get the record id, in this example, get from "OrderID" field in "order details extended" view
var val = ew_Ajax("<?php echo ew_Encrypt("SELECT COUNT(*) FROM order details extended WHERE OrderID = '{query_value}'"); ?>", myKey); // get the record count by using AJAX
if (val < 1) { // if the record count is less than one, it means no records are found
var hideThis = { // declare multiple attribute, we need this since the target tab is identified based on these both attributes
'data-table': ['order_details_extended'],
'data-target': ['' + dt + '']
};
$('tr > td > ul > li >')
.filter(function() {
for (var i in hideThis) {
var attr = $(this).attr(i),
match = ('|' + hideThis[i].join('|') + '|').indexOf(attr) > -1;
if (!match) {
return false;
}
}
return true;
})
.hide();
}
}
});


johnberman
User
Posts: 210

Post by johnberman »

Im sure its me

my master tabel is
duration_lookup

Details
duration_matched

Extended details
boam

the link between duration_lookup and boam is the field match_id

there are always records in duration_matched

boam there is either 1 record or no records

the script is placed in

Client Scripts \ List Page \ Startup Script of the table called duration_lookup - which is the master table

and the script is as follows

// this is when the preview area is being displayed
$(document).on("preview", function(e, args) {
var $tabpane = args.$tabpane;
var s = $tabpane.find("div:has(.ewAddEditOption)").find("a").attr("href");
var dt = $(".ewTablePreviewRow td ul li a").attr("data-target"); // we need this since each preview row area has a unique key
if (s) { // only if s is not an empty string, it means it returns the tabs
var myKey = s.substr(s.lastIndexOf("=") + 1); // get the record id, in this example, get from "OrderID" field in "order details extended" view
var val = ew_Ajax("<?php echo ew_Encrypt("SELECT COUNT(*) FROM boam WHERE match_id = '{query_value}'"); ?>", myKey); // get the record count by using AJAX
if (val < 1) { // if the record count is less than one, it means no records are found
var hideThis = { // declare multiple attribute, we need this since the target tab is identified based on these both attributes
'data-table': ['boam'],
'data-target': ['' + dt + '']
};
$('tr > td > ul > li >')
.filter(function() {
for (var i in hideThis) {
var attr = $(this).attr(i),
match = ('|' + hideThis[i].join('|') + '|').indexOf(attr) > -1;
if (!match) {
return false;
}
}
return true;
})
.hide();
}
}
});

the boam tab is not hidden if there are no records

Regards

John B


johnberman
User
Posts: 210

Post by johnberman »

Tables below are the

Master - duration_lookup
Details1 - duration_matched
Details2 - boam

Master/Detail relationships
duration_lookup.'match_id' = duration_matched.'match_id'
duration_lookup.'match_id' = boam.'match_id'

below each table I have listed some records and made comments

CREATE TABLE duration_lookup (
event_id int(11) NOT NULL,
date date default NULL,
Time time default NULL,
match_id int(11) default NULL,
group int(11) default NULL,
rdscore varchar(255) default NULL,
score int(11) default NULL,
PRIMARY KEY (event_id)

event_id,date,time,match_id, group
73200, 2017-01-01,10:08:10,5,5

event_id,date,time,match_id, group
48042, 2017-01-03,00:25:51:10,10

CREATE TABLE duration_matched (
event_id int(11) NOT NULL,
user_ID int(11) default NULL,
date date default NULL,
Time time default NULL,
match_id int(11) default NULL,
group int(11) default NULL,
score int(11) default NULL,
PRIMARY KEY (event_id)

the link from duration lookup finds 4 records in duration_matchedwhere the match_id = 5
event_id,date,time,match_id, group
73200,2017-01-01,10:08:10,5,5
64144,2017-01-01,10:08:10,5,5
52471,2017-01-01,10:08:10,5,5
47876,2017-01-01,10:08:10,5,5

the link from duration lookup finds 4 records in duration_matched where the match_id = 10
event_id,date,time,match_id, group
48042, 2017-01-03,00:25:51:10,10
52681,2017-01-03,00:25:51:10,10
7337,2017-01-03,00:25:51:10,10
60743,2017-01-03,00:25:51:10,10

CREATE TABLE boam (
Date date default NULL,
Time time default NULL,
camera varchar(255) default NULL,
match_id int(11) NOT NULL,
video varchar(255) default NULL,
boam_id int(11) NOT NULL,
PRIMARY KEY (match_id)

the duration lookup on match_id find no records in boam with a match_id=5
boam_id,date,time,match_id,video

the duration lookup on match_id finds 1 record in boam with a match_id=10
boam_id,date,time,match_id,video
4,2017-01-03,00:25:51,10,0

There wil allways be records found in duration_matched (the first tab) and sometimes there will be records in boam (the second tab)

but the script to hide the boam tab seems to do nothing and the tab is displayed even when there are no records

Regards
John B


mobhar
User
Posts: 11660

Post by mobhar »

I've just populated the tables schema and the records, and then implement the latest code you posted above, and it works properly.

Here are the results:

  1. When the PreviewRow button is clicked on the first record of "duration_lookup" master table, then the second tab ("boam") will be hidden, since there are no records in "boam" detail table.
  2. When the PreviewRow button is clicked on the second record of "duration_lookup" master table, then the second tab ("boam") will not be hidden, since there is one record in "boam" detail table.

Webmaster
User
Posts: 9425

Post by Webmaster »

If you have enabled "Detail record count", the tab will show the record count, so in your "preview" event you just need to check if the tab contains '<span dir="ltr">(0)</span>' and hide it. No need to get record count by Ajax. If the tab to be hidden is the active one (the first tab), you need to remove the "active" class and set "active" class to another tab (e.g. the second tab), if any.


strustam
User
Posts: 33
Location: Tajikistan

Post by strustam »

Hi

As you recommended to check if the tab contains '<span dir="ltr">(0)</span>' I put following code to Client Scripts \ List Page \ Startup Script

$(document).on("preview", function(e, args) {

$('.nav-tabs li.active').removeClass('active');
$('.ewTablePreviewRow td ul li a span').filter(function(){return $(this).text() != "(0)";}).parent().parent().addClass('active');
$('.ewTablePreviewRow td ul li a span').filter(function(){return $(this).text() == "(0)";}).parent().parent().hide();

});

It is hide tabs with (0) and set "active" other, but no table in active tabs.

Please advice what I should do to show second tab data

Regards,
STRustam


Webmaster
User
Posts: 9425

Post by Webmaster »

Webmaster wrote:
set "active" class to another tab (e.g. the second tab), if any.

Another (single) tab, not all other tabs as you wrote:

$('.ewTablePreviewRow td ul li a span').filter(function(){return $(this).text() != "(0)";}).parent().parent().addClass('active');


strustam
User
Posts: 33
Location: Tajikistan

Post by strustam »

Hi!

I have master hd_personal and 3 detail hd_uk, hd_koap, hd_gk. For a record in master just one of detail has single record. Counts detail in preview looks like 1-0-0 or 0-1-0 or 0-0-1. By above code there was no double active li. Required tab became active, but no data on tab-content. For situation, when 1st tab has data (count details 1-0-0) - everything is good, 2nd and 3rd tab became hidden. For situation, when 1st tab has no data (count details 0-1-0 or 0-0-1) - Tab became active and Tab-content still show data of first tab.

I thought, if I make active other tab, content will automatically changed.


Please advice, how I can change tab-content.

Regards,
STRustam


Webmaster
User
Posts: 9425

Post by Webmaster »

Use .click() to select the first tab with data.


Post Reply