Page 1 of 1

Hide some fields from LIST,ADD,VIEW and EDIT pages from a comma separated string

Posted: Thu Nov 03, 2022 12:42 am
by Andros

Hello, in my solution for a given table I have a HiddenFields string field which contains the comma separated list of fields that I want to hide from LIST, VIEW, ADD and EDIT pages.
For example for customers table (CustomerID,FirstName,LastName,CustomerLevelID,NickName,...) if I have the HiddenFields = "CustomerLevelID,NickName" I have to set CustomerLevelID.Visible and NickName.Visible to false.
I want to iterate the comma separated list and extract the field names: from the string field name (e.g. myFieldName="CustomerLevelID") how can I set the Visible property to false? something like CurrentTable.Field[myFieldName].Visible=false.
Thanx.


Re: Hide some fields from LIST,ADD,VIEW and EDIT pages from a comma separated string

Posted: Thu Nov 03, 2022 7:22 am
by MichaelG

To get a field by name and set Visible to false, use:

var field = FieldByName(name);
field?.Visible = false;

Re: Hide some fields from LIST,ADD,VIEW and EDIT pages from a comma separated string

Posted: Fri Nov 11, 2022 12:59 am
by Andros

Perfect it works, thank you.

Now I have this code in Page_DataRendering event for EVERY TABLE I need.

I tried to put the code in a GLOBAL FUNCTION, but it gives me the error that FieldByName is not available there.

How can I run this code from every table without coding all the specific events? Can I put some code in a TABLE GENERIC Page_DataRendering call?

// Page Data Rendering event
public void Page_DataRendering(ref string header) {
    // Example:
    //header = "your header";

//this is my full code
string jsonStr = Profile.GetValue("myJsonConfig");
JObject jsonObj = JObject.Parse(jsonStr);
var items = jsonObj["tables"][CurrentTable.Name]["fieldstohide"].ToString().Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
foreach (string item in items) {
    var myfield = FieldByName(item);
    myfield.Visible = false;
}      

//I would like to simply call the global function SETFIELDSVISIBLE()

}

Re: Hide some fields from LIST,ADD,VIEW and EDIT pages from a comma separated string

Posted: Mon Nov 14, 2022 6:33 am
by MichaelG

I tried to put the code in a GLOBAL FUNCTION, but it gives me the error that FieldByName is not available there.

You should pass the table as the parameter in the Global function, or just use CurrentTable (e.g. CurrentTable.FieldByName(name))