Model Functions¶
In this section, we will cover the following functions in-depth listed below:
uadmin.HideInDashboarder¶
type HideInDashboarder interface {
HideInDashboard() bool
}
HideInDashboarder is used to check if a model should be hidden in the dashboard.
Suppose I have five models in my dashboard: Todos, Categorys, Items, Friends, and Expressions. I want Friends and Expressions models to be hidden in the dashboard. In order to do that, go to the friend.go and expression.go inside the models folder and apply the HideInDashboard() function. Set the return value to true inside it.
friend.go
func (Friend) HideInDashboard() bool {
return true
}
expression.go
func (Expression) HideInDashboard() bool {
return true
}
Now go to the main.go and apply the following codes below inside the main function:
// Initialize the Expression and Friend models inside the modelList with
// the array type of interface
modelList := []interface{}{
models.Expression{},
models.Friend{},
}
// Loop the execution process based on the modelList count
for i := range modelList {
// Returns the reflection type that represents the dynamic type of i
t := reflect.TypeOf(modelList[i])
// Calls the HideInDashboarder function to access the HideInDashboard
// function
hideItem := modelList[i].(uadmin.HideInDashboarder).HideInDashboard()
// Initializes the dashboardmenu variable to assign the DashboardMenu
dashboardmenu := uadmin.DashboardMenu{
// Returns the name of the model based on reflection
MenuName: t.Name(),
// Returns the boolean value based on the assigned return in the
// HideInDashboard()
Hidden: hideItem,
}
// Prints the information of the dashboardmenu
uadmin.Trail(uadmin.INFO, "MenuName: %s, Hidden: %t", dashboardmenu.MenuName, dashboardmenu.Hidden)
}
Go back to your application. Open the DashboardMenu then delete the Expressions and Friends model.
Now rerun your application and see what happens.
[ INFO ] MenuName: Expression, Hidden: true
[ INFO ] MenuName: Friend, Hidden: true
As expected, Friends and Expressions models are now hidden in the dashboard. If you go to the Dashboard Menus, you will see that they are checked in the Hidden field.
Quiz:
uadmin.Model¶
type Model struct {
ID uint `gorm:"primary_key"`
DeletedAt *time.Time `sql:"index"`
}
Model is the standard struct to be embedded in any other struct to make it a model for uadmin.
In every struct, uadmin.Model must always come first before creating a field.
type (struct_name) struct{
uadmin.Model // <-- place it here
// Some codes here
}
Quiz:
uadmin.NewModel¶
func NewModel(modelName string, pointer bool) (reflect.Value, bool)
NewModel creates a new model from a model name.
Parameters:
modelName string: Is the model you want to call in the function
pointer bool Points to the interface
Before we proceed to the example, read Tutorial Part 9 - Introduction to API to familiarize how API works in uAdmin.
Suppose I have four records in my Category model.
- Education ID = 4
- Family ID = 3
- Work ID = 2
- Travel ID = 1
Create a file named custom_todo.go inside the api folder with the following codes below:
// CustomTodoHandler !
func CustomTodoHandler(w http.ResponseWriter, r *http.Request) {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/custom_todo")
r.URL.Path = strings.TrimSuffix(r.URL.Path, "/")
res := map[string]interface{}{}
// Call the category model and set the pointer to true
m, _ := uadmin.NewModel("category", true)
// Get the third record in the category model
uadmin.Get(m.Interface(), "id = ?", 3)
// Assign the m.Interface() to the newmodel
newmodel := m.Interface()
// Print the result in JSON format
res["status"] = "ok"
res["category"] = newmodel
uadmin.ReturnJSON(w, r, res)
}
Establish a connection in the main.go to the API by using http.HandleFunc. It should be placed after the uadmin.Register and before the StartServer.
func main() {
// Some codes
// CustomTodoHandler
http.HandleFunc("/custom_todo/", uadmin.Handler(api.CustomTodoHandler)) // <-- place it here
}
api is the folder name while CustomTodoHandler is the name of the function inside custom_todo.go.
Run your application and see what happens.
Quiz:
uadmin.NewModelArray¶
func NewModelArray(modelName string, pointer bool) (reflect.Value, bool)
NewModelArray creates a new model array from a model name.
Parameters:
modelName string: Is the model you want to call in the function
pointer bool Points to the interface
Before we proceed to the example, read Tutorial Part 9 - Introduction to API to familiarize how API works in uAdmin.
Suppose I have four records in my Category model.
Create a file named custom_todo.go inside the api folder with the following codes below:
// CustomTodoHandler !
func CustomTodoHandler(w http.ResponseWriter, r *http.Request) {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/custom_todo")
r.URL.Path = strings.TrimSuffix(r.URL.Path, "/")
res := map[string]interface{}{}
// Call the category model and set the pointer to true
m, _ := uadmin.NewModelArray("category", true)
// Fetch the records of the category model
uadmin.Filter(m.Interface(), "id >= ?", 1)
// Assign the m.Interface() to the newmodelarray
newmodelarray := m.Interface()
// Print the result in JSON format
res["status"] = "ok"
res["category"] = newmodelarray
uadmin.ReturnJSON(w, r, res)
}
Establish a connection in the main.go to the API by using http.HandleFunc. It should be placed after the uadmin.Register and before the StartServer.
func main() {
// Some codes
// CustomTodoHandler
http.HandleFunc("/custom_todo/", uadmin.Handler(api.CustomTodoHandler)) // <-- place it here
}
api is the folder name while CustomTodoHandler is the name of the function inside custom_todo.go.
Run your application and see what happens.
Quiz: