Search code examples
c#entity-frameworkodatainfragisticsiggrid

Loading an image into igGrid column based on ImageKey in Odata Controller


I have this Model i have set up:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace aSa.Web.Scheduling.Models
{
    public class Trailer
    {
        [Key]
        public int TrailerKey { get; set; }
        public string TrailerID { get; set; }
        public string TrailerDesc { get; set; }
        public string FabLocID { get; set; }
        public double MaxWgt { get; set; }
        public int TrailerImgKey { get; set; }

    }
}

Which I setup a Web API 2 OData Controller w/ Entity Framework for. I'm using the odata from this controller to fill in my IgniteUI igGrid. Here is my setup of this grid:

 $('#gridTrailers').igGrid({

            odata: true,
            autoGenerateColumns: false,
            autoGenerateLayouts: true,
            responseDataKey: 'value',
            dataSource: clientDS, 
            updateUrl: '/odata/Trailers',
            primaryKey: 'TrailerKey',
            width: '100%',
            height: '500',
            enableCheckBoxes: true,
            columns: [
                        { headerText: 'Trailer Key', key: 'TrailerKey', dataType: 'string', width: '100px' },
                        { headerText: 'Trailer ID', key: 'TrailerID', width: '200px' },
                        { headerText: 'Description', key: 'TrailerDesc', width: '100px' },
                        { headerText: 'FabLocID', key: 'FabLocID', width: '100px' },
                        { headerText: 'Max Weight', key: 'MaxWgt', width: '100px' },
                        { headerText: 'Trailer', key: 'TrailerImgKey', width: '100px' },

            ],
            features:
                [
                    {
                        name: 'Sorting',
                        type: 'local'
                    },                
                    {
                        advancedModeEditorsVisible: true,
                        name: 'Filtering',
                        caseSensitive: false,
                        type: 'local', //I think this can be local now
                        //mode: 'advanced',
                        FilterExprUrlKey: 'filter',
                        columnSettings: [
                            { columnKey: 'TrailerKey', allowFiltering: false },
                            { columnKey: 'TrailerImgKey', allowFiltering: false }
                        ]

                    },
                    {
                        name: 'ColumnMoving'
                    },
                    {
                        mouseDragSelect: false,
                        multipleSelection: true,
                        touchDragSelect: false,
                        name: 'Selection',

                    },
                    {
                        editMode: 'cell',
                        startEditTriggers: 'dblclick',
                        name: 'Updating',
                        enableAddRow: true,
                        enableDeleteRow: true
                    },

                    {
                        name: 'Resizing'
                    },
                    {
                        name: 'Hiding'
                    }


                ],

        });

Currently i'm using an old db that has a TrailerImageKey that relates to a specific picture that would have been loaded in from another db in the legacy version of this application. I now have a folder in my project called "TrailerImages". I am currently trying to find a way to set up the last column of my grid to display a specific trailer image based on the TrailerImageKey.

I know i'm setting up the column here:

columns: [
                 { headerText: 'Trailer Key', key: 'TrailerKey', dataType: 'string', width: '100px' },
                 { headerText: 'Trailer ID', key: 'TrailerID', width: '200px' },
                 { headerText: 'Description', key: 'TrailerDesc', width: '100px' },
                 { headerText: 'FabLocID', key: 'FabLocID', width: '100px' },
                 { headerText: 'Max Weight', key: 'MaxWgt', width: '100px' },
                 { headerText: 'Trailer', key: 'TrailerImgKey', width: '100px' },

         ],

I was wondering if it was possible to do something like:

{ headerText: 'Trailer', key: 'TrailerImgKey', width: '100px', dataType: "object",
  template: "<img width='100' height='90' src={{>val}}></img>", formatter:           function(val){
  switch(TrailerImageKey) {
     case 1:
         val = 'C:\Source_Code\Web\MyApp\TrailerImages\RedTrailer.png'
         break;
     case 2:
          val = 'C:\Source_Code\Web\MyApp\TrailerImages\RedTrailer.png'
          break;
     default:
          val = ''
   }
 }
},

I feel like I am close to the right idea, but am fairly unfamiliar with IgniteUI and infragistics and am having trouble getting it to work. If anyone has suggestions on how I can load the images into my igGrid based on the TrailerImageKey in my Odata I would appreciate it.


Solution

  • I figgured it out:

    { headerText: 'Trailer', key: 'TrailerImgKey', width: '100px', dataType: 'object', template: "<img width='150' height='90' src='${TrailerImgKey}' />", formatter: LoadImages}  
    

    LoadImages function:

    function LoadImages(val){
        switch (val) {
            case 1: val = '/Images/TrailerImages/FlatbedBlueEmpty.gif'; break;
            case 2: val = '/Images/TrailerImages/FlatBedGreenEmpty.jpg'; break;
            case 3: val = '/Images/TrailerImages/FlatBedPurpleEmpty.jpg'; break;
            case 4: val = '/Images/TrailerImages/PickupBlueEmpty.jpg'; break;
            case 5: val = '/Images/TrailerImages/PickupGreenEmpty.jpg'; break;
            case 6: val = '/Images/TrailerImages/PickupRedEmpty.jpg'; break;
            case 7: val = '/Images/TrailerImages/SmallFlatbedBlueEmpty.jpg'; break;
            case 8: val = '/Images/TrailerImages/SmallFlatbedGreenEmpty.jpg'; break;
            case 9: val = '/Images/TrailerImages/SmallFlatbedPurpleEmpty.jpg'; break;
            default: val = '';
        }
        return val;
    }