How do I show combined data as a field in the Editor form?

How do I show combined data as a field in the Editor form?

pansengtatpansengtat Posts: 66Questions: 26Answers: 1

Hi,

I read from the examples used in Editor that you can actually show combined data in the main table, but the examples shown display separate fields per column in the Editor form.

Is there a way to display combined data, say, for example, summation of tableName.numericColumn1 + tableName.numericColumn2 as a single field in that Editor form? The only other condition I like to add is that I won't save the data into the database after Editor form submission, as I intend to only display it in the form.

eg.)
Currently in the main table:

var table = $('#tableID').DataTable({
                dom: "Tfrtip",
                ajax: "php/myEditor.php",
                columns: [
// Lots of stuff here
                    { data:  null, render: function (data, type, row) {
                        var total = parseFloat(data.tableName.numericColumn1) + parseFloat(data.tableName.numericColumn2);
                        return total.toFixed(2);
                    } },
// Even more things here

In the Editor instance (my current attempt won't work):

var editor = new $.fn.dataTable.Editor({
                ajax: "php/myEditor.php",
                table: "#tableID",
                fields: [
                    {
                        label:      "Total Sum: ",
                        name:       "tableName.numericColumn1 + tableName.numericColumn2"
                    },
// More stuff

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Is there a way to display combined data

    Yes, the fields.data option can be used as a function in Editor in the same way that columns.data can in DataTables. However, please note that you must consider the set data type - i.e. the fields.data option is both a getter and setter and you must program both in your function.

    Generally I would recommend against combining two fields - assuming that they are two fields being stored in the server, since you would need to separate the data. But it is possible with a callback function.

    Allan

This discussion has been closed.