{"id":790,"date":"2010-08-19T01:52:55","date_gmt":"2010-08-18T20:22:55","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=790"},"modified":"2010-08-19T01:52:55","modified_gmt":"2010-08-18T20:22:55","slug":"row-expand-collapse-using-jquery-and-ajax","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/row-expand-collapse-using-jquery-and-ajax\/","title":{"rendered":"Row expand collapse using jquery and Ajax"},"content":{"rendered":"<p><strong>This article describes how to expand and collapse rows of a GridView or Table and showing data details using Jquery and Ajax<br \/>\n<\/strong><\/p>\n<p><span style=\"font-weight: normal;\"><strong>Introduction:<\/strong><\/span><\/p>\n<p>Now day&#8217;s , important issue with web applications is how quick a wed page is rendered\u00a0\u00a0and how it is animated or\u00a0\u00a0visualized<\/p>\n<p>So for quick reply from server Ajax is the solutions and for some visualizations with server side response we mostly use jquery or javascript.<br \/>\nI this article, for exp<\/p>\n<p>anding , collapsing and\u00a0\u00a0adding details to gridview row we are going to use AJAX to make call to server and we will visualized it using jquery.<\/p>\n<p><span style=\"font-weight: normal;\"><strong>Aim<\/strong><\/span><\/p>\n<p>1.\u00a0Getting details of product\u00a0\u00a0by extracting details at the next to current row and before second \u00a0 \u00a0one.<\/p>\n<p>2.\u00a0Visualizing\u00a0\u00a0the expanding of rows.<\/p>\n<p>3.\u00a0No server side postback.<\/p>\n<p>4.\u00a0Using GridView Control to bind data and simple data binding to table using scriptlet.<\/p>\n<p><span style=\"font-weight: normal;\"><strong>Using \u00a0Jquery\u00a0Code<\/strong><\/span><\/p>\n<p><strong>Expanding Row :<\/strong><\/p>\n<p>For expanding any row, we need to create an new row\u00a0\u00a0with the serverside details\u00a0\u00a0and then we will add it to the next of current row<\/p>\n<p style=\"text-align: center;\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-791\" title=\"Ajax JQuery Row Expand Collapse\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/rowexpand-300x203.png?resize=300%2C203&#038;ssl=1\" alt=\"Ajax JQuery Row Expand Collapse\" width=\"300\" height=\"203\" \/><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nvar trindex=0;\t\/\/trindex hold row index created by jquery\n$(&quot;tr&quot;).click(function() {\nif ($(this).find(&quot;td:first&quot;).length &gt; 0) { \/\/check whether it is header or content row\n\/\/row index increment to assign new row id everytime\ntrindex++;\n\/\/create a row with td colspan 3 to show product description\nvar row= '&gt;&lt;td class=&quot;currRow&quot; colspan=&quot;3&quot; &gt;&lt;\ndiv id=&quot;did&quot;&gt;&lt;img id=&quot;imagepath&quot; src=&quot;&quot;\nstyle=&quot;height: 81px; width: 104px&quot; \/&gt;  Description :&lt;\nspan id=&quot;des&quot;&gt;sd&lt;\/span&gt;&lt;p&gt;Cost :&lt;\nspan id=&quot;cost&quot;&gt;sd&lt;\/span&gt;&lt;\/p&gt;&lt;\/div&gt;&lt;\/td&gt;&lt;\/tr&gt;';\n\n\/\/adding animation to row\nvar newRow = $(&quot;&lt;tr id=tr&quot;+ trindex + row).animate({\nheight: &quot;140px&quot;,\nopacity: 0.25,\n}, 500);\n\/\/adding row to existing table\n$(this).after(newRow);\n<\/pre>\n<h5>Collapsing\u00a0 Row :<\/h5>\n<p>At the same moment we expand any row, we collapse previous created row, so we need to remember the previous created rowIndex or it&#8217;s id.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/gridcollapsed.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-805\" title=\"Ajax JQuery Row Expand Collapse\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/gridcollapsed-300x282.png?resize=300%2C282&#038;ssl=1\" alt=\"Ajax JQuery Row Expand Collapse\" width=\"300\" height=\"282\" \/><\/a><a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/gridcollapsed.png\"><br \/>\n<\/a><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n$(&quot;#&quot;+rowName).find('td').removeClass('currRow').addClass('hideRow');\n$(&quot;#&quot;+rowName).animate({\n    height: &quot;0px&quot;,\n    opacity: 0.25,\n    }, 1000, function() {\n$(&quot;#&quot;+rowName).remove();&lt;\/pre&gt;\n&lt;h5&gt;Ajax Call to the server :&lt;\/h5&gt;\nFor getting the full details about our shortly highlighted product, we need to make a asynchronous call to\nserver using AJAX. We are passing some key values to server(product Id) to extract the resultset.\n&lt;pre&gt;$.ajax({\n       type: &quot;POST&quot;,\n       url: &quot;WebService.asmx\/GetDetails&quot;,   \/\/webserviceName\/methodName\n       data: &quot;{'name': '&quot; + $(this).find(&quot;td span&quot;).text() + &quot;'}&quot;,  \/\/passing values to webservice(productid)\n       contentType: &quot;application\/json; charset=utf-8&quot;,\n       dataType: &quot;json&quot;,\n       success: function(msg) {\n       \/\/extrating response data to newly created row\n       $(&quot;#tr&quot;+trindex ).find(&quot;td #did p #cost&quot;).text(msg.d.id );\n       $(&quot;#tr&quot;+trindex ).find(&quot;td #did  #des&quot;).text(msg.d.order);\n       $(&quot;#tr&quot;+trindex ).find(&quot;td #did #imagepath&quot;).attr('src', msg.d.order);\n\/\/$(&quot;#myImage&quot;).attr(&quot;src&quot;, &quot;path\/to\/newImage.jpg&quot;);\n       },\n       error: FailedMessage     \/\/showing error message if failure\n       });\n<\/pre>\n<h5>Using The WebService.cs<\/h5>\n<p>Programmable application logic accessible via standard Web protocols.We are using webservice to extract data from database using jquery Ajax call.\u00a0\u00a0CollectData class to hold extracted details from db.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class CollectData\n{\n        public string cost { get; set; }\n        public string imagePath { get; set; }\n        public string description { get; set; }\n}\n<\/pre>\n<p>Simple queries to the database via WebMethod<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#x5B;WebMethod()]\n    public CollectData GetDetails(string name)\n    {\n        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings&#x5B;&quot;ConnectionString&quot;].\n        ConnectionString);\n        SqlCommand cmd = new SqlCommand(&quot;Select cost, imagePath, description from Products where ProductId='&quot; +\n        name + &quot;'&quot;,con);\n        SqlDataReader data;\n        CollectData c = new CollectData();\n        try\n        {\n            con.Open();\n            data=cmd.ExecuteReader();\n            if (data.Read())\n            {\n                c.cost= data&#x5B;0].ToString();\n                c.imagePath = data&#x5B;1].ToString();\n                c.description = data&#x5B;2].ToString();\n                return c;\n            }\n            else\n            {\n                c.cost = &quot;N\/A&quot;;\n                c.imagePath = &quot;N\/A&quot;;\n                c.description = &quot;N\/A&quot;;\n                return c;\n            }\n        }\n        catch (Exception ex)\n        {\n            c.cost = &quot;N\/A&quot;;\n            c.imagePath = &quot;N\/A&quot;;\n            c.description = &quot;N\/A&quot;;\n            return c;\n        }\n\n    }\n<\/pre>\n<p><strong>Conclusion :<\/strong><\/p>\n<p>We have mixed up Jquery, Ajax, server Control(Gridview ) to get quick and visualized output. Result can be little bit change as per the browsers.<\/p>\n<p><a href=\"http:\/\/www.codeproject.com\/KB\/aspnet\/rowexpandcollapse.aspx\">Uploaded on Codeproject<br \/>\n<\/a><br \/>\n<a href=\"http:\/\/www.box.net\/shared\/p0t9kzu07r\">Download Demo Article<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using JQuery and Ajax to expand and collapse the row in ASP.NET<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[21],"tags":[33,43,71,111,133,216],"class_list":["post-790","post","type-post","status-publish","format-standard","hentry","category-net","tag-ajax","tag-asp-net","tag-datagrid","tag-gridview","tag-jque","tag-web-service"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1178,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/datagrid-gridview-datalist-and-repeater-control-in-asp-net\/","url_meta":{"origin":790,"position":0},"title":"DataGrid, GridView, DataList and Repeater Control in ASP.net","author":"Jitendra","date":"October 1, 2010","format":false,"excerpt":"DataGrid, GridView, DataList and Repeater Control in ASP.net","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1527,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/ajax-based-multiselect-jquery-autocomplete-control-in-asp-net\/","url_meta":{"origin":790,"position":1},"title":"Ajax Based Multiselect JQuery Autocomplete Control in ASP.Net","author":"Jitendra","date":"February 19, 2011","format":false,"excerpt":"Tutorial on creating Ajax Based Multiselect JQuery Autocomplete User Control in ASP.Net","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"Ajax Based Multiselect JQuery Autocomplete Control in ASP.Net","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Ajax-Based-Multiselect-JQuery-Autocomplete-Control-in-ASP.Net_.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2281,"url":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/web\/disable-inputs-after-submit-to-avoid-double-submission-using-jquery-and-ajax\/","url_meta":{"origin":790,"position":2},"title":"Disable inputs after submit to avoid double submission using JQuery and Ajax","author":"Jitendra","date":"June 24, 2011","format":false,"excerpt":"This article will explain the safe way to submit the forms using JQuery. It will avoid accidental double submission.","rel":"","context":"In &quot;HTML&quot;","block_context":{"text":"HTML","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/web\/"},"img":{"alt_text":"Disable inputs after submit to avoid double submission using JQuery","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/06\/Disable-inputs-after-submit-to-avoid-double-submission-using-JQuery.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3347,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/ajax-based-autocomplete-component-in-salesforce-using-jquery-ui-and-json\/","url_meta":{"origin":790,"position":3},"title":"AutoComplete Component in Visualforce using JQueryUI","author":"Jitendra","date":"June 28, 2013","format":false,"excerpt":"In this tutorial, I am going to explain very Simple AJAX and JSON based Auto Complete component with the help of JQuery UI. First I am assuming that you already have Static Resource of named \"AutoCompleteWithModal\"\u009d. This Static resource has all images, CSS and JQuery library needed to implement this\u2026","rel":"","context":"In &quot;HTML&quot;","block_context":{"text":"HTML","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/web\/"},"img":{"alt_text":"JQuery UI and JSON based AJAX AutoComplete Component in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/06\/JQuery-UI-and-JSON-based-AJAX-AutoComplete-Component-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/06\/JQuery-UI-and-JSON-based-AJAX-AutoComplete-Component-in-Salesforce.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/06\/JQuery-UI-and-JSON-based-AJAX-AutoComplete-Component-in-Salesforce.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2843,"url":"https:\/\/www.jitendrazaa.com\/blog\/others\/tips\/streaming-api-using-jquery-salesforce\/","url_meta":{"origin":790,"position":4},"title":"Streaming API Using JQuery &#8211; Salesforce","author":"Jitendra","date":"May 6, 2012","format":false,"excerpt":"Tutorial of Streaming API in Salesforce with Source code and Demo Video - Implementation of Bayeux protocol and CometD using JQuery and JSON","rel":"","context":"In &quot;Tech Tips&quot;","block_context":{"text":"Tech Tips","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/others\/tips\/"},"img":{"alt_text":"Salesforce Streaming API - Create Push topic","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/Salesforce-Streaming-API-Create-Push-topic-1024x332.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/Salesforce-Streaming-API-Create-Push-topic-1024x332.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/Salesforce-Streaming-API-Create-Push-topic-1024x332.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1643,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/servlet-hibernate-jquery-and-ajax-based-google-like-chat\/","url_meta":{"origin":790,"position":5},"title":"Servlet, Hibernate, jQuery and Ajax based google like chat","author":"Jitendra","date":"March 14, 2011","format":false,"excerpt":"Servlet, Hibernate, jQuery and Ajax based google like chat with source code","rel":"","context":"In &quot;Hibernate&quot;","block_context":{"text":"Hibernate","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/hibernate\/"},"img":{"alt_text":"Servlet, Hibernate, jQuery and Ajax based google like chat","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Servlet-Hibernate-jQuery-and-Ajax-based-google-like-chat.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=790"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/790\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}