I was recently requested to create multiple select drop-down list that'll also support images display.
As you probably know, the standard select HTML tag supports single-choice and text only.
Since no in-built support for this kind of behavior I had to implement it by myself and I choose the fantastic AngularJS MVC framework to do it, with a little help of jQuery.
I wanted to share my work with you so enjoy it.
Demo and source-code are available through plunker:
Yair Nevet
Blog about my +1 stuff
Thursday, 7 February 2013
Saturday, 18 August 2012
How LIVE method works in jQuery?
The idea is simple, event bubbling is occurring when any event is fired on the browser.
Base on this concept, if a specific element's event not handled, then it will anyway reach to the document level and there will be the required check.
Using a pure JavaScript object which I created, you can understand how live method is working:
Update: As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().
Base on this concept, if a specific element's event not handled, then it will anyway reach to the document level and there will be the required check.
Using a pure JavaScript object which I created, you can understand how live method is working:
Update: As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().
Labels:
JavaScript,
jquery
Tuesday, 3 January 2012
Fast value retrieving using Hashtable in C# - Get number's total occurrences
Recently I was asked about how to retrieve value from a method as fast as possible, I thought of using Hashtable and as a result I made the following class in order to demonstrate the use.
As you will see, the aim of this class is to provide information about how many times a single number reappearing in a given array or multiple array(the constructor accepts params IEnumerable<int>[]).
I'm calculating the total occurrences per number in the Constructor and saving the results at the Hashtable. by doing it, I'm preventing recalculating again and again.
class Program
{
static void Main()
{
var occurrenceCalculator = new OccurrenceCalculator(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, new[] { 1, 1, 3, 4, 5, 6, 7, 8, 9, 23 });
Console.WriteLine(occurrenceCalculator.GetOccurrences(5));
Console.WriteLine(occurrenceCalculator.GetOccurrences(23));
Console.WriteLine(occurrenceCalculator.GetOccurrences(1));
Console.WriteLine(occurrenceCalculator.GetOccurrences(250));
Console.ReadLine();
}
}
public class OccurrenceCalculator
{
private readonly Hashtable _numberRepeats = new Hashtable();
public OccurrenceCalculator(params IEnumerable<int>[] randomNumbersArrays)
{
foreach (var randomNumbersArray in randomNumbersArrays)
{
CalculateOccurrences(randomNumbersArray);
}
}
public int GetOccurrences(int number)
{
return _numberRepeats.Contains(number) ? int.Parse(_numberRepeats[number].ToString()) : 0;
}
private void CalculateOccurrences(IEnumerable<int> numbers)
{
foreach (var t in numbers)
{
if (!_numberRepeats.Contains(t))
_numberRepeats.Add(t, 1);
else
{
var currentNumber = int.Parse(_numberRepeats[t].ToString());
_numberRepeats[t] = ++currentNumber;
}
}
}
}
As you will see, the aim of this class is to provide information about how many times a single number reappearing in a given array or multiple array(the constructor accepts params IEnumerable<int>[]).
I'm calculating the total occurrences per number in the Constructor and saving the results at the Hashtable. by doing it, I'm preventing recalculating again and again.
class Program
{
static void Main()
{
var occurrenceCalculator = new OccurrenceCalculator(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, new[] { 1, 1, 3, 4, 5, 6, 7, 8, 9, 23 });
Console.WriteLine(occurrenceCalculator.GetOccurrences(5));
Console.WriteLine(occurrenceCalculator.GetOccurrences(23));
Console.WriteLine(occurrenceCalculator.GetOccurrences(1));
Console.WriteLine(occurrenceCalculator.GetOccurrences(250));
Console.ReadLine();
}
}
public class OccurrenceCalculator
{
private readonly Hashtable _numberRepeats = new Hashtable();
public OccurrenceCalculator(params IEnumerable<int>[] randomNumbersArrays)
{
foreach (var randomNumbersArray in randomNumbersArrays)
{
CalculateOccurrences(randomNumbersArray);
}
}
public int GetOccurrences(int number)
{
return _numberRepeats.Contains(number) ? int.Parse(_numberRepeats[number].ToString()) : 0;
}
private void CalculateOccurrences(IEnumerable<int> numbers)
{
foreach (var t in numbers)
{
if (!_numberRepeats.Contains(t))
_numberRepeats.Add(t, 1);
else
{
var currentNumber = int.Parse(_numberRepeats[t].ToString());
_numberRepeats[t] = ++currentNumber;
}
}
}
}
Monday, 4 July 2011
The mysterious LINQ extenstion methods
A little embarrassing, but only recently I have been realized what stands behind the mysterious methods which added to some types in .NET
It turns out that Microsoft has added extensions methods to all types that are implements the IEnumerable interface, like: Dictionary, ArrayList, List etc.
As an example, look at the following code and the possible cool LINQ IENUMERABLE Method Extension, I want to find the names which starts with 'Y':
Given this List object: var names = new List<string> {"Yair", "David", "Joe", "John", "Yan", "Yuri", };
Option 1 - Regular foreach loop
var filteredNames = new List<string>();
foreach (var name in names)
{
if (name.StartsWith("Y"))
{
filteredNames.Add(name);
}
}
Option 2 - LINQ query
var query = from n in names
where n.StartsWith("Y")
select n;
Option 3 - LINQ IENUMERABLE Method Extention
var filteredNames = names.Where(name => name.StartsWith("Y"));
as a reference you can take a look at msdn documentation: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.aspx
It turns out that Microsoft has added extensions methods to all types that are implements the IEnumerable interface, like: Dictionary, ArrayList, List etc.
As an example, look at the following code and the possible cool LINQ IENUMERABLE Method Extension, I want to find the names which starts with 'Y':
Given this List object: var names = new List<string> {"Yair", "David", "Joe", "John", "Yan", "Yuri", };
Option 1 - Regular foreach loop
var filteredNames = new List<string>();
foreach (var name in names)
{
if (name.StartsWith("Y"))
{
filteredNames.Add(name);
}
}
Option 2 - LINQ query
var query = from n in names
where n.StartsWith("Y")
select n;
Option 3 - LINQ IENUMERABLE Method Extention
var filteredNames = names.Where(name => name.StartsWith("Y"));
as a reference you can take a look at msdn documentation: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.aspx
Labels:
c#,
enumerable,
linq
My first post
This is my first post at Blogger.
I hope to share with you my technology experiences as soon as I exposed to them.
Yair
Subscribe to:
Posts (Atom)