Click here to Skip to main content
15,896,557 members
Articles / Web Development / ASP.NET

Easy Way to Implement Ajax using Jquery in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
28 Aug 2010CPOL3 min read 57.7K   1.1K   35  
Simple steps do an Ajax operation using Jquery and ASP.NET
(function ($) {
    $.fn.tabletojson = function (options) {
        var defaults = {
            headers: null,  // add headers seperated by commas if you don't want to use headers in table
            attribHeaders: null,//if you pass in JSON like {'attribute name':'fieldname'} -
			//	"{'customerID':'CustomerID','orderID':'OrderID'}", the plugin will find these attributes
			// and build elements using the fieldname you indicate.
            returnElement: null,
            // if you add a hidden field id like e.g. 	
		    //$("#myTable").tabletojson({returnElement:'#myInputElement'})   
            //here then the chain won't be broken and the json will be dumped to myInputElement, 
            //else, this plugin will just return the json, e.g. var json = 
			//$("#myTable").tabletojson()     
            complete: null  //pass me a function and I'll dish you the json
        };

        var options = $.extend(defaults, options);
        var selector = this;
        var jsonRowItem = ""
        var jsonItem = new Array();
        var jsonRow = new Array();
        var heads = []
        var rowCounter = 1
        var comma = ",";
        var json = "";

        if (options.headers != null) {
            options.headers = options.headers.split(' ').join(''); 
			//this is in case you delimit like this ddd, dddd, ddddd instead of
            heads = options.headers.split(",");                    
			// ddd,dddd,ddddd,ddd  I gotta remove the spaces to make this work 
        }   //right.



        var rows = $(":not(tfoot) > tr", this).length;
        $(":not(tfoot) > tr", this).each(function (i, tr) {
            jsonRowItem = ""

            //use return to continue and return false to exit loop
            if (this.parentNode.tagName == "TFOOT") {
                return;  //we don't care about the footer so continue on...
            }
            if (this.parentNode.tagName == "THEAD") {
                if (options.headers == null) {  //if we don't indicate headers, then grab the actual headers in thead.
                    $('th', tr).each(function (i, th) {
                        heads[heads.length] = $(th).text()
                    });
                }
            }
            else {

                if (options.attribHeaders != null) {
                    var h = eval("(" + options.attribHeaders + ")");

                    for (z in h) {
                        heads[heads.length] = h[z];
                    }
                }

                rowCounter++
                //collect row stuuf
                var headCounter = 0
				
                jsonRowItem = "{"
                jsonItem.length = 0;
                $('td', tr).each(function (i, td) {
                    var re = / /gi
                    var v = $(td).text().replace(re, '')
                    jsonItem[jsonItem.length] = "\"" + heads[headCounter] + "\":\"" + v + "\"";
                    headCounter++

                });

                if (options.attribHeaders != null) {
                    for (z in h) {
                        jsonItem[jsonItem.length] = "\"" + heads[headCounter] + "\":\"" + tr[z] + "\"";
                        headCounter++
                    }
                }
                
                jsonRowItem += jsonItem.join(",");
                jsonRowItem += "}";
                jsonRow[jsonRow.length] = jsonRowItem;


            }

        });
        json += "[" + jsonRow.join(",") + "]"
        
        if (options.complete != null) {
            options.complete(json);
        }

        if (options.returnElement == null)
            return json;
        else {
            $(options.returnElement).val(json);
            return this;
        }

    }
})(jQuery)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions