If you working in web development for a long time, you must have heard about knockout.js. It implements MVVM pattern in javascript. Siverlight guys must be familiar with this word. This helps to create rich, responsive display and editor user interfaces with a clean underlying data model. User Need not worry about binding the data model to your view. Any time , Your data changes your View automatically refreshes.
While working on a project, I was asked to create a MultiSelect Box. As , I was using I thought of creating Custom binding handler so that I can use it time and again my code.
Below is the HTML :
1) Have a select element as below :
<select id=”customID” data-bind=”Values: ViewModelPropertyName”>
<option value=”1″>value1</option>
<option value=”2″>value2</option>
<option value=”3″>value3</option>
<option value=”4″>value4</option>
</select>
<img id=”imgcustomID” src=”../add.png”/><br/>
<ul id=”ulcustomID” class=”navlist”></ul>
2) Add below style in your css :
.navlist
{
margin-left: 0;
padding-left: 0;
list-style: none;
}
.navlist li
{
padding-left: 25px;
background-image: url(../images/minus.png);
background-repeat: no-repeat;
background-position: 0 .1em;
height:16px;
}
3) Add below Javascript Code in your HTML Files:
<script type=”text/javascript”>
ko.bindingHandlers.Values = {
init: function (element, valueAccessor) {
var _this = ko.bindingHandlers.Values;
var id = jQuery(element).attr(‘id’);
jQuery(element).after(“<img src=’../../../images/add.png’ id=’img” + id + “‘ onClick=’javascript: ko.bindingHandlers.Values.setCommand(\”" + id.toString() + “\”);’ ></img><ul class=’navlist’ id=’ul” + id + “‘></ul>”);
var values = new Array();
var value = ko.utils.unwrapObservable(valueAccessor());
if (value) {
values[0] = value;
if (values[0] == “”)
values.splice(0, 1);
setTimeout(“ko.bindingHandlers.Values.addLI(‘” + id + “‘,’” + values + “‘,0);”, 300);
}
},
setCommand: function (id) {
var _this = ko.bindingHandlers.Values;
if (jQuery(‘#’ + id).attr(‘value’) !== “”) {
var select = document.getElementById(id);
if (select.selectedIndex >= 0)
_this.addLI(id, select.options[select.selectedIndex].value, 1);
}
},
addLI: function (id, values, mode) {
var _this = ko.bindingHandlers.Values;
var currentvalues = [];
var prop = id.replace(“slct”, “”);
var viewModelData = JSON.parse(ko.toJSON(viewModel));
if (viewModelData[prop]) {
if (viewModelData[prop].toString().search(“,”) !== -1) {
var currentvalues = eval(“viewModel.” + prop + “().split(‘,’)”);
}
else {
currentvalues[0] = eval(“viewModel.” + prop + “()”);
}
if (currentvalues[0] == “”)
currentvalues.splice(0, 1);
if (values !== “” && mode == 1)
currentvalues.pushIfNotExist(values);
eval(“viewModel.” + prop + “(‘” + currentvalues.join(‘,’) + “‘)”);
}
else {
eval(“viewModel.” + prop + “(” + values + “)”);
}
if (mode == 0) {
jQuery.each(currentvalues, function (key, value) {
var li = document.createElement(‘li’);
var select = document.getElementById(id);
li.innerHTML = _this.getText(id, value.trim());
li.customValue = value;
var ul = document.getElementById(‘ul’ + id);
li.onclick = function () {
var option = document.createElement(‘option’);
option.text = this.innerHTML;
option.value = this.customValue;
select.add(option);
ul.removeChild(li);
eval(“viewModel.” + prop + “(_this.getValues(‘” + id + “‘))”);
};
ul.appendChild(li);
_this.removeOption(id, value.trim());
});
}
else {
var li = document.createElement(‘li’);
var select = document.getElementById(id);
li.innerHTML = _this.getText(id, values);
li.customValue = values;
var ul = document.getElementById(‘ul’ + id);
li.onclick = function () {
var option = document.createElement(‘option’);
option.text = this.innerHTML;
option.value = this.customValue;
select.add(option);
ul.removeChild(li);
eval(“viewModel.” + prop + “(_this.getValues(‘” + id + “‘))”);
};
ul.appendChild(li);
_this.removeOption(id, values);
}
},
getValues: function (id) {
return jQuery(‘#ul’ + id + ‘ li’).map(function () {
return jQuery(this).attr(‘customValue’);
}).get().join(“, “);
},
getText: function (id, value) {
return jQuery(“#” + id + ” option[value='" + value + "']“).text();
},
removeOption: function (id, value) {
jQuery(“#” + id + ” option[value='" + value + "']“).remove();
}
};
});
}
var viewModel = {};
viewModel .ViewModelPropertyName=’1,2′;
ko.applyBindings(viewModel);
</script>
4) Add two images add.png and minus.png into your images folder and make sure they are been accessible from your css and html file.
Hopefully ths will be very useful for those who wants to use knockout.js in their projects. To use the above code please do not forget to add latest jQuery library and knockout.js
Happy Coding :)
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.
The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.