how to give an array as option values in select using select2 - jquery-select2

This is my html
<select class="compare-tag1">
</select>
this is the array i want to be there as option values in the above select class:
optionVal = ["1","2","3"];
and this is how I am trying to do it with select2
$(".compare-tag1").select2({
val: tagValuesArray
});
Im getting no-error,but no values are shown in the dropdown. How can I get the values of the array as option values using select2?

It sounds like you might be looking for the data option, which will convert a an array of data objects (or single strings) into <option> tags.
$(".compare-tag1").select2({
data: tagValuesArray
});

Related

How to create filter for checkbox in kendo Grid MVC

I need to have a filter on my kendo grid for check box.
The checkbox is a Non-Bound column. Using checkbox I select the corresponding row.
I need to have a filter to get all the rows which are selected and not selected on the checkbox
columns.Template(#<text> </text>).ClientTemplate("<input type='checkbox' class='chkbx'").Title("Select");
Kindly help....
The best way to achieve such functionality is to extend your data source and add boolean column in your datasource definition. Then you will able to apply custom filter like this:
dataSource.filter({ field:"Field", operator: "eq", value: true });
As far I know - there is no build kendo way to manage filtering unbounded columns.

Tablesorter filter-onlyAvail

I use tablesorter (mottie's fork v2.23.0) by the gem jquery-tablesorter-rails
I have a table where I use the classes filter-select filter-onlyAvail to get a dropdown of only available options in the column.
Recently I added jeditable to the column which adds a script tag to the cells (and maybe also embed the value in a span) like this:
<td>
<span class="editable" data-id="15" data-name="user[route]" title="Click to edit..">1</span>
<script type="text/javascript">
...script for jeditable....
</script>
</td>
The result is that the select filter dropdown shows all variants of values including the script contents.
I tried using filter_selectSource with the filter-match class added, but the contents of the filter is so common (like 1,2 99 etc) I get lots of false hits in the script.
Any pointers on how to ditch the contents of the script tags both in select population and in search results?
I ended up using data-text="value" on the element I want to use for filter and sorting.
In my case the tag.
This does not, however, update tablesorter's filter values or search content for an updated cell.
Use the filter_selectSource option to populate the filter dropdown.
Set it as an array
filter_selectSource : {
0 : [ "abc", "def", "ghi", "xyz" ]
}
or a function:
filter_selectSource : function(table, column, onlyAvail){
// get an array of all table cell contents for a table column
var array = $.tablesorter.filter.getOptions(table, column, onlyAvail);
// manipulate the array as desired, then return it
return array;
}
Also, since it is not ideal to have inline scripts in table rows which are dynamically sorted, please check out the editable widget which uses HTML5 contenteditable to allow user interaction.

Take and a array of names and values and create a dropdown that can set the values based on the objects attribute values

I am trying to figure how to set a select dropdown to the correct value of an object when returning back to a form. Here is what I have so far.
In my model I have an array that holds the name and value for a color select dropdown.
ROW_COLORS = [["Green", "greenTableRow"], ["Red", "redTableRow"], ["Orange", "orangeTableRow"], ["Yellow", "yellowTableRow"]]
I then set the ROW_COLORS to a variable #row_colors in the controller.
I am looping through an array of rule objects in my view...
<% rule_formats.each do |rule| %>
And every existing rule has an attribute called color_class.
Then in my form, I am trying to create a dropdown list with the color names (ex. green) from the array with the value of each color (greenTableRow) being the color_class.
<%= f.select("column_color", options_from_collection_for_select(#row_options, rule.color_class), {include_blank: "-- Select Color --", class: 'span7'}) %>
This however errors out with a wrong number of arguments (2 for 3..4).
I know that if #row_options was an object I could then do something like options_from_collection_for_select(#row_options, :color_class, :name, rule.color_class) But since #row_options is just an array.. I'm not sure if I'm even using the correct helper_method.
I cannot find how to set the color select to the correct value using rule.color_class when the user returns to a previously submitted form.
try using options_for_select instead of options_from_collection_for_select.
options_from_collection_for_select expects you to pass it ruby objects, whereas options_for_select expects an array of douples, which appears to be what you have

Adding fields to form based on a value in MVC 5

I have a drop down in my view. Say the selected value is 1, I want to show the next field as textbox.
#f.FormGroup().TextBoxFor(model => model.Parameter)
If the selected value is 2, then i need it as a dropdown with a list of values.
#f.FormGroup().DropDownListFor(model => model.Parameter, Model.ForumList)
If the selected value is 3, then i need to hide the parameter.
What is the best way to acheive this?
Thanks in advance,
Vanitha.
Add a function inside javascript that should be called onChange event of your dropdownlist. Pass the value of dropdown to that on change function and use if condition to decide which element to show, for this purpose you can keep a div and append elements inside it on runtime
function myfunc(dropValue)
{
if(dropValue==1)
document.getElementbyId('yourdiv').innerHtml=//the html for your element in ''
}

Dynamic Form in ASP.NET MVC3 Razor

I have a small problem.
I want to have dropdown list with some objects. After clicking on one I want to add it to list with textfield for naming it. I don't want to limit quantity of this fields. I want to receive in controller ID (stored in dropdown list) and name (given by user) for each selected item. How can I do it?
I was thinking about storing it in some fields as a text, and parsing in cotroller but I think it's not elegant.
EDIT.
Ok, Thansk for your help, but it's not working for me correctly.
I generate html like this:
<input type="hidden" value="96" name="Inputs[0].Key">
<input type="text" name="Inputs[0].Value">
In my controller I'm receiving this dictionary. The problem is that quantity of elements is correct, but all values are null. What is wrong here?
The best way to go about this is by using array-style model binding.
So, for each element you wish to name you create a hidden field to store the drop down value plus a text field to store the user-given name. You name them as follows:
<input type="hidden" name="element[0].Key" /><input type="text" name="name[0].Value" />
increasing the index value each time. This is easily achieved with a bit of JavaScript. You then create an action method which takes a KeyValuePair<string, string>[] as a parameter. You will then be able to parse through your values no problem with a loop or LINQ expression.
Use IEnumerable<KeyPairValue<string,string>> MySelectedItem = new List<KeyPairValue<string,string>>(); on model, and when adding it to the list, name it like an array:
MySelectedItem[1].Key, MySelectedItem[1].Value, MySelectedItem[2].Key...
(I haven't tested this, but it should work)
Edit: check out this blog post with better explanation on how to do it: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

Resources