Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatables - Merge columns together

I have these database columns, but I want them to be in one column. How would I do that? With mRender, I think?

                    /* Address */  
        {"sTitle": "Address",
                    "bVisible": true,
                    "bSearchable": true},
        /* City */   
        {"sTitle": "City",
                    "bVisible": true,
                    "bSearchable": true},
        /* State */    
        {"sTitle": "State",
                    "bVisible": true,
                    "bSearchable": true},
        /* Zip */    
        {"sTitle": "Zip",
                    "bVisible": true,
                    "bSearchable": true},
like image 494
ChaseC Avatar asked Nov 04 '13 21:11

ChaseC


2 Answers

provided that the columns returned by the datatables get are address, city , state, zip 1-4

if your data returned is a regular array

   { "mData": 0 , //or address field
     "mRender" : function ( data, type, full ) { 
     //data = mData
     //full is the full array address= [0] city = [1] state=[2] zip=[3] 
        return data+', '+full[1]+', '+full[2]+', '+full[3];}
      },

if your data is an associate array

   { "mData": 'address' , 
     "mRender" : function ( data, type, full ) { 
        return data+', '+full['city']+', '+full['state']+', '+full['zip'];}
      },

or you can call mRender independent of mData (though it seems not needed for this situation)

   { "mData": null , 
     "mRender" : function ( data, type, full ) { 
        return full['address']+', '+full['city']+', '+full['state']+', '+full['zip'];}
      },

EDIT: for datatables 1.10, just change the names a bit, drop the "m"

   { "data": null , 
     "render" : function ( data, type, full ) { 
        return full['address']+', '+full['city']+', '+full['state']+', '+full['zip'];}
      },

*note i'm not taking into account whether you should store this data in one column, just showing how its done

like image 124
Jay Rizzi Avatar answered Oct 22 '22 13:10

Jay Rizzi


database table fields : id,first_name,last_name,email

datatable : full_name,email (without id) ?

"columns": [
            {
             "mData": null ,
             "mRender" : function ( data, type, full ) {
                return full['first_name']+' '+full['last_name'];
              }
             },
like image 21
Bekir Bozkurt Avatar answered Oct 22 '22 12:10

Bekir Bozkurt