Standalone editor won't call ssp

Standalone editor won't call ssp

Stacey1134Stacey1134 Posts: 118Questions: 22Answers: 0
edited August 6 in DataTables 2

I have a need to edit the data on a table not shown, and for some reason, I cannot get editor to call the ssp script.

    let selectedFacilityID = null;
    const facilityEditor = new $.fn.dataTable.Editor( {
        ajax: {
            url: baseUrl + "modules/database/ssp_facilities.php",
            type: "POST",
            error: function (xhr, error, thrown) {
                console.error('AJAX Error:', error, thrown);
                console.error('XHR:', xhr);
            }
        },
        idSrc: "Facility_ID",
        template: '#facForm',      
        fields: [ {
                name:  "Facility_Type",
                type:  "select",                  
                className: 'full block'                    
            }, {   
            ...
            }

        ]
    } );
    $('#printerTable tbody').on('click', 'tr', function () {
        var data = ( table.row( this ).data() );
        // The facility ID should come from PrisonFacilityListing, not form.FAC_ID
        selectedFacilityID = data.PrisonFacilityListing.Facility_ID;
        ...
        })
    }
    $('#editFacilityBtn').on('click', function () {
        if (!selectedFacilityID) {
            alert('No facility selected. Please select a row in the table first.');
            return;
        }
        
        console.log('Selected Facility ID:', selectedFacilityID);
        facilityEditor
            .edit(selectedFacilityID)
            .title('Quick Edit Facility')
            .buttons([
                {
                    label: 'Save',
                    className: 'btn-sm lnk-modal-button',
                    fn: function () {
                        this.submit();
                    }
                }, {
                    label: 'Cancel',
                    className: 'btn-sm lnk-modal-button',
                    fn: function () {
                        this.close();
                    }
                }
            ])
            .open();
    });

Standalone editor opens fine, template is fine, but no data, when watching the network calls, it never calls ssp_facilities.php
I can do this by hand, but standalone should be able to do this. What do I need to do to initiate the ajax call?

To be clear, selectFacilityID does contain the id, and it is a correct ID as it is visibly showing a proper left join in a different table, the one selected above.

Answers

  • allanallan Posts: 64,889Questions: 1Answers: 10,745 Site admin

    Are you able to give me a link to the page showing the issue please? I'm not immediately seeing what would cause it Ajax URL not to be called there.

    Thanks,
    Allan

  • Stacey1134Stacey1134 Posts: 118Questions: 22Answers: 0

    Yup, I'll send it to you

  • Stacey1134Stacey1134 Posts: 118Questions: 22Answers: 0
    edited August 8

    Thanks to Allan for the nudge—his comment about needing to use the API manually got me thinking differently about how to handle the facility editor.

    What I had before wasn’t working: the standalone Editor wasn’t triggering an AJAX call to ssp_facilities.php, even though the config followed the docs. No errors, just silence. Turns out, standalone editors don’t automatically load data when calling .edit(id).open(), even though the documentation implies they should.

    Here’s what works now:

    • On button click, I grab the selected row’s Facility_ID
    • I manually load the data using an AJAX call when the editor opens (.one('open'))
    • Only the targeted facility gets pulled from the backend, not the entire dataset
    • I map fields safely to avoid errors, and use the row data as a fallback
    • Special handling is in place for the Quill editor field (Mailing_Notes)
    • The Save/Cancel buttons behave cleanly and the modal looks how it should

    This fixed the issue and gave me a much faster, more reliable setup. So if you’re using a standalone Editor and it’s not loading data like you expect, this approach might save you some serious time.

        $('#editFacilityBtn').on('click', function () {
            // Get the currently selected row
            var selectedRow = table.row('.selected');
            if (selectedRow.length === 0) {
                alert('No facility selected. Please select a row in the table first.');
                return;
            }
            
            var rowData = selectedRow.data();
            var facilityID = rowData.PrisonFacilityListing.Facility_ID;
            
            // console.log('Opening facility editor for ID:', facilityID);
            
            facilityEditor
                .edit(facilityID)
                .title('Quick Edit Facility')
                .buttons([
                    {
                        label: 'Save',
                        className: 'btn-sm lnk-modal-button',
                        fn: function () {
                            this.submit();
                        }
                    }, {
                        label: 'Cancel',
                        className: 'btn-sm lnk-modal-button',
                        fn: function () {
                            this.close();
                        }
                    }
                ])
                .one('open', function() {
                    // Load specific facility data from ssp_facilities.php
                    console.log('Loading facility data for ID:', facilityID);
                    
                    var requestData = {
                        'action': 'edit'
                    };
                    requestData['data[' + facilityID + '][Facility_ID]'] = facilityID;
                    
                    $.ajax({
                        url: baseUrl + "modules/database/ssp_facilities.php",
                        type: 'POST',
                        dataType: 'json',
                        data: requestData,
                        success: function(response) {
                            // console.log('Facility edit response:', response);
                            // console.log('Response data structure:', response.data);
                            
                            // The edit response contains an array with one facility
                            var facilityData = null;
                            if (response.data && response.data.length > 0) {
                                // Check if it's an array with the facility data
                                facilityData = response.data[0];
                                console.log('Found facility data in array:', facilityData);
                            } else if (response.data && response.data[facilityID]) {
                                // Check if it's an object with facilityID as key
                                facilityData = response.data[facilityID];
                                console.log('Found facility data by ID:', facilityData);
                            }
                            
                            if (facilityData) {
                                
                                // Define which fields we want to populate (only those that exist in the editor)
                                var editorFields = [
                                    'Facility_ID', 'Facility_Type', 'Prison_Name', 'Website', 'Phone_Number', 'DOC_ACR',
                                    'Address_1', 'Address_2', 'Address_3', 'City', 'State', 'Zipcode',
                                    'Mailing_Notes', 'Mail_Addr_1', 'Mail_Addr_2', 'Mail_Addr_3', 
                                    'Mail_City', 'Mail_State', 'Mail_Zip', 'Pkg_Addr_1', 'Pkg_Addr_2', 
                                    'Pkg_Addr_3', 'Pkg_City', 'Pkg_State', 'Pkg_Zip', 'Alt_Addr_1', 
                                    'Alt_Addr_2', 'Alt_Addr_3', 'Alt_City', 'Alt_State', 'Alt_Zip',
                                    'eOnly', 'eProvider', 'correspondence', 'publications', 'books',
                                    'timestamp', 'EditedBy'
                                ];
                                
                                // Populate only the fields that exist in both the data and the editor
                                editorFields.forEach(function(fieldName) {
                                    if (facilityData.hasOwnProperty(fieldName) && facilityEditor.field(fieldName)) {
                                        console.log('Setting field:', fieldName, '=', facilityData[fieldName]);
                                        facilityEditor.field(fieldName).val(facilityData[fieldName]);
                                    } else if (!facilityEditor.field(fieldName)) {
                                        console.warn('Field not found in editor:', fieldName);
                                    }
                                });
                                
                                // Handle Quill editor separately for Mailing_Notes
                                if (facilityData.Mailing_Notes && facilityEditor.field('Mailing_Notes')) {
                                    setTimeout(function () {
                                        facilityEditor.field('Mailing_Notes').quill().clipboard.dangerouslyPasteHTML(facilityData.Mailing_Notes);
                                    }, 100);
                                }
                            } else {
                                console.warn('No facility data found in edit response');
                                console.log('Response structure debug:', response);
                                // Fallback to row data
                                var facilityData = rowData.PrisonFacilityListing;
                                for (var fieldName in facilityData) {
                                    if (facilityEditor.field(fieldName) && facilityData[fieldName] !== undefined) {
                                        facilityEditor.field(fieldName).val(facilityData[fieldName]);
                                    }
                                }
                            }
                        },
                        error: function(xhr, error, thrown) {
                            console.error('Error loading facility data:', error, thrown);
                            console.error('Response text:', xhr.responseText);
                            
                            // Fallback to row data
                            var facilityData = rowData.PrisonFacilityListing;
                            for (var fieldName in facilityData) {
                                if (facilityEditor.field(fieldName) && facilityData[fieldName] !== undefined) {
                                    facilityEditor.field(fieldName).val(facilityData[fieldName]);
                                }
                            }
                        }
                    });
                })
                .open();
        });
    
Sign In or Register to comment.