select2 How to search by value? - jquery-select2

How to search by value? I tried many methods but it didn't work.
<option value="276" data-select2-id="475">Beaconsfield Village House</option>
or Also i changed templateSelection and templateResult to this template
<li class="select2-selection__choice" title="Beaconsfield Village House" data-select2-id="478">Beaconsfield Village House<span class="cat-id"> ID 276</span></li>
But still when i type 276 it says no results found.
escapeMarkup:function(e){return e},templateResult:function(e){return e.text + '<span class="cat-id"> ID ' + e.id + '</span>'},templateSelection:function(e){return e.text + '<span class="cat-id"> ID ' + e.id + '</span>'},theme:"default",width:"300"
How to solve this problem? I want to search with value. Thanks in advance.
Select2 v4.0.13

Related

thymeleaf evaluate string as variable

suppose I have a thymeleaf fragment named "reference" that takes a parameter referenceNumber="1" and in my model I have "reference1_firstName" = "Bob"
<div ... th:fragment="reference(referenceNumber)">
Reference <div th:text="${referenceNumber}"/> first name is <div th:text="${'reference' + referenceNumber + '_firstName'}".>
</div>
in the obviously incorrect example above I would like to print out "Reference 1 first name is Bob". It seems so simple, I can do it in several other languages, but so far my search has come up empty for thymeleaf
Couple ways to do this.
Preprocessing:
<span th:text="${reference__${referenceNumber}___firstName}" />
#ctx basic object:
<span th:text="${#ctx.getVariable('reference' + referenceNumber + '_firstName')}" />
<span th:text="${#ctx['reference' + referenceNumber + '_firstName']}" />
Or if you intend to access variables this way, use a Map instead of variables.

Thymleaf I18N Syntax

I have this code;
<div class="row">
<h5 th:text="#{heading.st}"/>
<h6 th:text="${'Sub Type: ' + results[0].subType + '; Internal Switch Role: ' + results[0].internalSwitch}"></h6>
</div>
where #{heading.st} is the internationalized string for 'Sub Type' stored in a properties file. So I can see than i18n is correctly set up. However, I cannot workout the syntax for replacing the string Sub Type in the h6 element. Both
<h6 th:text="${#heading.st + ': ' + results[0].subType + '; Internal Switch Role: ' + results[0].internalSwitch}"></h6>
and
give syntax errors. Can someone point me in the right direction please.
There are more options as well. Personally, my favorite is either using literal substitutions:
<h6 th:text="|#{heading.st}: ${results[0].subType}|" />
or using additional tags like this:
<h6>
<span th:text="#{heading.st}" />: <span th:text="${results[0].subType}" />
</h6>
I think either option is more readable than string concatenation.
Got it!
<h6 th:text="#{heading.st} + ': ' + ${results[0].subType}"></h6>

Rails is inserting element in the wrong position on HTML-Output. Wrong Syntax?

this is probably quite simple, but I tried solving it now for a longer time.
I have a helper method picture(file, alt) and a customized image-tag for the Markdown converter RedCarpet.
The helper method creates a <picture>-tag with a given file and alt-text. The custom Redcarpet renderer uses this picture(file, alt) method to compose a div.image including the picture-tag and an additional caption. The div.caption should go after the <picture>-tag inside of the div.image. But for some reason, my RedCarpet renderer includes the div.caption into the <picture>-tag.
like:
<div class="project-body-image">
<picture>
...
<div class="caption"></div>
</picture>
</div>
Visually it works aswwell, but according to the W3C validator it should go outside.
How to get the div.caption oustide of the picture-tag?
Additionally, is this a good way of outputting HTML from a method?
application_helper.rb:
def picture(file, alt)
#html = "<picture>" +
"<!--[if IE 9]><video style='display: none;''><![endif]-->" +
"<source media='(min-width: 0px)' sizes='1280px' srcset='" + file.url + " 1280w'>" +
"<!--[if IE 9]></video><![endif]-->" +
"<img src='" + file.url + "' alt='" + alt + "'>"
"</picture>"
#html.html_safe
end
custom_redcarpet.rb:
require 'redcarpet'
class CustomRedcarpet < Redcarpet::Render::HTML
include ApplicationHelper
# Custom Image tag like ![id](filename)
def image(link, title, alt_text)
# Use alt_text for record id
# if you don't find anything return nothing: ""
if Part.exists?(link)
#part = Part.find(link)
#file = #part.file
#caption = #part.description
#html = "<div class='project-body-image'>" +
picture(#file, #caption) +
"<div class='caption'>" + #caption + "</div>" +
"</div>"
#html.html_safe
else
nil
end
end
end
You are missing + at the end of this line:
"<img src='" + file.url + "' alt='" + alt + "'>"
This thereby renders an unclosed <picture> tag. But since I think browsers automatically auto-closes incomplete tags, then that is why you still see <picture></picture> properly closed in your snippet.
"Additionally, is this a good way of outputting HTML from a method?"
Usually, I use content_tag when building up a render-able view inside a helper action. But since your rendered view has <!--[if IE 9]>, I would do it like you do (using concatenated string). The only difference I might do is to use multiple-lined string using <<-EOS like the following:
#html = <<-EOS
<picture>
<!--[if IE 9]><video style='display: none;''><![endif]-->
<source media='(min-width: 0px)' sizes='1280px' srcset='#{file.url} 1280w'>
<!--[if IE 9]></video><![endif]-->
<img src='#{file.url}' alt='#{alt}'>"
"</picture>"
#html.html_safe

Better practice for js i18n with angular

I wonder to know if there is any bad smell on my practice for i18n on Angular.
I put the I18n translating function on Angular controller (because I don't know how to put it on HTML template file)
And about the i18n scope, I use this way I18n.t("city." + city_name) to indicate that the city_name is under "city" scope. Could I write it in this way I18n.t(city_name, scope: "city") to make it more understandable.
I appreciate every comment and suggestion to enhance my current solution.
Data structure
departures_lst is a list of countries' English name e.g.,: [US, China, Japan]
Each country has many cities name. e.g. [New York, LA, Boston]
Angular controller
App.controller("departures_ctrl", function($scope, $location, $http) {
$http.get("/departures.json")
.success(function (response) {
$scope.departures_lst = response;
});
$scope.get_destinations = function(city_name) {
return $location.url("/depart_from/" + city_name);
};
$scope.i18nCountryName = function(country_name) {
return I18n.t("country." + country_name) + country_name
};
$scope.i18nCityName = function(city_name) {
return I18n.t("city." + city_name) + city_name
};
});
HTML teamplate?
<div class="panel panel-transparent" ng-repeat="departure in departures_lst">
<h5>{{i18nCountryName(departure.country)}}</h5>
<li class="col-sm-3 col-lg-3 col-xs-3" ng-repeat="city in departure.cities">
<a ng-click="get_destinations(city)">
<i class="fa fa-plane"></i>
{{i18nCityName(city)}}
</a>
</li>
</div>
You should always try to put localization in the markup (as much as possible), that way you further encapsulate layout from your logic and data. I have always used the great angular-localization plug in. You get some great filters and directives to play with that have parametrization and other features built in. For example:
You have a string where you have to insert the name of a user, you define the string in your localized file as:
"some-loc-id-tag": "insert the user name {user} here!"
And in your controller scope have the property:
$scope.model.userName
And can display the name of the user with the localized string like so in HTML:
<div data-i18n="loc.some-loc-id-tag" data-user="{{model.userName}}"></div>
Check it out:
https://github.com/doshprompt/angular-localization

Grails select integer value and string value

I have two domain classes...
First:
class Employee {
String vorname
String nachname
Department department
}
Second:
class Department {
Integer number
String description
}
...and I want to create a dropdown in Grails.
My code looks like this:
<g:select id="department" name="department.description" from="${workloadreport.Department.list()}" optionValue="bezeichnung" optionKey="id" required=""
value="${employeeInstance.department?.id}" class="many-to-one" noSelection="${['null':'Please select...']}"/>
But I want to show the number + description as one value in dropdown.
Like:
Value 1 : 30 General Services
Value 2 : 40 Product Services
Value 3 : 50 Other Services
I'm assuming you want the resulting html to look something like this for an option element ...
<option value="1">1 : 30 General Services</option>
this should produce it (there may be typo, I haven't tested it):
<g:select id="department" name="department.description"
from="${workloadreport.Department.list()}" optionKey="id" required=""
value="${employeeInstance.department?.id}" class="many-to-one"
noSelection="${['null':'Please select...']}"
optionValue="${{it.id + ' : ' + it.number + ' ' + it.description}}"
/>
** note you probably have a typo in your from property. If you just want to generate a list of options from all departments, your from should look like this
<g:select ... from="${Department.list()}" .../>

Resources