6

我有这些数据库列,但我希望它们在一列中。我该怎么做?使用 mRender,我想?

                    /* 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},
4

3 回答 3

14

前提是数据表返回的列是地址、城市、州、邮编 1-4

如果您返回的数据是常规数组

   { "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];}
      },

如果您的数据是关联数组

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

或者您可以独立于 mData 调用 mRender(尽管这种情况似乎不需要)

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

编辑:对于数据表 1.10,只需稍微更改名称,删除“m”

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

*注意我没有考虑你是否应该将这些数据存储在一个列中,只是显示它是如何完成的

于 2013-11-07T14:15:46.667 回答
3

数据库表字段:id、first_name、last_name、email

数据表:全名,电子邮件(无 id)?

"columns": [
            {
             "mData": null ,
             "mRender" : function ( data, type, full ) {
                return full['first_name']+' '+full['last_name'];
              }
             },
于 2017-05-12T07:55:51.050 回答
0
$(document).ready(function() {
    $('#example').DataTable( {
        "columnDefs": [
            {
                // The `data` parameter refers to the data for the cell (defined by the
                // `data` option, which defaults to the column being worked with, in
                // this case `data: 0`.
                "render": function ( data, type, row ) {
                    return data +' ('+ row[3]+')';
                },
                "targets": 0
            },
            { "visible": false,  "targets": [ 3 ] }
        ]
    } );
} );
于 2018-09-17T10:23:57.803 回答