{"id":224,"date":"2016-02-17T06:33:40","date_gmt":"2016-02-17T06:33:40","guid":{"rendered":"http:\/\/fluentreports.com\/blog\/?p=224"},"modified":"2016-02-17T06:33:40","modified_gmt":"2016-02-17T06:33:40","slug":"nativescript-tip-access-a-related-view-of-a-gridviewlistviewrepeater","status":"publish","type":"post","link":"http:\/\/fluentreports.com\/blog\/?p=224","title":{"rendered":"NativeScript: Tip - access a related view of a  GridView\/ListView\/Repeater"},"content":{"rendered":"<p>I was asked earlier this week about how to access a related label when you click on a button or other object in the list of a ListView\/GridView\/Repeater. Since it took me a little while to figure this out for my own project a couple weeks ago and since someone else ran into this issue also, I'm going to post about it.<\/p>\n<p>Pretend our template is such:<br \/>\n<pre>&amp;lt;Page xmlns=&quot;http:\/\/www.nativescript.org\/tns.xsd&quot; loaded=&quot;pageLoaded&quot;&amp;gt;\n &amp;lt;ListView items=&quot;{{items}}&quot;&amp;gt;\n&nbsp;&nbsp;&amp;lt;ListView.itemTemplate&amp;gt;\n&nbsp;&nbsp; &amp;lt;StackLayout&amp;gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&amp;lt;Label id=&quot;wow&quot; text=&quot;{{text}}&quot;\/&amp;gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&amp;lt;Button text=&quot;Tap&quot; tap=&quot;onTap&quot;\/&amp;gt;\n&nbsp;&nbsp; &amp;lt;\/StackLayout&amp;gt;\n&nbsp;&nbsp;&amp;lt;\/ListView.itemTemplate&amp;gt;\n &amp;lt;\/ListView&amp;gt;\n &amp;lt;\/Page&amp;gt;<\/pre><br \/>\nThe code would be:<br \/>\n<pre>exports.pageLoaded = function(args) {\n&nbsp;&nbsp;&nbsp;&nbsp;var page = args.object;\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Set our Record Set Data up.&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;var data = [{text: &quot;One&quot;, color: &quot;#FF0000&quot;},\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{text: &quot;Two&quot;, color: &quot;#00FF00&quot;},\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{text: &quot;Three&quot;, color: &quot;#0000FF&quot;}];\n&nbsp;&nbsp;&nbsp;&nbsp;page.bindingContext = {items: data};\n};\n\nexports.onTap&nbsp;&nbsp;= function(args) {\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Find the Parent StackLayout\n&nbsp;&nbsp;&nbsp;&nbsp;var stackLayoutContainer = args.object.parent;\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Disable button\n&nbsp;&nbsp;&nbsp;&nbsp;args.object.isEnabled = false;\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Find the &quot;Label&quot; Child inside the StackLayout\n&nbsp;&nbsp;&nbsp;&nbsp;var label = stackLayoutContainer.getViewById(&#039;wow&#039;);&nbsp;&nbsp;&nbsp;&nbsp;\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Look at our Specific Record \n&nbsp;&nbsp;&nbsp;&nbsp;var dataRecord = args.object.bindingContext;\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Set the Label color to our Records color...\n&nbsp;&nbsp;&nbsp;&nbsp;label.backgroundColor = dataRecord.color;\n};<\/pre><br \/>\nSo in this example; I'm showing you two cool things about the args that get sent to the onTap function inside a <em>ListView<\/em>.\u00a0\u00a0 The first item, is <strong>args.object<\/strong> -- this is the actual button.\u00a0 So if you wanted to disable the button you could something like <strong>args.object.isEnabled = false; <\/strong>and it would disable just that specific button.\u00a0 However, items in the view tree have a parent.\u00a0\u00a0 In our Declarative UI XML you can see that the <em>Button<\/em> is a child of the <em>StackLayout<\/em>, then that means the <em>StackLayout<\/em> is the parent.\u00a0 So once we have the parent object, we just search its children for the child (label) we want.\u00a0\u00a0\u00a0 Once we have the label we can do whatever we want with it.<\/p>\n<p>The next thing I'm showing you is that in addition to the <em>Button<\/em> having a <strong>parent <\/strong>property, in this case the <em>Button<\/em> will also have a <strong>bindingContext<\/strong> property.\u00a0 The bindingContext in this case is the single record that was used in the creation of this specific view object (i.e. <em>Button<\/em> in this case).\u00a0 So for the first <em>Button<\/em>, it would have the full \"One\" record.\u00a0 The second <em>Button<\/em> has the \"Two\" record and the final <em>Button<\/em> has the \"Three\" record.\u00a0 You can use this information to be able to do just about anything from the <em>Button<\/em> since you now know which record this tap event is now related to.\u00a0\u00a0 Instead of a <em>Button<\/em>, we could have used a <em>Image<\/em>, <em>Label<\/em>, or any other view object.\u00a0\u00a0 The <strong>args <\/strong>passed to the tap event would contain the same information.<a href=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2016\/02\/blog-listview-samepl.png\" rel=\"attachment wp-att-225\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-225\" src=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2016\/02\/blog-listview-samepl.png\" alt=\"blog-listview-sample\" width=\"416\" height=\"509\" srcset=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2016\/02\/blog-listview-samepl.png 561w, http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2016\/02\/blog-listview-samepl-245x300.png 245w\" sizes=\"auto, (max-width: 416px) 100vw, 416px\" \/><\/a><\/p>\n<p>As you can see in this simple sample, by clicking the second button; it found the \"Two\" label, changed the color to the color that we set in the \"Two\" record; and also disabled the tap button.\u00a0\u00a0\u00a0 If you were to tap the Third button, it would change that label to be blue, since the third record has color: \"#0000FF\" as its color.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was asked earlier this week about how to access a related label when you click on a button or other object in the list of a ListView\/GridView\/Repeater. Since it took me a little while to figure this out for my own project a couple weeks ago and since someone else ran into this issue&hellip; <a class=\"more-link\" href=\"http:\/\/fluentreports.com\/blog\/?p=224\">Continue reading <span class=\"screen-reader-text\">NativeScript: Tip - access a related view of a  GridView\/ListView\/Repeater<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[15,39],"tags":[48,47,16,50,49],"class_list":["post-224","post","type-post","status-publish","format-standard","hentry","category-nativescript","category-tips","tag-gridview","tag-listview","tag-nativescript","tag-nested-object","tag-repeater","entry"],"_links":{"self":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=224"}],"version-history":[{"count":1,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/224\/revisions"}],"predecessor-version":[{"id":226,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/224\/revisions\/226"}],"wp:attachment":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=224"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}