{"id":3555,"date":"2012-05-28T06:59:00","date_gmt":"2012-05-28T06:59:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/antenna-aligner-part-4-roleing-in-the-deep\/"},"modified":"2016-07-28T10:50:49","modified_gmt":"2016-07-28T10:50:49","slug":"antenna-aligner-part-4-roleing-in-the-deep","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/antenna-aligner-part-4-roleing-in-the-deep\/","title":{"rendered":"Antenna Aligner Part 4: Role&#8217;ing in the deep"},"content":{"rendered":"<p>Since last time I&#8217;ve been trying to sort out the general workflow of the app. It&#8217;s fundamentally not hard, there is a list of transmitters, you select a transmitter and it shows the compass view. <\/p>\n<p>Having done quite a bit of ajax\/asp.net\/html in the past, I immediately started off by creating two divs within my &#8216;page&#8217;, one for the list, one for the compass. Then using the onClick event in the list, this will switch the display attribute on the divs. <\/p>\n<p>This seemed to work, but did lead to some dodgy transitional redrawing artefacts which I was not happy with.<\/p>\n<p>So after some Googling I realised I was doing it all wrong! JQuery mobile has the concept of giving an object in html a data-role. By giving a div the attribute data-role=&#8221;page&#8221; it is then treated as a separate page on the mobile device.<\/p>\n<p>Within the code, this is referenced like a html anchor in the form #mypage. Using this system, page transitions such as fade or slide are automatically applied which adds to the whole authenticity of the app! Here is a simple example:<\/p>\n<p>.      <br \/>&lt;a href=&#8221;#&#8217;compasspage&#8221;&gt;compass&lt;\/a&gt;       <br \/>.       <br \/>&lt;div data-role=&#8221;page&#8221; id=&#8221;quot;compasspage&#8221;&#8221; data-add-back-btn=&#8221;true&#8221;&gt;<\/p>\n<p>But I don&#8217;t want just a static link, I want to dynamically create my list, and get each list elements to switch to the compass page with the right information. So here is the jquery that I used to dynamically inject new &lt;li&gt; rows into the &lt;ul&gt; block.<\/p>\n<p>$(&#8216;ul&#8217;).append($(&#8216;&lt;li\/&gt;&#8217;, {&#160;&#160;&#160; \/\/here appendin `&lt;li&gt;`      <br \/>&#160;&#160;&#160; &#8216;data-role&#8217;: &#8220;list-divider&#8221;       <br \/>}).append($(&#8216;&lt;a\/&gt;&#8217;, {&#160;&#160;&#160; \/\/here appending `&lt;a&gt;` into `&lt;li&gt;`       <br \/>&#160;&#160;&#160; &#8216;href&#8217;: &#8216;#compasspage&#8217;,       <br \/>&#160;&#160;&#160; &#8216;data-transition&#8217;: &#8216;none&#8217;,       <br \/>&#160;&#160;&#160; &#8216;onclick&#8217;: &#8216;selectTx(&#8216; + i + &#8216;)&#8217;,       <br \/>&#160;&#160;&#160; &#8216;html&#8217;: buttonHtml       <br \/>}))); $(&#8216;ul&#8217;).listview(&#8216;refresh&#8217;);<\/p>\n<p><a href=\"http:\/\/www.simple-talk.com\/blogbits\/chris_george\/Antenna-Aligner-Part-4-Roleing-in-the-de_BF77\/blog_list_3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/blogbits\/chris_george\/Antenna-Aligner-Part-4-Roleing-in-the-de_BF77\/blog_list_thumb_3.png\" width=\"199\" height=\"144\" class=\"float-right\" alt=\"blog_list_thumb_3.png\" \/><\/a>This is called within a for loop so the first 5 appropriate transmitters are used. There are several things of interest to note here.   <\/p>\n<p>Firstly, I could not find a more elegant way to tell the target page which transmitter I&#8217;ve clicked on, so I have used the onclick event as well as the href attribute. The onclick event fires &#8216;selectTx&#8217; which simply sets a global member variable to the specific index number I&#8217;ve clicked on. Yes it&#8217;s not nice, but it works. <\/p>\n<p>Secondly, the data-transition attribute is set to &#8216;none&#8217;. I wanted the transition between the pages to be a whooshy slidey effect. However this worked going to the compass page, but returning to the list page gave some undesirable visual artefacts (flickering, redrawing etc.). So I decided to remove the transitions all together, which was a shame. <\/p>\n<p>Thirdly, rather than embedding loads of html into the append command, I removed this out into a variable &#8216;buttonHtml&#8217;. Doing this really tidied up my code.<\/p>\n<p>Until next time!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since last time I&#8217;ve been trying to sort out the general workflow of the app. It&#8217;s fundamentally not hard, there is a list of transmitters, you select a transmitter and it shows the compass view. Having done quite a bit of ajax\/asp.net\/html in the past, I immediately started off by creating two divs within my&#8230;&hellip;<\/p>\n","protected":false},"author":37746,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-3555","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/37746"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=3555"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3555\/revisions"}],"predecessor-version":[{"id":42162,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3555\/revisions\/42162"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=3555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=3555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=3555"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=3555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}