Sunday, November 6, 2016
jQuery: Close a Menu by Clicking Anywhere on the Document
0 comments Posted by Duc Nguyen at 3:06 AM<div class="dropMenu">
<span class="trigger">Options</span>
<div class="panel">
<ul class="list">
<li><a href="">List Item 1</a></li>
<li><a href="">List Item 2</a></li>
<li><a href="">List Item 3</a></li>
<li class="separator"></li>
<li><a href="">List Item 4</a></li>
<li><a href="">List Item 5</a></li>
</ul>
</div>
</div>
$(function() { initDropDowns($("div.dropMenu")); }); function initDropDowns(allMenus) { allMenus.children(".trigger").on("click", function() { var thisTrigger = jQuery(this), thisMenu = thisTrigger.parent(), thisPanel = thisTrigger.next(); if (thisMenu.hasClass("open")) { thisMenu.removeClass("open"); } else { allMenus.removeClass("open"); thisMenu.addClass("open"); } return false; }); }
body { font: normal 14px Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif; } .dropMenu { display: inline-block; position: relative; z-index: 1; zoom: 1; *display: inline; } .dropMenu .trigger { cursor: pointer; display: block; position: relative; top: 1px; z-index: 3; color: #1279A2; min-width: 230px; padding: 4px 7px; -webkit-border-radius: 3px; border-radius: 3px; } .dropMenu .panel { border: solid 1px #868686; position: absolute; left: 0; min-width: 242px; z-index: 2; display: none; -webkit-box-shadow: 0 2px 2px 0 rgba(0,0,0,0.3); box-shadow: 0 2px 2px 0 rgba(0,0,0,0.3); -webkit-border-radius: 0 0 3px 3px; border-radius: 0 0 3px 3px; } .dropMenu .panel .list { background: #fff; list-style: none; margin: 0; padding: 0; } .dropMenu .panel .list li { clear: both; margin: 1px; padding: 1px; /* compensate for hover border */ border-radius: 3px; -moz-border-radius: 3px; width: 120px; } .dropMenu .panel .list li:hover { border: solid 1px #d2b47a; background: #ffecb5; padding: 0; } .dropMenu .panel .list li.separator, .dropMenu .panel .list li:hover.separator { width: 232px; border: 0 none; border-bottom: solid 1px #999; height: 1px; margin: 0 5px 2px; padding: 0; background: none; float: left; /* this is only here for IE7 */ } .dropMenu .panel .list a { display: block; text-decoration: none; padding: 2px 4px; color: #1279A2; } /* Open and Hover styles */ .dropMenu.open { z-index: 999; } .dropMenu.open .trigger { z-index: 1001; -webkit-box-shadow: 0 -1px 2px 0 rgba(0,0,0,0.3); box-shadow: 0 -1px 2px 0 rgba(0,0,0,0.3); -webkit-border-radius: 3px 3px 0 0; border-radius: 3px 3px 0 0; background: #78A9FF; color: #fff; } .dropMenu .trigger:hover, .dropMenu.open .trigger { border: solid 1px #868686; padding: 3px 6px; } .dropMenu.open .panel { display: block; z-index: 1000; }
Labels: C#, Javascript, jquery
Wednesday, September 28, 2016
get the value of selected option in a select box using jQuery
0 comments Posted by Duc Nguyen at 6:58 PM
$(document).ready(function(){
$("select.country").change(function(){
var selectedCountry = $(".country option:selected").val();
alert("You have selected the country - " + selectedCountry);
});
});
Labels: ASP.net, Javascript, jquery
Friday, September 9, 2016
Until just this month one of the best Kinect 2 integration tools was hidden, like Rappuccini’s daughter, inside a walled garden. Microsoft released a Unity3D plugin for the Kinect 2 in 2014. Unfortunately, Unity 4 only supported plugins (bridges to non-Unity technology) if you owned a Unity Pro license which typically cost over a thousand dollars per year.
On March 3rd, Unity released Unity 5 which includes plugin support in their free Personal edition – making it suddenly very easy to start building otherwise complex experiences like point cloud simulations that would otherwise require a decent knowledge of C++. In this post, I’ll show you how to get started with the plugin and start running a Kinect 2 application in about 15 minutes.
(As an aside, I always have trouble keeping this straight: Unity has plugins, openFrameworks as add-ins, while Cinder has bricks. Visual Studio has extensions and add-ins as well as NuGet packages after a confusing few years of rebranding efforts. There may be a difference between them but I can’t tell.)
1. First you are going to need a Kinect 2 and the Unity 5 software. If you already have a Kinect 2 attached to your XBox One, then this part is easy. You’ll just need to buy a Kinect Adapter Kitfrom the Microsoft store. This will allow you to plug your XBox One Kinect into your PC. The Kinect for Windows 2 SDK is available from the K4W2 website, though everything you need should automatically install when you first plug your Kinect into your computer. You don’t even need Visual Studio for this. Finally, you can download Unity 5 from the Unity website.
Friday, September 2, 2016
internal class Program
{ private static void Main() { List<double> data = new List<double> {1, 2, 3, 4, 5, 6}; double mean = data.Mean(); double variance = data.Variance(); double sd = data.StandardDeviation(); Console.WriteLine("Mean: {0}, Variance: {1}, SD: {2}", mean, variance, sd); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } } public static class MyListExtensions { public static double Mean(this List<double> values) { return values.Count == 0 ? 0 : values.Mean(0, values.Count); } public static double Mean(this List<double> values, int start, int end) { double s = 0; for (int i = start; i < end; i++) { s += values[i]; } return s / (end - start); } public static double Variance(this List<double> values) { return values.Variance(values.Mean(), 0, values.Count); } public static double Variance(this List<double> values, double mean) { return values.Variance(mean, 0, values.Count); } public static double Variance(this List<double> values, double mean, int start, int end) { double variance = 0; for (int i = start; i < end; i++) { variance += Math.Pow((values[i] - mean), 2); } int n = end - start; if (start > 0) n -= 1; return variance / (n); } public static double StandardDeviation(this List<double> values) { return values.Count == 0 ? 0 : values.StandardDeviation(0, values.Count); } public static double StandardDeviation(this List<double> values, int start, int end) { double mean = values.Mean(start, end); double variance = values.Variance(mean, start, end); return Math.Sqrt(variance); } }Friday, August 26, 2016
Create Windows Service and Send Mail Daily At Fixed Time Using C#
0 comments Posted by Duc Nguyen at 12:01 AMIntroduction
In this article I am explaining how
to create a Windows Service to schedule daily mail at a specified time.
Scheduling email and sending the email is a basic requirement of any
project.
Step 1
Open Visual Studio and
create a new project. Under Windows Desktop select Windows Service and
provide a proper name and click on the OK button.
More link...
Monday, August 15, 2016
1. Query tìm kiếm trong SQL
- Bản chất của việc tìm kiếm là việc chúng ta sẽ lập trình để thực thi một câu lệnh SQL với keyword được nhập từ người dùng, cấu trúc lệnh SQL cho việc tìm kiếm trong ví dụ này như dưới đây. Trong trường hợp này mình cho phép tìm từ khóa trong cả tiêu đề "Title" và nội dung "Content"
1
2
| select * from [Articles] where ([Title] like '%keyword%' or [Content] like '%keyword%') |
1
2
| select * from [Articles] where ([Title] like N'%Võ Khánh Thụy%' or [Content] like N'%Võ Khánh Thụy%') |
1
2
3
4
| select * from [Articles] where ([Title] like N'%Võ%' or [Content] like N'%Võ%') and ([Title] like N'%Khánh%' or [Content] like N'%Khánh%') and ([Title] like N'%Thụy%' or [Content] like N'%Thụy%') |
2. Xử lý từ khóa người dùng nhập vào
- Với yêu cầu tìm kiếm mà người dùng nhập vào từ form tìm kiếm sẽ phải qua một số bước xử lý dữ liệu như loại bỏ các ký tự cấm trong SQL, xử lý tiếng việt không dấu...Tuy nhiên trong phần này mình chỉ muốn nói qua về cách tách chuỗi từ khóa được nhập vào để tăng kết quả tìm kiếm như mình đã đề cập ở trên. Mình sử dụng đoạn code dưới đây để tách chuỗi từ khóa người dùng nhập vào thành 1 mảng các từ khóa độc lập
1
2
| // Turn user input to a list of keywords.string[] keywords = txtKeyWord.Text.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); |
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| /// /// Search records from database./// |
/// the list of keywords/// all found records public List Search(List< string> keywords){// Generate a complex Sql command.StringBuilder sqlBuilder = new StringBuilder();sqlBuilder.Append("select * from [Articles] where ");foreach (string item in keywords){ sqlBuilder.AppendFormat("([Title] like '%{0}%' or [Content] like '%{0}%') and ", item);}// Remove unnecessary string " and " at the end of the command.string sql = sqlBuilder.ToString(0, sqlBuilder.Length - 5);return QueryList(sql);}/// /// Excute a Sql command./// /// Command textprotected List QueryList( string cmdText){ List articles = new List(); SqlCommand cmd = GenerateSqlCommand(cmdText); using (cmd.Connection) { SqlDataReader reader = cmd.ExecuteReader(); // Transform records to a list. if (reader.HasRows) { while (reader.Read()) { articles.Add(ReadArticle(reader)); } } } return articles;}3. Hightlight Keyword
- Sau khi hoàn thành các bước ở phần 1 & 2, chúng ta đã có thể lấy được kết quả tìm kiếm trả về, và các bạn có thể bind dữ liệu vào repeate, listview hay datalist... thì tùy vào mục đích sử dụng. Phần này mình muốn nói thêm về cách đánh dấu từ khóa tìm kiếm trên danh sách kết quả tìm kiếm trả về trên giao diện người dùng.- Đầu tiên mình sẽ viết css cho phân hightlight keyword của mình
1
2
3
4
5
6
| |
1
| keyword thay thế bằng |
1
2
3
4
5
6
7
8
9
| //hight light keywordforeach (string keyItems in keyList){ foreach (Article items in _ls) { items.Title = items.Title.Replace(keyItems, "" + keyItems + " |
); items.Content = items.Content.Replace(keyItems, "" + keyItems + "); } }- Với cách làm trên mình thấy chạy khá ổn khi tìm kiếm với lượng dữ liêu vừa phải, tuy nhiên nếu xử lý với dữ liệu lớn sẽ không được tối ưu.
Wednesday, May 4, 2016
File upload and download using JQuery and submit button ASP.net
0 comments Posted by Duc Nguyen at 8:33 PMThe Post method
This method will look in the Request object to see if there are any posted files. If so, it will loop over the files and create them on the server side. The server will return a 201 HttpStatus and a list of strings that will contain the full path of the file(s) at server side. When there are no files posted, the service will return a 401 status a.k.a. BadRequest.public HttpResponseMessage Post() { HttpResponseMessage result = null; var httpRequest = HttpContext.Current.Request; // Check if files are available if (httpRequest.Files.Count > 0) { var files = new List<string>(); // interate the files and save on the server foreach (string file in httpRequest.Files) { var postedFile = httpRequest.Files[file]; var filePath = HttpContext.Current.Server.MapPath("~/" + postedFile.FileName); postedFile.SaveAs( filePath); files.Add(filePath); } // return result result = Request.CreateResponse(HttpStatusCode.Created, files); } else { // return BadRequest (no file(s) available) result = Request.CreateResponse(HttpStatusCode.BadRequest); } return result; }
The Get Method
As mentioned before, the Get Method will accept a string file that will identify (= filename) the file that needs to be downloaded.public HttpResponseMessage Get(string id) { HttpResponseMessage result = null; var localFilePath = HttpContext.Current.Server.MapPath("~/" + id); // check if parameter is valid if (String.IsNullOrEmpty(id)) { result = Request.CreateResponse(HttpStatusCode.BadRequest); } // check if file exists on the server else if (!File.Exists(localFilePath)) { result = Request.CreateResponse(HttpStatusCode.Gone); } else {// serve the file to the client result = Request.CreateResponse( HttpStatusCode.OK); result.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read)); result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment"); result.Content.Headers.ContentDisposition.FileName = id; } return result; }
Uploading a File
Using a submit button
<form name="form1" method="post" action="api/file" enctype="multipart/form-data"> <div> <label> Using submit</label> <input name="myFile" type="file" /> </div> <div> <input type="submit" value="Submit" /> </div> </form>
Using JQuery
<form enctype="multipart/form-data"> <label> Using JQuery</label> <input name="file" type="file" /> <input type="button" id="Upload" value="Upload" /> </form>
<script type="text/javascript"> $(function () { $('#Upload').click(function () { var formData = new FormData($('form')[0]); $.ajax({ url: 'api/file', type: 'POST', // Form data data: formData, //Options to tell JQuery not to process data or worry about content-type cache: false, contentType: false, processData: false }); }); }); </script>
Subscribe to:
Posts (Atom)