where to intercept and tweak data shown in table (i.e. combine several fields into one, etc)

where to intercept and tweak data shown in table (i.e. combine several fields into one, etc)

ironandsteelironandsteel Posts: 17Questions: 4Answers: 0
edited October 2016 in Editor

I'm using datatables and editor and I've generated a starting point for my app using the Generator tool. It works fine.

My database has a contact1 and contact2 field. In the table, I want to write a bit of code that combines the contact1 and contact2 fields into a single field (one per line) and display that in the table. I may want to do other manipulations of what is shown- putting things in spans for formatting, etc. When I edit the record, I want to edit contact1 and contact2 in their own fields.

In my example, the table is "cont" (for contractor).

In js/table.cont.js, I see where I can set up whatever database fields I want to appear in the edit form. And I see where I can set up what fields appear in the table. Where can I intercept the record coming from the db, combine a couple of fields and pass along to maybe a new field name?

Thanks for a great tool, and for any info you can send my way.

    var editor = new $.fn.dataTable.Editor( {
        ajax: 'php/table.cont.php',
        table: '#cont',
        fields: [
            {
                "label": "name:",
                "name": "name"
            },
            {
                "label": "addr:",
                "name": "addr"
            },
            {
                "label": "city:",
                "name": "city"
            },
            {
                "label": "state:",
                "name": "state"
            },
            {
                "label": "zip:",
                "name": "zip"
            },
            {
                "label": "contact1:",
                "name": "contact1"
            },
            {
                "label": "csi1_contact1:",
                "name": "csi1_contact1"
            },          
            

            {
                "label": "contact2:",
                "name": "contact2"
            },
            {
                "label": "phone:",
                "name": "phone"
            },
            {
                "label": "email1:",
                "name": "email1"
            }
        ]
    } );

    var table = $('#cont').DataTable( {
        dom: 'Bfrtip',
        ajax: 'php/table.cont.php',
        "paging": false,
        columns: [
            {
                "data": "name"
            },
            {
                "data": "addr"
            },
            {
                "data": "city"
            },
            {
                "data": "state"
            },
            {
                "data": "zip"
            },
            {
                "data": "contact1"
            },
            {
                "data": "contact2"
            },
            {
                "data": "phone"
            },
            {
                "data": "email1"
            }
        ],
        select: true,
        lengthChange: false,
        buttons: [
            { extend: 'create', editor: editor },
            { extend: 'edit',   editor: editor },
            { extend: 'remove', editor: editor }
        ]
    } );
} );


This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Answer ✓

    When I edit the record, I want to edit contact1 and contact2 in their own fields.

    This is key. If you want to simply combine them in the table view, use a renderer.

    I use one for exactly this purpose in this example to combine the first and last name in the table, but keep them individually editable.

    Allan

This discussion has been closed.