顯示具有 jQuery 標籤的文章。 顯示所有文章
顯示具有 jQuery 標籤的文章。 顯示所有文章

2013年6月19日 星期三

jQuery UI - Date Picker in MVC4

image

 

In the MVC view, change from EditorFor() to TextBoxFor() and add a htmlAttribute @class = “data-datepicker”

image

 

In the script,

1. get and format now as today date

2. $(‘input.data-datepicker’) to target the field defined in view

3. .attr(“value”, now) to assign default value of today

4. .datepicker() to enable jQuery UI datepicker function

image

 

In the MVC view, add 3 references

1. jQueryUI (~/bundles/jqueryval)

2. CSS of jQueryUI (~/Content/themes/base/css)

3. Our own javascript file (~/bundles/MyScript)

image

 

In the BundleConfig.cs, we can see jquery.ui.datepicker.css included in ~/Content/themes/base/css

image

 

Output result:

image

 

Reference Site:

http://jqueryui.com/datepicker/

image

2013年5月2日 星期四

Change color of navigation bar in MVC 4 template

Bootstrap + jQuery to change color of navigation bar in MVC 4

image

_Layout.cshtml

1. remove id=”menu” in <ul> tag

2. add class=“nav nav-pills” in <ul> tag

<nav>
    <ul class="nav nav-pills">
        <li><a href="/">Home</a>
        <li><a href="/Home/About">About</a>
        <li><a href="/Home/Contact">Contact</a>
    </li></ul>
</nav>

 

jQuery

$(function () {

var path = window.location.pathname;

//Method 1:
var $a = $('nav ul li a[href="' + path + '"]');
$a.parents("li:first").addClass("active");

//Method 2:
//$("nav ul li a").each(function (index, elem) {
// if ($(elem).attr("href") == path) {
// $(this).parents("li:first").addClass("active");
// }
//});

});

2013年4月17日 星期三

jQuery ajax() Function

ajax() function provides extra control over making Ajax calls to a server, there are several properties can be defined in JSON format, for example:

url

contentType

data

dataType

success

error

type (GET or POST)

image

 

Demo Example:

WCF Web Service code

image

Customer class

image

OperationStatus class

image

image

Output Result

image

Fiddler Result:

image

 

Addition Note:

http://api.jquery.com/jQuery.ajax/

image

2013年4月16日 星期二

jQuery POST Request

$.post(url, data, callback, dataType) can post data to a server and retrieve results

image

post() can also be used to interact with an Ajax-enabled WCF service

image

 

Demo Example:

image

image

Output Result

image

Fiddler Result:

image

image

 

WCF Web Service

image

image

Output Result

image

Fiddler Result:

image

 

Additional Note:

A good tool to trace all network routes (request and response)

http://fiddler2.com/

image

image

jQuery GET Requests

$.get(url, data, callback, dataType) can retrieve data from a server:

image

$ is a jQuery object and .get() is a global function

dataType can be one of:

HTML, XML, JSON, string, or script

image

 

$.getJSON(url, data, callback) can retrieve JSON data from a server

image

 

Demo Example:

image

CustomerJson.aspx.cs

image

$.get(url, data, callback, dataType)

image

$.getJSON(url, data, callback)

image

 

Additional Notes:

1. http://james.padolsey.com/jquery

image

2. .load() is always HTML datatype

3. .load() without data –> it’s a GET request

4. .load() with data –> become POST request

image

Loading HTML Content from the Server

$(selector).load(url, data, callback) allows HTML content to be loaded from a server and added into a DOM object

image

We can even filter the content that returned from server

image

Data can be passed to the server by using load(url, data)

image

A callback function used

image

 

Demo Example:

1. .load()

image

image

Output result:

image

 

2. Filter content

image

image

Output result:

image

 

3. Callback function

image

image

 

4. With Data passed to server

image

image

Output result:

image