i want to customize sakai site tool configuration like this format using layouts. but by default in sakai layout is possible by double layout or single layout. actually i need combination of both first row is double column layout and remaining single column layouts.
please tell me is there any option to do like this.or provide me guidance how to customize .here i am inserted image i want tool alignment like this
Thanks In Advance
http://i.stack.imgur.com/Xjy17.png
finally its working i did this. Please provide any suggestions if required for more flexibility or any other way to do this.
Kernel
Sitepage.java :-
Add
public static final int LAYOUT_MY_CUSTOM = 2;
BaseSitepage.java:-
Change this in
protected BaseSitePage(BaseSiteService siteService, Site site, String id, String title, String layout,
boolean popup)
{}
if (layout.equals(String.valueOf(LAYOUT_SINGLE_COL)))
{
m_layout = LAYOUT_SINGLE_COL;
}
else if (layout.equals(String.valueOf(LAYOUT_DOUBLE_COL)))
{
m_layout = LAYOUT_DOUBLE_COL;
}
else if(layout.equals(String.valueOf(LAYOUT_MY_CUSTOM))){
m_layout = LAYOUT_MY_CUSTOM;
}
And
protected BaseSitePage(BaseSiteService siteService, Site site, String id, String title, String layout,
boolean popup)
{} method
else if(layout.equals(String.valueOf(LAYOUT_MY_CUSTOM))){
m_layout = LAYOUT_MY_CUSTOM;
}
And
public void setLayout(int layout)
{
if ((layout == LAYOUT_SINGLE_COL) || (layout == LAYOUT_DOUBLE_COL) || (layout == LAYOUT_MY_CUSTOM))
{
m_layout = layout;
}
else
M_log.warn("setLayout(): set to invalid value: " + layout);
}
BaseSiteService.java:-
public String[] getLayoutNames()
{
String[] rv = new String[3];
rv[0] = rb.getString("sitpag.lay_sngl");
rv[1] = rb.getString("sitpag.lay_dbl");
rv[2] = rb.getString("sitpag.lay_cust1");
return rv; }
Portal:-
Comment following code in the method
public void includePage(PortalRenderContext rcontext, HttpServletResponse res,
HttpServletRequest req, Session session, SitePage page,
String toolContextPath, String wrapperClass) throws IOException
{
rcontext.put("pageColumnLayout", (page.getLayout()==SitePage.LAYOUT_DOUBLE_COL) ? "col1of2": "col1");
and replace the code
if(page.getLayout() == SitePage.LAYOUT_DOUBLE_COL){
rcontext.put("pageColumnLayout","col1of2");
}
else if(page.getLayout() == SitePage.LAYOUT_MY_CUSTOM){
rcontext.put("pageColumnLayout","SingleDouble");
}
else{
rcontext.put("pageColumnLayout","col1");
}
And change
rcontext.put("pageTwoColumn", Boolean
.valueOf(page.getLayout() == SitePage.LAYOUT_DOUBLE_COL || page.getLayout() == SitePage.LAYOUT_MY_CUSTOM));
And
// do the second column if needed
if (page.getLayout() == SitePage.LAYOUT_DOUBLE_COL ||page.getLayout() == SitePage.LAYOUT_MY_CUSTOM)
Site Manage:-
SiteAction.java
Add custom layout to following method 8990 line
private void addSynopticTool(SitePage page, String toolId,
String toolTitle, String layoutHint) {
if (page.getContainingSite() != null) {
if (page.getLayout() != SitePage.LAYOUT_SINGLE_COL || page.getLayout() != SitePage.LAYOUT_MY_CUSTOM|| !page.getContainingSite().isCustomPageOrdered() ) {
page.setLayout(SitePage.LAYOUT_DOUBLE_COL);
}
}
}
OSP Tool
XSltcharon portal:-
XsltrenderContext.java:-
Comment the following
/*pageElement.setAttribute("layout", (context.get("pageColumnLayout")
.equals("col1")) ? "0" : "1");*/
And replace with
String selectedLayout="0";
//context.put("pageColumnLayout","SingleDouble");
if(context.get("pageColumnLayout").equals("col1of2")){
selectedLayout="1";
}
else if(context.get("pageColumnLayout").equals("SingleDouble")){
selectedLayout="2";
}
else{
selectedLayout="0";
}
And add following statements in if (selected)
if(context.get("pageColumnLayout").equals("SingleDouble")){
Element column3 = doc.createElement("column");
Element column4 = doc.createElement("column");
Element column5 = doc.createElement("column");
column3.setAttribute("index","3");
column4.setAttribute("index","4");
column5.setAttribute("index","5");
LinkedList l1=new LinkedList();
LinkedList l2=new LinkedList();
LinkedList all=new LinkedList();
List column0Tools=(List)context.get("pageColumn0Tools");
List column1Tools=(List)context.get("pageColumn1Tools");
if(column0Tools !=null){
l1.add(column0Tools.get(0));
column3.appendChild(createColumnToolsXml(doc,l1, page));
column0Tools.remove(0);
if(column0Tools !=null)
{
all.addAll(column0Tools);
}
columns.appendChild(column3);
if(column1Tools !=null && column1Tools.size()>0 )
{ l2.add(column1Tools.get(0)); column4.appendChild(createColumnToolsXml(doc,l2, page));
column1Tools.remove(0);
if(column1Tools !=null){
all.addAll(column1Tools);
}
}
columns.appendChild(column4);
if(all !=null){
column5.appendChild(createColumnToolsXml(doc,all, page));
}
columns.appendChild(column5);
}
Osp-Portal
osp-portal.xsl:-
Add the following into portal.xslt and osp-portal.xsl
<!--
===============match single and Double============
process a selected page with two column layouts
param:content - "true" or "false" if rendering tool content or tool list
=========================================================
-->
<xsl:template match="page[#layout='2' and #selected='true']">
<xsl:param name="content"/>
<xsl:if test="$content='true'">
<xsl:call-template name="page-content-custom">
<xsl:with-param name="page" select="."/>
</xsl:call-template>
</xsl:if>
<xsl:if test="$content='false'">
<li>
<a accesskey="1" class="selected" href="#">
<xsl:attribute name="accesskey">
<xsl:value-of select="../../#order"/>
</xsl:attribute>
<xsl:value-of select="title"/>
</a>
</li>
</xsl:if>
</xsl:template>
<!--Custom Sarath -->
<xsl:template name="page-content-custom">
<xsl:param name="page" />
<h1 class="skip">
<xsl:value-of select="$externalized/entry[#key='sit.contentshead']" />
</h1>
<a id="tocontent" class="skip" name="tocontent"></a>
<div id="content">
<div id="col1of2">
<div class="portlet">
<p class="pageId">$page</p>
<xsl:for-each select="$page/columns/column[#index='3']/tools/tool">
<xsl:call-template name="tool">
<xsl:with-param name="tool" select="." />
</xsl:call-template>
</xsl:for-each>
</div>
</div>
<div id="col2of2">
<div class="portlet">
<xsl:for-each select="$page/columns/column[#index='4']/tools/tool">
<xsl:call-template name="tool">
<xsl:with-param name="tool" select="." />
</xsl:call-template>
</xsl:for-each>
</div>
</div>
<div id="col1">
<div class="portlet">
<xsl:for-each select="$page/columns/column[#index='5']/tools/tool">
<xsl:call-template name="tool">
<xsl:with-param name="tool" select="." />
</xsl:call-template>
</xsl:for-each>
</div>
</div>
</div>
</xsl:template>
in Portal.css file
#col1_new{
padding-right: .0em;
clear:both;
}
#col1of2_new{
width: 51%;
*/width: 48%;
float: left;
margin: 0;
margin-left: 0.3em;
}
#col2of2_new{
width: 48%;
*/width: 48%;
float: right;
}
Related
I am developing an application using C# in Blazor Framework. I have designed some forms like the following, where the grey areas are populated with the button below which triggers a pop up window for selection. Then after selection is done the selected item description will populated into the gray area. This grey area is an InputText element.
If the grey InputTexts are marked as required & readonly, then the areas are grey and users cannot insert manually their values but only though selection window. This is good, but if the user did not populate the window for selection it can also submit the form.
If the grey InputTexts are marked as required and beeing readonly though css, then the validation works, so the user should populate the window selection first, but if he did not, then the grey area becomes editable for manual input.
Any ideas how I can protect the application from manual input but at the same time make the validation work?
Any ideas how I can protect the application from manual input but at the same time make the validation work?
If I'm reading the question correctly, the demo below shows how to link the selector (in this case a select control) and the display and show the correct validation information and UX without access to the readonly control.
As you show no code, I don't know whether this fits with your model and form.
#page "/"
#using System.ComponentModel.DataAnnotations;
<PageTitle>Index</PageTitle>
<h1>Demo</h1>
<EditForm Model="model" OnValidSubmit=#OnSubmit class="border border-dark p-3 m-2">
<DataAnnotationsValidator />
<div class="mb-2">
<label class="form-label">Country</label>
<InputText class="form-control" disabled #bind-Value="#model.Value" />
<ValidationMessage For="() => model.Value" />
</div>
#if (!show)
{
<div class="mb-2">
<button type="button" class="btn btn-dark" #onclick=OnShow>Select Country</button>
</div>
}
else
{
<div class="mb-2">
<InputSelect class="form-select" #bind-Value:get=#model.Value #bind-Value:set="this.OnSetCountry">
<option value="">-- Select a Country -- </option>
<option value="UK">UK</option>
<option value="France">France</option>
<option value="Portugal">Portugal</option>
</InputSelect>
</div>
}
<div class="col=12 mt-2 text-end">
<button class="btn btn-success" type="submit">Submit</button>
</div>
</EditForm>
<h3 class="mt-4">Hides the -- Select a Country -- once a value is selected</h3>
<EditForm Model="model2" OnValidSubmit=#OnSubmit class="border border-dark p-3 m-2">
<DataAnnotationsValidator />
<div class="mb-2">
<label class="form-label">Country</label>
<InputText class="form-control" disabled #bind-Value="#model2.Value" />
<ValidationMessage For="() => model2.Value" />
</div>
#if (!show2)
{
<div class="mb-2">
<button type="button" class="btn btn-dark" #onclick=OnShow2>Select Country</button>
</div>
}
else
{
<div class="mb-2">
<InputSelect class="form-select" #bind-Value:get=#model2.Value #bind-Value:set="this.OnSetCountry2">
#if (model2.Value is null)
{
<option selected disabled value="">-- Select a Country -- </option>
}
<option value="UK">UK</option>
<option value="France">France</option>
<option value="Portugal">Portugal</option>
</InputSelect>
</div>
}
<div class="col=12 mt-2 text-end">
<button class="btn btn-success" type="submit">Submit</button>
</div>
</EditForm>
#code {
private Model model = new();
private bool show = false;
private Model model2 = new();
private bool show2 = false;
private void OnSetCountry(string? value)
{
model.Value = null;
if (value is not null || value != string.Empty)
model.Value = value;
show = false;
}
private void OnSetCountry2(string? value)
{
model2.Value = null;
if (value is not null || value != string.Empty)
model2.Value = value;
show2 = false;
}
private void OnShow()
=> show = !show;
private void OnShow2()
=> show2 = !show2;
public void OnSubmit()
{ }
public class Model
{
[Required]
public string? Value { get; set; }
}
}
recently i am playing around with blazor-pizza shops tutorial.
I think of something to fix here.
Current:
American bacon is my first selection, after i selected it, the dropdownlist is selecting Artichoke hearts
Outcome I want:
Dropdownlist to reset default selecting "(select)" after i select the American bacon
My code like this:
#inject HttpClient HttpClient
#code {
List<Topping> toppings;
[Parameter] public Pizza Pizza { get; set; }
[Parameter] public EventCallback OnCancel { get; set; }
[Parameter] public EventCallback OnConfirm { get; set; }
protected async override Task OnInitializedAsync()
{
toppings = await HttpClient.GetFromJsonAsync<List<Topping>>("toppings");
}
void ToppingSelected(ChangeEventArgs e)
{
if (int.TryParse((string)e.Value, out var index) && index >= 0)
{
AddTopping(toppings[index]);
toppings.Remove(toppings[index]);
}
}
void AddTopping(Topping topping)
{
if (Pizza.Toppings.Find(pt => pt.Topping == topping) == null)
{
Pizza.Toppings.Add(new PizzaTopping() { Topping = topping });
}
}
void RemoveTopping(Topping topping)
{
Pizza.Toppings.RemoveAll(pt => pt.Topping == topping);
toppings.Add(topping);
toppings = toppings.OrderBy(p=>p.Name).ToList();
}
}
<div class="dialog-container">
<div class="dialog">
<div class="dialog-title">
<h2>#Pizza.Special.Name</h2>
#Pizza.Special.Description
</div>
<form class="dialog-body">
<div>
<label>Size:</label>
<input type="range" min="#Pizza.MinimumSize" max="#Pizza.MaximumSize" step="1" #bind="Pizza.Size" #bind:event="oninput" />
<span class="size-label">
#(Pizza.Size)" (£#(Pizza.GetFormattedTotalPrice()))
</span>
</div>
<div>
<label>Extra Toppings:</label>
#if (toppings == null)
{
<select class="custom-select" disabled>
<option>(loading...)</option>
</select>
}
else if (Pizza.Toppings.Count >= 6)
{
<div>(maximum reached)</div>
}
else
{
<select id="ToppingSelection" class="custom-select" #onchange="ToppingSelected">
<option value="-1" disabled selected>(select)</option>
#for (var i = 0; i < toppings.Count; i++)
{
<option value="#i">#toppings[i].Name - (£#(toppings[i].GetFormattedPrice()))</option>
}
</select>
}
</div>
<div class="toppings">
#foreach (var topping in Pizza.Toppings)
{
<div class="topping">
#topping.Topping.Name
<span class="topping-price">#topping.Topping.GetFormattedPrice()</span>
<button type="button" class="delete-topping" #onclick="#(() => RemoveTopping(topping.Topping))">x</button>
</div>
}
</div>
</form>
<div class="dialog-buttons">
<button class="btn btn-secondary mr-auto" #onclick="OnCancel">Cancel</button>
<span class="mr-center">
Price: <span class="price">#(Pizza.GetFormattedTotalPrice())</span>
</span>
<button class="btn btn-success ml-auto" #onclick="OnConfirm">Order ></button>
</div>
</div>
I would recommend rewriting the form to use the built-in Blazor components like InputSelect with proper two-way binding etc.
However, a "quick" fix could be something like a static binding. First, create a field that always has a value of -1 which is your code for (select)
#code {
private readonly Int32 _toppingsIndex = -1;
}
In the next step, set the value to the select control.
<select id="ToppingSelection" class="custom-select"
#onchange="ToppingSelected"
value="#_toppingsIndex">
<option value="-1" disabled>(select)</option>
#for (var i = 0; i < toppings.Count; i++)
{
<option value="#i">#toppings[i].Name - (£#(toppings[i].GetFormattedPrice()))</option>
}
</select>
Explanation
As soon as you select something, your method ToppingSelected is executed, and the topping is removed from the list. As soon as the method has finished, a new render cycle is started. The value is still -1, so the option with the value -1 will be selected after the rendering is finished.
I have an image I would like to display only after a user has filled in all text fields.
I have tried using disabled attribute, but that does not seem to work. Any other insights?
Here is my current code:
<EditForm EditContext="#EditContext" style="max-width:800px;" onkeydown="javascript: DisableEnterKey();">
<FluentValidator />
<img src="images/approval-16-grey.ico" alt="Image" disabled="#OkayDisabled">
<p class="statp">How many families and/or individuals are living on your land?</p><br />
<label class="statlabel" for="amountOfFamilies">Amount of families:</label><br />
<InputNumber id="fams" for="indivNum" class="input" #bind-Value="#familyData.IndividualAmount" onwheel="this.blur()" placeholder="Families..." autofocus />
<ValidationMessage For="() => familyData.IndividualAmount" />
<br /><hr class="statHR" />
<label class="statlabel" for="amountOfIndividuals">Amount of single individuals: </label><br />
<InputNumber id="individuals" for="famNum" class="input" #bind-Value="#familyData.FamilyAmount" onwheel="this.blur()" placeholder="Individuals..."/>
<ValidationMessage For="() => familyData.FamilyAmount" />
<br /><hr class="statHR" />
<label class="statlabel" for="names"> Please enter all of the names here:</label><br />
<InputTextArea id="names" class="textArea" rows="4" cols="18" #bind-Value="#PersonName" placeholder="Names of all individuals..." />
<ValidationMessage For="() => familyData.PersonName" />
</EditForm>
</div>
</ul>
#code
{
private EditContext? EditContext;
public FamilyData Model = new FamilyData();
protected string OkayDisabled { get; set; } = "disabled";
protected override void OnInitialized()
{
EditContext = new EditContext(Model);
EditContext.OnFieldChanged += EditContext_OnFieldChanged;
base.OnInitialized();
}
protected override void OnAfterRender(bool firstRender)
{
base.OnAfterRender(firstRender);
SetOkDisabledStatus();
}
private void EditContext_OnFieldChanged(object? sender, FieldChangedEventArgs e)
{
SetOkDisabledStatus();
}
private void SetOkDisabledStatus()
{
if(EditContext.Validate())
{
OkayDisabled = null;
}
else
{
OkayDisabled = "disabled";
}
}
}
The hidden html attribute also works to hide an element.
<p hidden>This paragraph should be hidden.</p>
To bind to Model:
<p hidden="#HideLabel">I am Hidden When HideLabel == true</p>
<p hidden="#(!HideLabel)">I am Hidden when Hidelabel == false</p>
<button #onclick="#Toggle">Show/Hide</button>
#code {
private bool HideLabel {get;set;} = false;
private void Toggle()
{
HideLabel = !HideLabel;
}
}
Edit: You can also use a CSS class to hide/show an element:
<div class="font-italic #(HideLabel ? "d-none" : "d-show")">
I am Hidden When HideLabel == true
</div>
Change OkayDisabled to a bool, and then around your image do this
#if (!OkayDisabled)
{
<img src=".....whatever" etc />
}
You might also want to add #bind:event="oninput" wherever you use an #bind.
Instead of binding your flag to the disabled attribute (an image's disabled attribute just grays it out), I would bind it to a css class that has display: none;
.hidden {
display: none;
}
<img class="#(ShouldShowImage? "hidden" : string.Empty)">
didn't used it within editform but should work
#if(OkayDisabled)
{
<img src="images/approval-16-grey.ico" >
We are developing a .net core 3.1 MVC application (actual with MVVMC).
We have implemented custom validation attributes. And want them to be checked on server and on client side.
The view is a standard view, however the user has the possibility to add multiple partial views to the standard view (via a button).
In the partial view we were not able to use the 'asp-for' tag helper within the input fields, because one can have several times the same item added, and we need to be able to create a list in the ViewModel out of those additional partial views selected. That's why we tried to build the tags by ourselfes.
Once a partial view has been requested via ajax we are calling the validator again. Server validation works, client validation only works for those inputs, which are on the main view, not for the inputs on the partial views. However, after refreshing or when the server sends the user back after a validation error, client validation starts working (because site is completely refreshed and jquery validation takes input fields of partial views into account).
Please find the code below:
Implementation of validation attribute
public class AllowedExtensionsAttribute: ValidationAttribute, IClientModelValidator
{
private readonly List<string> _extensions = new List<string>();
public AllowedExtensionsAttribute(string extensions)
{
_extensions.Add(extensions);
}
protected override ValidationResult IsValid(object value, ValidationContext context)
{
if (value is IFormFile file)
{
var extension = Path.GetExtension(file.FileName);
if (!_extensions.Contains(extension.ToLower()))
{
return new ValidationResult(ErrorMessage);
}
}
return ValidationResult.Success;
}
public void AddValidation(ClientModelValidationContext context)
{
context.Attributes.Add("data-val", "true");
context.Attributes.Add("data-val-extension", ErrorMessage);
}
}
MainViewModel
[AllowedExtensions(".pdf", ErrorMessage = "This data type is not allowed!")]
public IFormFile PdfDocumentOne { get; set; }
PartialViewModel
[AllowedExtensions(".pdf", ErrorMessage = "This data type is not allowed!")]
public IFormFile PdfDocumentTwo { get; set; }
Main View
<form method="post" asp-controller="Home" asp-action="MainView" enctype="multipart/form-data">
<div class="form-group top-buffer">
<div class="row">
<div class="col-2">
<label asp-for="PdfDocumentOne" class="control-label"></label>
</div>
<div class="col-3">
<input asp-for="PdfDocumentOne" class="form-control-file" accept="application/pdf" />
<span asp-validation-for="PdfDocumentOne" class="text-danger"></span>
</div>
</div>
</div>
<div id="container">
<div id="containerFull" class="form-group">
<div class="row">
<div class="col-2">
<label class="control-label">...</label>
</div>
<div class="col-10">
<div id="containerPartialView">
</div>
</div>
</div>
<div class="row">
<div class="col-2">
</div>
<div class="col-3">
<button id="AddPartialView" type="button" class="form-control">...</button>
</div>
</div>
</div>
</div>
...
<div class="form-group top-buffer">
<div class="row">
<div class="col-2">
<input type="submit" value="Submit" class="form-control" id="checkBtn" />
</div>
</div>
</div>
</form>
Partial View
<input id="Lists[#Model.ViewId].ViewId" name="Lists[#Model.ViewId].ViewId" class="partialViewModel" type="hidden" value="#Model.ViewId" />
<div id="Lists[#Model.ViewId].MainContainer" class="partialView">
<div class="form-group">
<div>
<div class="col-2">
<label asp-for="PdfDocumentTwo" class="control-label"></label>
</div>
<div class="col-3">
<input name="Lists[#Model.ViewId].PdfDocumentTwo" id="Lists[#Model.ViewId].PdfDocumentTwo " type="file" class="form-control-file" accept="application/pdf"
data-val="true" data-val-extension="This data type is not allowed!"/>
<span class="text-danger field-validation-valid" data-valmsg-for="Lists[#Model.ViewId].PdfDocumentTwo" data-valmsg-replace="true"></span>
</div>
</div>
</div>
...
</div>
Javascript
function AddPartialView() {
var i = $(".partialView").length;
$.ajax({
url: '/Home/AddPartialView?index=' + i,
success: function (data) {
$('#containerPartialView').append(data);
$().rules('remove','extension');
jQuery.validator.unobtrusive.adapters.addBool("extension");
},
error: function (a, b, c) {
console.log(a, b, c);
}
});
}
$('#AddPartialView').click(function () {
AddPartialView();
});
jQuery.validator.addMethod("extension",
function (value, element, param) {
var extension = value.split('.').pop().toLowerCase();
if ($.inArray(extension, ['pdf']) == -1) {
return false;
}
return true;
});
jQuery.validator.unobtrusive.adapters.addBool("extension");
HomeController
[HttpPost]
public IActionResult MainView(MainViewModel vm)
{
if (vm == null)
{
return RedirectToAction("Index");
}
DatabaseHelper.GetMainViewModel(vm);
if (!ModelState.IsValid)
{
#ViewData["StatusMessageNegative"] = "The entered data is not valid. Please scroll down to correct your data.";
return View(vm);
}
return RedirectToAction("UploadDocument", new { Id = vm.Id});
}
[HttpGet]
public ActionResult AddPartialView(int index)
{
PartialViewModel pvm = DatabaseHelper.GetPartialViewModel(index);
return PartialView("Partial", pvm);
}
After searching in the internet we found the following argument (data-ajax="true"). However, we don't know where to place it or how to use it correctly.
Have you tried re-apply validation after loading your partial view?
$.ajax({
url: '/Home/AddPartialView?index=' + i,
success: function (data) {
$('#containerPartialView').append(data);
$().rules('remove','extension');
jQuery.validator.unobtrusive.adapters.addBool("extension");
},
error: function (a, b, c) {
console.log(a, b, c);
}
}).then(function () {
$("form").each(function () { $.data($(this)[0], 'validator', false); });
$.validator.unobtrusive.parse("form");
});
I am currently working on a grails project and am trying to create a one to many form. I have been using the tutorial at the link below to get started:
http://omarello.com/2010/08/grails-one-to-many-dynamic-forms/
The solution has a form with two field of data that are static and one dynamic fiwld where the user is allowed to add as many variables as they want and then save them. below you can see all the various files for this functionality:
Products.Groovy Domain Class
import org.apache.commons.collections.list.LazyList;
import org.apache.commons.collections.FactoryUtils;
/**
* Products
* A domain class describes the data object and it's mapping to the database
*/
class Products {
String productType
String product
List vars = new ArrayList()
//This represents a product belonging to a single department
static belongsTo = [dept:Depts]
static hasMany = [ vars:Dynam ]
static mapping = {
vars cascade:"all-delete-orphan"
}
def getVarsList() {
return LazyList.decorate(
vars,
FactoryUtils.instantiateFactory(Dynam.class))
}
static constraints = {
productType blank: false
product blank:false, size: 1..160
}
}
Dynam.groovy
class Dynam {
public enum VarType{
T("Testimonial"),
D("Dimentions"),
N("Networking")
final String value;
VarType(String value) {
this.value = value;
}
String toString(){
value;
}
String getKey(){
name()
}
static list() {
[T, D, N]
}
}
static constraints = {
index(blank:false, min:0)
data(blank:false)
type(blank:false, inList:VarType.list(), minSize:1, maxSize:1)
}
int index
String data
VarType type
boolean deleted
static transients = [ 'deleted' ]
static belongsTo = [ product:Products ]
def String toString() {
return "($index) ${data} - ${type.value}"
}
}
ProductsController.Groovy
Creation Function
def createDynProduct(){
def productsInstance = new Products()
productsInstance.properties = params
//This is used in order to create a new User Object for the current User logged in
def userObjects = springSecurityService.currentUser
//This passes the 2 models to the view one being Products and the other a User Department
[productsInstance: productsInstance, dept: userObjects.dept]
}
Save Function
def save() {
def productInfo = "dynam"
def userObjects = springSecurityService.currentUser
def dept = Depts.findByName(params.dept.name)
def product = new Products(product:productInfo, productType:params.productType, dept: dept, vars:params.varsList)
def _toBeRemoved = product.vars.findAll {!it}
// if there are vars to be removed
if (_toBeRemoved) {
product.vars.removeAll(_toBeRemoved)
}
//update my indexes
product.vars.eachWithIndex(){phn, i ->
if(phn)
phn.index = i
}
//If the the save is not successful do this
if (!product.save(flush: true)) {
render(view: "create", model: [productsInstance: product, dept: userObjects.dept])
return
}
redirect(action: "show", id: product.id)
}
createDynProduct.gsp
<%# page import="com.tool.Products" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name='layout' content='springSecurityUI'/>
<g:set var="entityName" value="${message(code: 'messages.label', default: 'Products')}" />
<title><g:message code="default.create.label" args="[entityName]" /></title>
</head>
<body>
<g:renderErrors bean="${productsInstance}" />
<g:form action='save' name='ProductForm' >
<br/>
<!-- Render the product template (_dynam.gsp) here -->
<g:render template="dynam" model="['productsInstance':productsInstance]"/>
<!-- Render the product template (_dynam.gsp) here -->
<div class="buttons">
<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
</div>
</g:form>
<!-- Render the var template (_var.gsp) hidden so we can clone it -->
<g:render template='var' model="['var':null,'i':'_clone','hidden':true]"/>
<!-- Render the var template (_var.gsp) hidden so we can clone it -->
</body>
</html>
_dynam.gsp
<div class="dialog">
<table>
<tbody>
<tr class="prop">
<td valign="top" class="name">
<label for="productType">Product Type</label>
</td>
<td>
<g:textField name='productType' bean="${productsInstance}" value="${productsInstance.productType}"
size='40'/>
</td>
</tr>
<tr class="prop">
<td valign="top" class="name">
<label for="Vars">Vars</label>
</td>
<!-- Render the vars template (_vars.gsp) here -->
<g:render template="vars" model="['productsInstance':productsInstance]" />
<!-- Render the vars template (_vars.gsp) here -->
</tr>
<tr class="prop">
<td valign="top" class="name">
<label for="Department">Department</label>
</td>
<td>
<g:textField name='dept.name' readonly="yes" value="${dept.name}" size='40'/>
</td>
</tr>
</tbody>
</table>
</div>
_vars.gsp
<script type="text/javascript">
var childCount = ${productsInstance?.vars.size()} + 0;
function addVar(){
var clone = $("#var_clone").clone()
var htmlId = 'varsList['+childCount+'].';
var varInput = clone.find("input[id$=number]");
clone.find("input[id$=id]")
.attr('id',htmlId + 'id')
.attr('name',htmlId + 'id');
clone.find("input[id$=deleted]")
.attr('id',htmlId + 'deleted')
.attr('name',htmlId + 'deleted');
clone.find("input[id$=new]")
.attr('id',htmlId + 'new')
.attr('name',htmlId + 'new')
.attr('value', 'true');
varInput.attr('id',htmlId + 'data')
.attr('name',htmlId + 'data');
clone.find("select[id$=type]")
.attr('id',htmlId + 'type')
.attr('name',htmlId + 'type');
clone.attr('id', 'var'+childCount);
$("#childList").append(clone);
clone.show();
varInput.focus();
childCount++;
}
//bind click event on delete buttons using jquery live
$('.del-var').live('click', function() {
//find the parent div
var prnt = $(this).parents(".var-div");
//find the deleted hidden input
var delInput = prnt.find("input[id$=deleted]");
//check if this is still not persisted
var newValue = prnt.find("input[id$=new]").attr('value');
//if it is new then i can safely remove from dom
if(newValue == 'true'){
prnt.remove();
}else{
//set the deletedFlag to true
delInput.attr('value','true');
//hide the div
prnt.hide();
}
});
</script>
<div id="childList">
<g:each var="var" in="${productsInstance.vars}" status="i">
<!-- Render the var template (_var.gsp) here -->
<g:render template='var' model="['var':var,'i':i,'hidden':false]"/>
<!-- Render the var template (_var.gsp) here -->
</g:each>
</div>
<input type="button" value="Add Var" onclick="addVar();" />
_var.gsp
<div id="var${i}" class="var-div" <g:if test="${hidden}">style="display:none;"</g:if>>
<g:hiddenField name='varsList[${i}].id' value='${var?.id}'/>
<g:hiddenField name='varsList[${i}].deleted' value='false'/>
<g:hiddenField name='varsList[${i}].new' value="${var?.id == null?'true':'false'}"/>
<g:textField name='varsList[${i}].number' value='${var?.data}' />
<g:select name="varsList[${i}].type"
from="${com.tool.Dynam.VarType.values()}"
optionKey="key"
optionValue="value"
value = "${var?.type?.key}"/>
<span class="del-var">
<img src="${resource(dir:'images/skin', file:'icon_delete.png')}"
style="vertical-align:middle;"/>
</span>
</div>
The solution works in part so you can go to the products page and it loads with the fields and the dynamic fields are added to the view fine and I can enter the data. However when I click save the data for the static fields is persisted but the dynamic vars are not saved to the domain.
I don’t get any errors but I believe it has something to do with the save function line below and the vars:params.varsList in particular.
def product = new Products(product:productInfo, productType:params.productType, dept: dept, vars:params.varsList)
I have checked the varsList data using println and it returns null, can someone please help with this?
Thanks in advance