Copied to clipboard

Flag this post as spam?

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


  • Alex Brown 129 posts 620 karma points
    Oct 27, 2016 @ 13:38
    Alex Brown
    0

    Render Grid in Custom Section

    Hi

    I've been having trouble rendering a custom grid data type, which is within a custom section, using Angular in the backoffice.

    I've managed to use the following to get the id of the data type in a controller:

    ApplicationContext.Services.DataTypeService.GetDataTypeDefinitionByName
    

    Then injected the dataTypeResource module and used its "getById" method to return an object containing data for this data type. However, the object returned not what I'm looking for, as when I call the grid it falls over in umbraco.controller.js on the following line:

    if ( ($scope.model.config.items.config && $scope.model.config.items.config.length > 0) || ($scope.model.config.items.styles && $scope.model.config.items.styles.length > 0)) {
    

    This is due to the object/model not containing "items".

    Below is the search, and setting the scope object:

        productResource.getSpecialProductGridId()
            .then(function (result) {
                if (result) {
                    dataTypeResource.getById(result.data).then(function (result) {
                        $scope.myEditor.config = result.preValues[0];
                    });
                }
            });
    
        $scope.myEditor = {
            view: 'grid',
            config: {}
        }
    

    I've got a feeling I'm doing something very wrong here.

    I'd really appreciate it if anyone could guide me in the right direction.

    Thanks.

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Oct 28, 2016 @ 06:35
    David Brendel
    101

    Hi Alex,

    I'm doing it like this:

    First I created a new Api Controller responsible for getting me the property editor data:

    [PluginController(Constants.Routing.PluginName)]
    public class DataTypeController : UmbracoAuthorizedApiController
    {
        /// <summary>
        /// The get by name.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="DataTypeDisplay"/>.
        /// </returns>       
        public object GetByName(string name)
        {
            return GetDataType(name);
            //var all = DataTypeCacheProvider.Current.GetOrExecute(() => this.Services.DataTypeService.GetAllDataTypeDefinitions().ToList());
            //var dataType = all.FirstOrDefault(x => x.Name == name);
            //return this.FormatDataType(dataType);
        }
        [HttpGet]
        public void Clear()
        {
            DataTypeCacheProvider.Current.Clear();
        }
        internal object GetDataType(string name)
        {
            var dts = Services.DataTypeService.GetAllDataTypeDefinitions().ToList();
            var found = dts.FirstOrDefault(x => x.Name == name);
            if(found == null)
            {
                throw new System.Exception("Datatype " + name + "not found");
            }
            var formatted = FormatDataType(found);
            if(formatted == null)
            {
                throw new System.Exception("Datatype " + name + "not found");
            }
            return formatted;
        }
        /// <summary>
        /// The format data type.
        /// </summary>
        /// <param name="dtd">
        /// The dtd.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        /// <exception cref="HttpResponseException">
        /// </exception>
        protected object FormatDataType(IDataTypeDefinition dtd)
        {
            if (dtd == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            var dataTypeDisplay = Mapper.Map<IDataTypeDefinition, DataTypeDisplay>(dtd);
            var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias);
            var configDictionairy = new Dictionary<string, object>();
            foreach (var pv in dataTypeDisplay.PreValues)
            {
                configDictionairy.Add(pv.Key, pv.Value);
            }
            return new
            {
                guid = dtd.Key,
                propertyEditorAlias = dtd.PropertyEditorAlias,
                config = configDictionairy,
                view = propEditor.ValueEditor.View
            };
        }
    }
    

    As you can see it fetches the data based on a name. Think you can change that for using an id instead.

    Then I created a resource witch fetches the configuration from the api controller:

    angular.module('umbraco.resources').factory('dataTypeResource', function ($http) {
    return {
        getByName: function (name) {
            return $http.get(dataTypeControllerBaseUrl  + 'GetByName?name=' + name);
        },
        clear: function (name) {
            return $http.get(dataTypeControllerBaseUrl + 'Clear');
        }
    }
    

    });

    The view renders the editor like this:

    <div class="" style="" id="{{editor.label}}" ng-repeat="editor in gridEditors">
            <umb-property property="editor">
                <umb-editor model="editor"></umb-editor>
            </umb-property>
        </div>
    

    The controller builds up the stuff for the view like:

    dataTypeResource.getByName("GridEditorName").then(function (response) {
                    UpdateGridEditors(response.data);                  
                });
    
    function UpdateGridEditors(gridConfig) {
                    $scope.gridConfig = gridConfig;
    $scope.gridEditors = [];
    var editor = {
                                view: $scope.gridConfig.view,
                                config: $scope.gridConfig.config,
                                label: language.IsoCode,
                                hideLabel: true,
                                value: null
                            };
    $scope.gridEditors.push(editor);
    

    The UpdateGridEditors function is not comlpete as I'm doing some fetching of saved content and so on there. But thats not relevant for getting the idea.

    Hope that helps.

    Regards David

  • Alex Brown 129 posts 620 karma points
    Oct 28, 2016 @ 08:21
    Alex Brown
    0

    Hi David

    Pretty much what I'm looking for, thanks for the help!

Please Sign in or register to post replies

Write your reply to:

Draft