0

Happy Evening All,

I am trying to add formatting to the data that was collected in MySql and display with its corresponding array value.

MySql
name, age, gender (1=M, 0=F)
Max, 18, 1


Current DataTable Display
Max, 18, 1


DataTable (desired version)
Max, 18, M


How to add array to datatable serverside php. Also tried using Formatter - unable to get the desired display. Any help would be greatful.

something tried at my end does't work.

array( 'db' => 'gender', $arrayName('dt') => 8 ),

Thanks,

2 Answers 2

1

you can define each column :

$('#table').DataTable({    
"columns": [
{
    "title": "gender.", 
    "data": "gender", 
    "render": function (data, type, row) {
                 return data ? "M" : "F";
                 }
}
]
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Sylvain, tried with the above code - no luck - I am using ajax - server side to populate MySql data.
Following is code used - but it result with only male for both '0' and '1'. ` "title": "gender.", "data": "gender", render: function (data, type, row) { return data ? "Male" : "Female"; }}, `
can you console.log() your values ? data ? "M" : "F"; is just a short syntax for if else
0

Complete Working solution :)

<script type="text/javascript">
$(document).ready(function() {
var genderValue = [
"Female", //0
"Male", // 1 
];
$('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "scripts/server_processing.php",
        "columns": [
                { data : gender, 
                render: function (data, type, row) {
                return genderValue[data]
                }},
                    ]
        } );
} );
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.