Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kieron 152 posts 390 karma points
    Oct 02, 2018 @ 11:56
    Kieron
    0

    Custom List View, cant figure out how to bring an Image into the view

    Hi all, I've followed both the Umbraco TV guide, and this guide https://24days.in/umbraco-cms/2016/custom-list-view-layouts/ And I just can't seem to get the image to work.

    In my document type, it's a Media Picker, with an ID of 'picture'

    In my custom list view, the column is called Image ID (don't think this is important)

    View is this:

    <div ng-controller="My.ListView.Layout.SongsLayoutController as vm"
         ng-if="items"
         class="my-songs-layout__cards">
    
       <div
          class="my-songs-layout__card"
          ng-repeat="item in items"
          ng-class="{'-selected':item.selected}"
          ng-click="vm.selectItem(item, $event, $index)"
          ng-style="{'background-image': 'url(' + item.picture + ')'}">
    
          <div class="my-songs-layout__overlay">
            <div class="my-songs-layout__released">{{ item.authorName }}</div>
            <div class="my-songs-layout__artist">{{ item.authorName }}</div>
            <div class="my-songs-layout__name">{{ item.authorName }}</div>
            <div class="my-songs-layout__genre">{{ item.authorName }}</div>
          </div>
    
          <div class="my-songs-layout__button" ng-click="vm.clickItem(item)">View song</div>
    
       </div>
    
    </div>
    

    Controller is this:

    (function () {
        "use strict";
    
        function SongsLayoutController($scope, listViewHelper, $location, mediaResource, mediaHelper) {
    
            var vm = this;
    
            vm.selectItem = selectItem;
            vm.clickItem = clickItem;
    
            // Init the controller
            function activate() {
    
                // Load background image for each item
                angular.forEach($scope.items, function (item) {
                    getBackgroundImage(item);
                });
    
            }
    
            // Load background image
            function getBackgroundImage(item) {
                mediaResource.getById(item.picture)
                    .then(function (media) {
                        // find the image thumbnail
                        item.picture = mediaHelper.resolveFile(media, true);
                    });
            }
    
            // Item select handler
            function selectItem(selectedItem, $event, index) {
    
                // use the list view helper to select the item
                listViewHelper.selectHandler(selectedItem, index, $scope.items, $scope.selection, $event);
                $event.stopPropagation();
    
            }
    
            // Item click handler
            function clickItem(item) {
    
                // change path to edit item
                $location.path($scope.entityType + '/' + $scope.entityType + '/edit/' + item.id);
    
            }
    
            activate();
    
        }
    
        angular.module("umbraco").controller("My.ListView.Layout.SongsLayoutController", SongsLayoutController);
    
    })();
    

    So you can see in the controller above I have the item.picture referenced but it's coming through with errors like this:

    https://gyazo.com/cc84d77812c1ff957c8478dc2d2c15f3

    And

    https://gyazo.com/e0c72166df4c885f76231945cc6cac88

    The Listview in console comes out like so

    ng-style="{'background-image': 'url(' + item.picture + ')'}" style="background-image: url(&quot;umb://media/c4797bb5fd464057b19d3be6e0841a0a&quot;);"
    

    For good measure, here is the manifest

    {
        javascript: [
            '~/App_Plugins/AuthorLayout/authorlayout.controller.js'
        ],
        css: [
            '~/App_Plugins/AuthorLayout/authorlayout.css'
        ]
    }
    

    And here is the CSS

    .my-songs-layout__cards {
        display: flex;
        flex-wrap: wrap;
    }
    
    .my-songs-layout__card {
        background-color: #ffffff;
        background-size: cover;
        background-position: center center;
        box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
        height: 200px;
        border: 5px solid transparent;
        display: flex;
        flex-direction: column;
        color: #ffffff;
        margin: 10px;
        user-select: none;
        position: relative
    }
    
    .my-songs-layout__card.-selected {
        border-color: #2e8aea;
    }
    
    .my-songs-layout__card:hover {
        cursor: pointer;
    }
    
    .my-songs-layout__overlay {
        background: rgba(0, 0, 0, 0.3);
        padding: 20px;
    }
    
    .my-songs-layout__released {
        margin-bottom: 5px;
        opacity: 0.5;
    }
    
    .my-songs-layout__genre {
        font-size: 11px;
    }
    
    .my-songs-layout__name {
        font-size: 24px;
        font-weight: bold;
        line-height: 1.3em;
    }
    
    .my-songs-layout__button {
        background: #2e8aea;
        color: #ffffff;
        font-size: 12px;
        text-transform: uppercase;
        display: flex;
        padding: 5px 15px;
        border: none;
        position: absolute;
        bottom: 10px;
        right: 10px;
        align-self: flex-start;
    }
    
    .my-songs-layout__button:hover {
        background: #177ce8;
    }
    

    And here is the file structure: https://gyazo.com/96d0f33a151c29a03cf0382b72611b6c

    The logs don't really say much in my eyes, it almost looks like they're talking about a different issue;

    https://gyazo.com/c6587fb535be62be0c4abd684ab68c6b

    Id also like to be able to rename the controller and the view, but every time I change them both, the view no longer works.

    Thanks!

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Oct 03, 2018 @ 08:56
    Dave Woestenborghs
    0

    Hi Kieron,

    I just looked in a customview we created based on the same tutorial.

    What I do is add a new property to the item which contains the background image path

    // Load background image
            function getBackgroundImage(item) {
                mediaResource.getById(item.picture)
                    .then(function (media) {
                        // find the image thumbnail
                        item.bgImage= mediaHelper.resolveFile(media, true);
                    });
            }
    

    And then in your view use that property to set the background image :

    <div ng-controller="My.ListView.Layout.SongsLayoutController as vm"
         ng-if="items"
         class="my-songs-layout__cards">
    
       <div
          class="my-songs-layout__card"
          ng-repeat="item in items"
          ng-class="{'-selected':item.selected}"
          ng-click="vm.selectItem(item, $event, $index)"
          ng-style="{'background-image': 'url(' + item.bgImage+ ')'}">
    
          <div class="my-songs-layout__overlay">
            <div class="my-songs-layout__released">{{ item.authorName }}</div>
            <div class="my-songs-layout__artist">{{ item.authorName }}</div>
            <div class="my-songs-layout__name">{{ item.authorName }}</div>
            <div class="my-songs-layout__genre">{{ item.authorName }}</div>
          </div>
    
          <div class="my-songs-layout__button" ng-click="vm.clickItem(item)">View song</div>
    
       </div>
    
    </div>
    

    Could you try that. I think angular is already binding the view before all images are resolved.

    Dave

  • Kieron 152 posts 390 karma points
    Oct 03, 2018 @ 09:04
    Kieron
    0

    Hi Dave, thanks for your response, its wierd, the code is being rendered as text in the HTML, if i view source for one of my cards, its this;

    <div class="my-songs-layout__card ng-scope" ng-repeat="item in items" ng-class="{'-selected':item.selected}" ng-click="vm.selectItem(item, $event, $index)" ng-style="{'background-image': 'url(' + item.bgImage + ')'}" style="background-image: url(&quot;&quot;);">
    
          <div class="my-songs-layout__overlay">
            <div class="my-songs-layout__released ng-binding">Dudes name</div>
            <div class="my-songs-layout__artist ng-binding">Dudes name</div>
            <div class="my-songs-layout__name ng-binding">Dudes name</div>
            <div class="my-songs-layout__genre ng-binding">Dudes name</div>
          </div>
    
          <div class="my-songs-layout__button" ng-click="vm.clickItem(item)">View song</div>
    
       </div>
    

    (this is after editing with your code in place) Theres no glaring red errors now, but also no dice :D

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Oct 03, 2018 @ 09:13
    Dave Woestenborghs
    0

    Hi Kieron,

    Can you inspect your network tab of your browser when opening this page. Look for a ajax request to GetById...check what is passed in what is returned.

    Dave

  • Kieron 152 posts 390 karma points
    Oct 03, 2018 @ 09:15
    Kieron
    0

    Hey, so this request is made alot:

    https://domain.co.uk/umbraco/backoffice/UmbracoApi/Media/GetById?id=umb%3A%2F%2Fmedia%2F9015727012dc4e9da582c3902e067b41
    

    And here is the Preview of the call

    )]}', {,…}
    ModelState: {}
    alias: null
    contentType: {alias: "Image", updateDate: "0001-01-01 00:00:00", createDate: "2018-08-28 11:08:44",…}
    contentTypeAlias: "Image"
    contentTypeName: "Image"
    createDate: "2018-09-03 16:52:37"
    hasPublishedVersion: false
    icon: "icon-picture"
    id: 1281
    isChildOfListView: false
    isContainer: false
    key: "90157270-12dc-4e9d-a582-c3902e067b41"
    mediaLink: "/media/1051/lucy_s_pic__1_.jpg"
    metaData: {}
    name: "lucy_s_pic__1_.jpg"
    notifications: []
    owner: {id: 1, name: "Dudes Name"}
    parentId: 1250
    path: "-1,1250,1281"
    properties: [{label: "Upload image", description: null, view: "imagecropper",…},…]
    published: false
    sortOrder: 14
    tabs: [{id: 3, active: true, label: "Image", alias: "Image",…}]
    trashed: false
    treeNodeUrl: "/umbraco/backoffice/UmbracoTrees/MediaTree/GetTreeNode/9015727012dc4e9da582c3902e067b41"
    udi: "umb://media/9015727012dc4e9da582c3902e067b41"
    updateDate: "2018-09-03 16:52:51"
    updater: null
    

    Which does look good, in terms of it coming back with the udi!

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Oct 03, 2018 @ 10:19
    Dave Woestenborghs
    0

    What does resolve file method return

    // Load background image
            function getBackgroundImage(item) {
                mediaResource.getById(item.picture)
                    .then(function (media) {
                        // find the image thumbnail
    var img = mediaHelper.resolveFile(media, true);
    console.log(img);
                        item.bgImage= img;
                    });
            }
    

    Dave

  • Kieron 152 posts 390 karma points
    Oct 03, 2018 @ 10:24
    Kieron
    0

    I'm not actually getting any output from that, though I can't even get a

    console.log("test");
    

    To output either. which is odd?

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Oct 03, 2018 @ 10:28
    Dave Woestenborghs
    0

    And if you put the console.log in the activate function ? Inside the foreach there

    Dave

  • Kieron 152 posts 390 karma points
    Oct 03, 2018 @ 10:30
    Kieron
    0

    Also nothing with this:

     angular.forEach($scope.items, function (item) {
                getBackgroundImage(item);
                console.log(img);
    
            });
    
        }
    
       // Load background image
        function getBackgroundImage(item) {
            mediaResource.getById(item.picture)
                .then(function (media) {
                    // find the image thumbnail
                var img = mediaHelper.resolveFile(media, true);
                console.log(img);
                   item.bgImage= img;
                });
        }
    

    Thanks for your help so far Dave

  • Kieron 152 posts 390 karma points
    Oct 03, 2018 @ 10:33
    Kieron
    0

    Bizarrely, ive just put everything back as i was getting a bit messy, and its now working?

    Does this stuff just cache like hell, lol. im so confused.

  • Kieron 152 posts 390 karma points
    Oct 03, 2018 @ 15:56
    Kieron
    0

    I just need to figure out why ive got 2 random empty buttons coming through now, lol

    https://gyazo.com/427c6ca0e6a65827676a2bcf26424894

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies