Editor example Parent child editor

This example demonstrates Editor being used in a parent / child manner to edit groups of related items. The code and methodology is fully documented in this blog post. A similar post is also available showing parent / child editing in a child row.

The Javascript shown below is used to initialise the table shown in this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
$(document).ready(function() {
    var siteEditor = new $.fn.dataTable.Editor( {
        ajax: "../../controllers/sites.php",
        table: "#sites",
        fields: [ {
                label: "Site name:",
                name: "name"
            }
        ]
    } );
    window.editor = siteEditor; // for demo only!
 
    var siteTable = $('#sites').DataTable( {
        dom: "Bfrtip",
        ajax: "../../controllers/sites.php",
        columns: [
            { data: 'name' },
            { data: 'users', render: function ( data ) {
                return data.length;
            } }
        ],
        select: {
            style: 'single'
        },
        buttons: [
            { extend: "create", editor: siteEditor },
            { extend: "edit",   editor: siteEditor },
            { extend: "remove", editor: siteEditor }
        ]
    } );
 
 
    var usersEditor = new $.fn.dataTable.Editor( {
        ajax: {
            url: '../../controllers/users.php',
            data: function ( d ) {
                var selected = siteTable.row( { selected: true } );
                if ( selected.any() ) {
                    d.site = selected.data().id;
                }
            }
        },
        table: '#users',
        fields: [ {
                label: "First name:",
                name: "users.first_name"
            }, {
                label: "Last name:",
                name: "users.last_name"
            }, {
                label: "Phone #:",
                name: "users.phone"
            }, {
                label: "Site:",
                name: "users.site",
                type: "select",
                placeholder: "Select a location"
            }
        ]
    } );
 
    var usersTable = $('#users').DataTable( {
        dom: 'Bfrtip',
        ajax: {
            url: '../../controllers/users.php',
            type: 'post',
            data: function ( d ) {
                var selected = siteTable.row( { selected: true } );
                if ( selected.any() ) {
                    d.site = selected.data().id;
                }
            }
        },
        columns: [
            { data: 'users.first_name' },
            { data: 'users.last_name' },
            { data: 'users.phone' },
            { data: 'sites.name' }
        ],
        select: true,
        buttons: [
            { extend: 'create', editor: usersEditor, enabled: false, init: function(dt) {
                var that = this;
                siteTable.on('select deselect', function() {
                    that.enable(siteTable.rows({selected: true}).any())
                })
            }},
            { extend: 'edit',   editor: usersEditor },
            { extend: 'remove', editor: usersEditor }
        ]
    } );
     
 
    siteTable.on( 'select', function (e) {
        usersTable.ajax.reload();
 
        usersEditor
            .field( 'users.site' )
            .def( siteTable.row( { selected: true } ).data().id );
    } );
 
    siteTable.on( 'deselect', function () {
        usersTable.ajax.reload();
    } );
     
    usersEditor.on( 'submitSuccess', function () {
        siteTable.ajax.reload();
    } );
 
    siteEditor.on( 'submitSuccess', function () {
        usersTable.ajax.reload();
    } );
} );

In addition to the above code, the following Javascript library files are loaded for use in this example:

Editor submits and retrieves information by Ajax requests. The two blocks below show the data that Editor submits and receives, to and from the server. This is updated live as you interact with Editor so you can see what is submitted.

Submitted data:

The following shows the data that has been submitted to the server when a request is made to add, edit or delete data from the table.

// No data yet submitted

Server response:

The following shows the data that has been returned by the server in response to the data submitted on the left and is then acted upon.

// No data yet received