2014-01-29 52 views
0

我正在使用ng-grid並希望根據表中的位標誌顯示「活動/非活動」而不是0/1。將位標誌格式化爲ng格形式的文本值

編輯補充:

網格有像EmployeeName,EmployeeRole等status列列,我想要顯示的狀態爲有效/無效。這個值在表中是0/1。我應該如何轉換數據,我應該使用單元格模板嗎?

`$ scope.cols = [{ 字段: 'EmployeeName',顯示名: '名稱',},{ 字段: '角色',顯示名: '角色'},
{字段: '狀態' ,displayName:'Status'} ];'

感謝您的幫助!

+0

那很簡短... –

+0

是的,那很簡短,我想最好的方法是將另一個屬性添加到您的對象,根據位特性返回所需的值,或者準備一個函數來執行此操作。 請給我們一些代碼。 – Fedaykin

回答

0

您可以爲該字段使用自定義單元格模板並使用$scope函數表示該值。

plunker example

我已經使用了plunker從ngGrid的網站從,因爲它有cellTemplate已經成立建。而不是ageOver30IsActive,你可以將其改爲可能類似於getStatusString的東西,它會根據狀態位返回活動或非活動狀態。

0

我更喜歡在這種情況下使用cellFilter。使用cellTemplate不必要地使事情複雜化。您可以創建一個可在整個應用程序中重複使用的自定義過濾器,然後將其應用於cellFilter。這樣,您可以在任何需要以可讀格式顯示值的其他頁面上使用相同的過濾器。

See plunker here

0

在這種情況下,我們可以創建自定義過濾器,並將它注入到主模塊。此過濾器可以從其他應用程序的地方重新使用

首先創建一個過濾器模塊

angular.module('tmb.filter', []) //tmb is the main module 
    .filter('statusFilter', function() { 

     return function (type) { 
      return type ? 'Active' : 'In-Active'; 
     }; 
    }); 

然後注入到主模塊

var app = angular.module("tmb", ['ngRoute', 'ngGrid', 'tmb.services', 'tmb.filter']); 

現在用它來NG-網格列

$scope.ConfigItemGrid = { 
      data: 'ConfigItemList', 
      primaryKey: 'ItemID', 
      multiSelect: false, 
      selectedItems: $scope.selectedUsers, 
      enableColumnResize: false, 
      showFooter: true, 
      footerRowHeight: 35, 
      enableCellEdit: false, 
      columnDefs: [ 
       { field: 'ItemDescription', displayName: 'Item Description', width: '25%', sortable: false }, 
       { field: 'IsPO', displayName: 'Is PO', width: '15%', sortable: false }, 
       { field: 'Status', displayName: 'Po Status', width: '15%', sortable: false, cellFilter: 'statusFilter' }, 
       { field: 'ItemType', displayName: 'Item Type', width: '25%', sortable: false }, 
       { filed: '', cellTemplate: '<button ng-click="edit(row, primaryKey)">Edit</button> <button ng-click="delete(row)">Delete</button>', width: '15%' } 
      ] 
     }; 

enter image description here