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

本文关键字:图像 加载 igGrid ImageKey Odata Controller | 更新日期: 2023-09-27 18:15:33

我设置了这个模型:

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.0数据控制器w/实体框架。我使用这个控制器的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相当不熟悉,并且很难让它工作。如果有人有关于如何我可以加载图像到我的igGrid基于我的Odata中的TrailerImageKey的建议,我会很感激。

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

我明白了:

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