Conditional use of Custom Template

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.

Conditional use of Custom Template

Postby sangnandar » Wed Jan 10, 2018 7:24 pm

This code,
View Page, Client Scripts:
$(document).on("rendertemplate", function(e, args) {
if (args.template) {
if (args.id == "tpd_<tablename>") { // with or without this check
args.enabled = false;
ew_ShowTemplates(args.template.substr(4));
}
}
});
Didn't work, threw the error:
> Uncaught TypeError: Cannot read property 'substr' of undefined
>
> console.log(args.template.substr(4)); // output: (mytablename)view
sangnandar
 
Posts: 296
Joined: Mon Sep 19, 2016 2:04 am

Re: Conditional use of Custom Template

Postby Webmaster » Thu Jan 11, 2018 10:51 am

1. If (args.template) is defined, args.template.substr(4) will not possibly cause "cannot read property 'substr' of undefined". You probably have another old handler you forgot to remove, double check HTML in your browser and look for "rendertemplate". However, note that "tpd_<tablename>" should be "tpd_<tablename>view" for View page, view HTML source in browser to check the id.
2. In v2018, the CSS class "hidden" is used to hide the original table, you also need to remove the class after calling ew_ShowTemplates(), view HTML source in browser to check the class name.
Webmaster
 
Posts: 10734
Joined: Fri Aug 06, 2004 5:59 pm

Re: Conditional use of Custom Template

Postby sangnandar » Thu Jan 11, 2018 1:08 pm

What's the point of calling ew_ShowTemplates() (that is to show), but yet again has to remove the class manually (in order to show)? I assumed that it should (logically) has been done within ew_ShowTemplates() so I didn't check the class.

I found another problem applying the scenario on List Page.
The ListOption items didn't drawn properly, always drawn on the left, both at table head and body (didn't check footer).

This is my working code to fix the problem in case else need it.

$(document).on("rendertemplate", function(e, args) {
if (ewVar.userlevel == -1) { // the condition
if (args.template) {
args.enabled = false;
var tablename = args.template.substr(4);
ew_ShowTemplates(tablename);
$("#tbl_" + tablename).removeClass("hidden");
// fix header
var that = $("#tbl_" + tablename + ">thead>tr");
$("#tbl_" + tablename + ">thead>tr").find(".ewListOptionHeader").each(function(){
$(this).detach().appendTo(that);
});
that.children("span." + tablename).remove();
// fix body
$("#tbl_" + tablename + ">tbody>tr").each(function(){
var those = $(this);
$(this).find(".ewListOptionBody").each(function(){
$(this).detach().appendTo(those);
});
});
}
}
});

Adjust .appendTo() method to .prependTo() if you do link on the left side.
sangnandar
 
Posts: 296
Joined: Mon Sep 19, 2016 2:04 am


Return to General Discussion (PHPMaker)