Search code examples
javascriptdojodgriddojo.gridx

Dgrid formatter create column based on values in another column


I am creating a dgrid based on a data source which has an address and two columns with Boolean values which identifies what type of address is in the record, either postal address or business address.

I would like to display a descriptive name for the Boolean values in another column (Address Type) based on the two Boolean columns. I am trying to use the dgrid formatter function however i am unable to get the desired results. Under is my code:

Javascript

function getAddressType(){
    $.each(employeeAddressData, function(key, value){

       if(key == "postalAddress"){
          if(value == true || value == 'true'){
              console.log('returning postal');
          return 'Postal';
          }

      }else{
          console.log('returning business');
          return 'Business';
      }

      });  
    }

var Employer = [{id: 1, businessName:'John Doe and Sons Limited',phone:'123456',address:'123 Long Street', postalAddress:true, businessAddress:false},
                {id: 1, businessName:'Alice and Bob Limited',phone:'78956', address:'56 Short Street', postalAddress:false, businessAddress:true}];

var employerGrid = new CustomGrid({
    store: employerStore,
    columns:{
    id:{
        label:"Id",
        field:"id",
        hidden:true
    },
    businessName:{
        label:"Business",
        field:"businessName",
    },
    phone:{
        label:"Contact No.",
        field:"phone",
    },
    address:{
        label:"Address",
        field:"address",
    },
    addressType:{
        label:"Address Type",
        formatter:getAddressType();
    }   
    },
    selectionMode:"none",
    loadingMessage: "Loading data...",
    noDataMessage: "No results found....",
    allowSelectAll: true

}, "employerGrid");

Solution

  • I realized dgrid has a get function which i can use to access the datasource attributes. So formatter was not needed instead get was used. Under is my solution :

    Javascript

     },
        addressType:{
            label:"Address Type",
            get: function(object){
              if(object.postalAddress == true){
                  return 'Postal';
               }else{
                  return 'Business';
               }
            }
        }