Chosen and Jquery and MVC
In the MVC forums I have seen a reference to Chosen. From the description here:
“Chosen is a JavaScript plugin that makes long, unwieldy select boxes much more user-friendly.”
It look beautifool – and I see the opportunity to improve my toolbox. I said: “I will do in 10 minutes”. Well, it did take longer – but not so long.
I will make an example my tutorial mvc and ajax – since it does not require any database.
So I download the source from https://github.com/harvesthq/chosen/ , put the .js in Scripts folder, the .css in Contents folder and registered in JqueryMVCRazor\JqueryMVCRazor_Web\Views\Shared\_Layout.cshtml
<link href="@Url.Content("~/Content/chosen.css")" rel="stylesheet" type="text/css" /> <script src="@Url.Content("~/Scripts/chosen.jquery.min.js")" type="text/javascript"></script>
Here is the code : ( from JqueryMVCRazor\JqueryMVCRazor_Web\Views\Shared\EditorTemplates\EditEmployeeViewModel.cshtml ):
@{ var idcombo = "#"+ ViewData.TemplateInfo.GetFullHtmlFieldId("cmbemp" + Model.Employee.IdEmployee); }<script> $(document).ready(function () { $("@idcombo").chosen({ no_results_text: "No results matched"}); }); </script>
And I said, this will be all.
Not so: see the picture – the names does not show up:
First I was thinking that it does not select the item. After several attempts, I realize the “…” ( three dots) – that means the width was not enough.
Next move, since I was in the javascript mood, was to add some javascript to adjust the width to fit:
So the code becomes:
$("@idcombo").chosen({ no_results_text: "No results matched" }); $("@idcombo").width("350");
No difference. So I tried the opposite way:
$("@idcombo").width("350"); $("@idcombo").chosen({ no_results_text: "No results matched" });
And , thinking about a bit, it is better right in the Dropdown declaration :
@Html.DropDownList("cmbemp" + Model.Employee.IdEmployee, new SelectList(Model.DepartmentList, "IDDepartment", "NameDepartment", Model.Employee.Department.IdDepartment),new{ style="width:350px;"})
Summary:
Integration of Chosen with MVC dropdown is rather simple. Add the .js, the .css, register in the page, add $(“@idcombo”).chosen() declaration and do not forget the width of the dropdown ( select )
Source code here
Later edit: Please add also
to the header
Hi, can you post a simple version of using choosen for dropdown, example: one action that send list of items from controller and dropdown in view (referencing jscript code and using html helper), i am curios of sending selected value from choosen dropdown.
Sure. IDSelectedTags { get; set; }
Look at
http://pmkb.codeplex.com/
You have “edit link” – just edit a link and you will see a list of tags.
More simply :
in the LinkAddOrEdit.cs file I have
public List
This is automatically filled by MVC
LOL, i think you didn’t understand my question, and i don’t see any “edit link” on http://pmkb.codeplex.com/
I just wanted to see basic usage of choosen dropdown, but nevermind i created it myself and testing right now, and when it comes to get selected value it’s pretty easy to collect it with jquery .val() or getselectedvalue().
If you want on client side, it’s the same as any dropdown (* chosen actually hides the dropdown) ( for multiple)
For server side, you will add an int or an List
Did you try to create choosen dropdown with groups like the “Single Select with Groups” on http://harvesthq.github.com/chosen/
that would be very nice for users
It requires some syntax to think about…
Thanks, this is exactly what I’ve been looking for. I’m hoping to use partial views to implement both Chosen and JEditable.. we’ll see. I really want to show the table as plain text until the user double clicks a cell, and then have it be editable, only using Chosen as the plugin. THAT my friend, would be a terrific tutorial (not to say this isn’t, this tutorial is great as well)
Hello There,
Thank You for this article.
http://harvesthq.github.com/chosen/
Please goto the above url and see the multi select option My requirement is to use the same selector but I want the the auto selection to happen only when I enter a special Character like #
For example I want to type in the input box like
I lived in countries like #India #USA with my @friend-1 and friend-2
So the below drop-down should show a list of countries when I press # and when I press @ it should display a list of my contacts
Could you please help ?
You modify the list when typing. It’s easy if you know javascript.