Thymeleaf dynamic list setter - html

I would like to simplify some code of mine. It's quite simple honestly. I have 1 input field, and if a certain condition is fulfilled, it should have 1 more attribute. It should have the list attribute. The Problem I'm having is, that I don't know how to dynamically set an attribute.
The code looks like this currently:
<input th:if="${fieldIndex == 0 && !#arrays.contains(dropDownFields, y)}"
th:disabled="${#arrays.contains(disabledFields, y)}"
th:field="*{ZMatrixValues[__${dataIndex}__].tableValues[__${i}__][__${y}__]}"
class="table-input" type="text" onfocus="focused(this)">
<input th:if="${fieldIndex == 0 && #arrays.contains(dropDownFields, y)}"
list="list"
th:disabled="${#arrays.contains(disabledFields, y)}"
th:field="*{ZMatrixValues[__${dataIndex}__].tableValues[__${i}__][__${y}__]}"
class="table-input" type="text" onfocus="focused(this)">
And as you can see, the 2 input fields are the exact same, except when this condition is true: #arrays.contains(dropDownFields, y), then it should also add the attribute list="list". Is there any easier way than copying so much code?
Any help would be appreciated, thank you!

Instead of using th:if you can use th:attr and place the conditional logic in that attribute using the Thymeleaf "if-then-else" operator: (if) ? (then) : (else).
Here is a simplified version of your code showing this:
<input th:attr="list=${#arrays.contains(dropDownFields, y) ? 'some_value' : null}">
If the #arrays.contains condition is true, then the list attribute will be added to the <input> element; otherwise if the condition is false, and the null is returned, then no attribute will be added.
Instead of 'some_value' you can use whatever you want - including the th:field expression from your question.
All the other attributes in your question can be included in the tag, and will be unaffected, since they are not part of the (if) ? (then) : (else) logic.

Related

ng-show not able to check null or empty string

Am trying to show the html element based on condition. Like i have to check claim.claimToDealerCurrencyExchangeRate is empty or null. If it is empty or null then, i dont want to display the element (label and span ).
But it is not working as expected. Label & span elements are visible even claim.claimToDealerCurrencyExchangeRate empty.
Please find my code below.
<div id="exchange" ng-if="hasClaimGrouping('customerInvoiceOrRepairDate')"
ng-show="claim.claimToDealerCurrencyExchangeRate == null"
class="form-group row">
<label class="col-lg-3">{{'claim.view.exchangeRateApplied'|translate}}
</label>
<span>{{claim.claimToDealerCurrencyExchangeRate}}</span>
</div>
Is there any reason why you are using both ng-if and ng-show? I think one of them should suffice. The ngIf directive removes or recreates a portion of the DOM tree based on an expression. If the expression assigned to ngIf evaluates to a false value then the element is removed from the DOM, otherwise a clone of the element is reinserted into the DOM.
Your code has ng-show="claim.claimToDealerCurrencyExchangeRate == null" which means the div will be visible when claim.claimToDealerCurrencyExchangeRate is null. As per your ask, your logic is wrong. Use ng-hide="claim.claimToDealerCurrencyExchangeRate == null" or ng-show="claim.claimToDealerCurrencyExchangeRate != null". But since you are already using ng-if, you should combine all the conditions within it, unless you absolutely want the DOM element to be toggled between visibility and non-visibility multiple times and not have it re-instantiated every time, use a combination of ng-if and ng-show.
Also, understand empty and null are two different things in JavaScript. For example, if value is a variable. Then value = "" is empty but not null. value = "null" is not null but a non-empty string. Set value = null explicitly then it is null. And in all cases value is not undefined. Check what exactly claim.claimToDealerCurrencyExchangeRate is being set and define your logic in ng-show appropriately.

using checkboxes in Laravel's Form::model

I'm having trouble with Laravel's Form::model() construct. I was able to populate it with my model data but the checkboxes aren't being checked. The values are being set to 1 or 0 (courtesy of Eloquent calling a "boolean" a "tinyint" in MySQL), but I can't see how to use the Form::model() approach and set that checked attribute if the value coming is an integer and not a boolean (hence why I think Form Model Binding for checkboxes isn't working).
I'm specifying this in my view:
{{ Form::checkbox('hasDiscount') }}
And the form builder is returning this:
<input name="hasDiscount" type="checkbox" value="1" id="hasDiscount">
I think it's not really related to data-value. The value could be 'true' and the checked attribute could be false as well.
If you want to display checkbox with checked attribute, you can try my wait (it's not the best, I admit):
Form::model($model)
Form::checkbox('name', null, $model->value)
Form::close()
The third parameter could be convert to false if the value of model is null or 0 and the checkbox is not checked. otherwise, it's checked.
If you have another idea, please share it.

Putting HTML in a hidden form field in Django

I'm having a problem with a template: I'm trying to display a form for changing a value, in which the user enters the current value in a textarea and the old value is kept inside a hidden field for auditing purposes. This value is generally some HTML, and when I render the page this HTML in the hidden field seems to get partially rendered: the value attribute of my hidden field gets closed by the first quotation marks inside the entered HTML, and the rest of the HTML spews out onto my page. I've tried using the escape decorator but that hasn't changed anything.
Firstly, a better solution might be to keep the audit value in a separate model field defined with editable=False. You can still perform checks against the value in a form's clean method:
def clean(self):
cleaned_data = super(SomeForm, self).clean()
if instance.the_audit_field == cleaned_data['the_editable_field']:
...raise a validation error?
You can also modify the value of the audit field from within the model's save method.
Secondly, assuming you must do it the way you are now, let me address the non-escaped value in your template. I assume you're using something like the following:
<textarea value="{{ form.the_audit_field.value }}"></textarea>
You should instead use the following:
<textarea>{{ form.the_audit_field.value }}</textarea>
Note, the value goes inside the textarea, instead of in the value attribute of it.
An even better way to do it is to simply allow Django to render the field for you like the following:
{{ form.the_audit_field }}

Checkbox checked value not set as expected [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ASP.NET MVC:Why does the CheckBoxFor render an additional input tag and how can I get the value using the FormCollection
I needed to create a check box and bind the model to the check box. Assume instead of the model value im just assigning false as the value. Code given bellow.
#Html.CheckBox("abcd",false)
Output:
<input id="abcd" name="abcd" type="checkbox" value="true" />
<input name="abcd" type="hidden" value="false" />
The output of the HTML being generated is shown above. I do understand why the hidden check box is put by the razor view engine. My question is if the value is false then the check box visible should be value="false" (unchecked).
Why it has put value="true" (checked). Same applies for the checkboxfor helper. What is wrong or could you explain how to implement this?
You should consider passing the value via the model:
#Html.CheckBoxFor(model => model.abcd)
If you need to set it to be 'false' then you can do this by setting the abcd property to false in the Controller before returning the View.
Checkboxes don't have different checked/unchecked values, they only have a checked value. If you understand why razor outputs the hidden field then you understand this.
Imagine for a second that it did have value='false'. What would happen when you checked the checkbox? Would you expect the value to change? (hint: you shouldn't). You'd have a checked checkbox with value='false'. What does that even mean? Then, upon posting, you would post false as your value, and that would be nonsense.
So. Checkbox value attributes don't change. If you need to write code that uses the value, don't look for its value, look for whether or not it's checked.

Missing value attribute when injecting text input using mootools

As noted by this question and in this fiddle, when injecting an input the value attribute doesn't seem to be added to the DOM, MooTools seems to handle it internally. Unfortunately this means that an input injected thus:
new Element("input", {type: "text", value: "injected value"}).inject(parent);
can't be selected like so:
parent.getElements('input[value="injected value"]').length;
(this returns 0 instead of 1 as it would if the value attribute was present)
In my project I need to insert blank input boxes but only if there isn't already a blank one;if(parent.getElements('input[value=""]').length == 0)however this is always resolving to true no matter how many blank inputs there are.
Is there a better way to achieve my goal or is there a fix for what seems like a bug?
You could try adding:
var x = new Element("input", {type: "text", value: "injected value"}).inject(parent);
x.setAttribute("value",x.value);
Caveat: While this fixes your issue with the element's value attribute not being reflected, the value attribute is typically not a good way to check if a field is empty, because it is not sync'd with the input's contents.
It would be better to use something like this:
$$('input[type=text]').filter(function(item){return (item.value=="");})