Conditional use of Custom Template

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

Conditional use of Custom Template

Post by sangnandar »

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


Webmaster
User
Posts: 9425

Post by Webmaster »

  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.

sangnandar
User
Posts: 980

Post by sangnandar »

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.


Post Reply