Dynamically adding data to select using jQuery chosen plugin - jquery-chosen

I am facing a problem of dynamically adding options to multiple select using chosen plugin.
As I have millions of records in database so I can not populate all as options during page load.
so now I have to get matching records (on keyup event) from database using ajax and populate select box dynamically. is there any way i can achieve this.

You can just select the option you want manually by value as such:
$("#form_field").val('value_to_be_selected');
and then call
$("#form_field").trigger("liszt:updated");

We can easily append option to chosen by using the following option.
<select class=”chosen-select”>
<option value=”101”>Hyderabad</option>
<option value=”102”>Banglore</option>
<option value=”103”>Chennai</option>
</select>
$(".chosen-select").append('<option value="104”>Delhi</option>');
$(".chosen-select").trigger("chosen:updated");
For more information related to chosen jquery dropdown you can click here http://www.dotnetqueries.com/Article/151/how-to-implement-chosen-plugin-with-example

Related

Select options obtained via ajax and unmap issue

I don't see any examples linking a form using select elements with options obtained via ajax. I am wanting to initialize a view model property via ajax in order to populate some select lists in my view and data-link the selected id from my model data. I don't need these arrays when doing an .unmap() of compiled view model because I don't want to send the large lists back to the server when updating the model. So, let's say I have data with ProjectID, ProjectDescription, ProjectTypeID, ProjectPriorityID. In my view model, I need to get via ajax a list of ProjectTypes and ProjectPriorities and use them in my view so that I can select the value from a list. I'm not sure how to achieve this without getting the lists as part of the data when doing .unmap() to send it back to the server.
If there is a property for "ProjectTypes" or "ProjectPriorites" in my view model, they always become part of the data when calling .unmap().
Thank you
There are different alternative approaches. One is to have a single model that includes both 'secondary data' - such as all the ProjectTypes - which will not change dynamically, and the 'primary data' which can get updated based on user input. In that case when you get plain JSON data back from the VM hierarchy using unmap() you can 'prune' it and send only the relevant 'primary' parts back the server.
Another approach is to keep the primary and secondary data separate - so the model is just the 'primary' data, and the secondary data is separate - for example, passed in as a helper:
// Instantiate View Models
var appVm = $.views.viewModels.MyModel.map(modelData);
var typesVm = $.views.viewModels.MyTypes.map(typeData);
$.templates("#appTemplate").link("#page", appVm, {types: typesVm});
<select data-link="typeId()"><
{^{for ~types.projectTypes()}}
<option value="{{:id()}}">{{:label()}}</option>
{{/for}}
</select>
You can also choose not to compile View Models for the secondary data, if it is basically 'static', and do:
$.templates("#appTemplate").link("#page", appVm, {types: typesData});
<select data-link="typeId()"><
{^{for ~types.projectTypes}}
<option value="{{:id}}">{{:label}}</option>
{{/for}}
</select>
Another approach is to only send back to the server any data values or array that have actually changed, for example by using a CRUD/REST web service approach server updates. In that case you would need to have code that diffs the data, or use observe or observeAll to respond to incremental changes...

How to work with select input with data coming from a simple relational db model

I'm using Angularjs plugged into a API which retrieves data from a mysqlk normal relational db.
Let's say I have this simple data model in my bdd:
table car:
id,
type_id
table type:
id,
label
I have a API which retrieves the data from DB to have the list of cars and the static labels from db:
http://myapi/car/
response :
{[{id:1, type_id:2}, {id:2, type_id:3}]}
http://myapi/carstaticlabel/
response :
{[{id:1, label:convertible}, {id:2, label:limousine}, {id:3, label:pickup}]}
My aim is first to display a list of cars with the type label and then, open a dialog and being able to show a form with preloaded values of the selected car:
rendered list of cars :
1 convertible
2 pickup
rendered form for edited car whose id is 1:
select the type of car:
<select>
<option value="1">convertible</option>
<option value="2" selected="selected">limousine</option>
<option value="3">pickup</option>
</select>
I have tried different approaches but none is elegant
Solution 1
for the list of cars
change my API and make the join directly between car and type to get the list with label:
http://myapi/car/
response :
{[{id:1, type_id:2, type:{[id:2, label:limousine]}}, {id:2, type_id:3, type:{[id:3, label:pickup]}}]}
then I just assign in the form : car.id and car['type']['label']
ISSUE: each car will contain repeated information (label) >> bloated info
for the edit form:
I pass the json of selected car to the form and set default values:
<p>type: <select ng-model="car.type" ng-options="type.label for type in type_list" required></select></p>
MY ISSUE with that:
when I submit the form, I don't get the type_id directly, I get the car.type object instead, so I need to painfully translate it into an id to post into the database.
To convert I have to go into the car.type object and retrieve the object inside and then retrieve its id... very unelegant.
Solution 2
make the "join" inside angular by working with arrays.
ISSUE: very hard to default values in the form later on
I'm lost, what is usually the best practice to achieve that simple task?
You should use select as label for value in array syntax for array data sources. Your example have complex data structure which is array of objects. So you need something like this:
<select ng-model="selectedId"
ng-options="x.id as x.label for x in response">
</select>
I created little example here to demonstrate proposed solution.

How to auto fill text field in Classic ASP

I have 5 textboxes (name, roll No, address etc) and I want that when I fill my name in first text box (name) then it should fill my all 4 fields automatically which I filled.
I got this in ASP.Net but I want to use this same in classic ASP.
Sounds like you'll need to fire off an ajax call via a onkeypress javascript function call that calls a script which queries your database and gets data for user when it finds a match.
<input type="text" id="uName" name="uName" value="" onkeypress="getUserDetails(this.value)"/>
You can then use the data and further ajax to populate the additional fields.

Getting the value and text from a selectbox in ColdFusion

I have several values in a select box. Is it possible to get both the value and the text between the <option> tags when the form is submitted?
<option value="413">Highland </option>
<option value="414">Inverclyde </option>
Alternatively I suppose I have to store the names in a table or array for retrieval but would be much easier if I could just insert both in the table when the form is submitted.
As Stephen Moretti pointed out, there are at least two ways to derive the text from the value.
You could also use a list containing the value proper and the text for the value of the select. So, instead of:
<option value="23">Twenty Three</option>
use
<option value="23,TwentyThree">Twenty Three</option>
and use list*() functions on the back end.
Finally, you could use JavaScript to store the text of the selected option in a hidden field (or similar). This, in my opinion, is the least attractive option. First, it would be more work than the other options, and second because it will fail if JS is turned off on the client.
Depending on the size/type of data, I would probably either rewrite the option values, as I've described, or switch off a lookup table, as Stephen described.
If you've only got a few value/text pairs in your select, then just store the value. If you need to output the text somewhere else other than the select, just write a if/elseif/else or case block to display the text.
If you've got quite a few value/text pairs then it would be best to create a lookup table in your database with these in. You can use this to generate your select and output the text from a stored value at a later date.
How do you tell if you've got too many value/text pairs? If writing the case block to display them results in a silly amount of tedious code. ;)
Another option would be to store a struct of your value/text pairs in the session scope, then on your form action page you can use the value to easily look up your text.
The beauty of this is that it's entirely server-side and does not include an extra trip to the database.
Delete the struct from the session scope if you don't want to keep it around.

struts2 select tag, second select based on value of first select

I have 2 select, I want the second select list loads based on selected value of first select. In another word first select is a list of objects, those objects each has a list of another objects. so I want load second list based on selected object of first select. I know that can be done using JQuery, but can I do this without JavaScript/JQuery?
Thanks in advance.
There is a tag that does this for you http://struts.apache.org/2.x/docs/doubleselect.html
Out of the box it probably uses Dojo API or uses a server side request to generate the second list. I have not used any of the JQuery UI in Struts.
No, you can't do this without Javascript. With static HTML you can't achieve this effect; drop-down fields on HTML forms come pre-loaded with their options, and are independent of other fields on the form.