基于Odata Controller中的ImageKey将图像加载到igGrid列中

时间:2014-07-30 14:33:19

标签: c# entity-framework odata infragistics iggrid

我已经设置了这个模型:

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; }

    }
}

我为其设置了一个Web API 2 OData Controller w / Entity Framework。我正在使用此控制器中的odata来填充我的IgniteUI igGrid。这是我对此网格的设置:

 $('#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'
                    }


                ],

        });

目前我正在使用一个旧的数据库,它有一个TrailerImageKey,它与本应用程序的旧版本中另一个数据库中加载的特定图片有关。我现在在我的项目中有一个名为“TrailerImages”的文件夹。我目前正在尝试找到一种方法来设置网格的最后一列,以显示基于TrailerImageKey的特定预告片图像。

我知道我在这里设置专栏:

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' },

         ],

我想知道是否可以做类似的事情:

{ 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 = ''
   }
 }
},

我觉得我接近正确的想法,但我对IgniteUI和infragistics非常不熟悉,并且无法让它工作。如果有人建议我如何根据我的Odata中的TrailerImageKey将图像加载到我的igGrid中,我将不胜感激。

1 个答案:

答案 0 :(得分:4)

我把它弄清楚了:

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

LoadImages功能:

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;
}