Using different ajax sources for table and editor

Using different ajax sources for table and editor

brendonsbrendons Posts: 39Questions: 14Answers: 1

Forgive me if this is a stupid question.
I am using PHP backend. To achieve a fast load time for my table, I created a "little" query that selects six fields to be shown in the datatable. I assigned the table datasource to this little query (Many rows, six fields in production).
For the editor I created a "big" query that selects all of my fields. (One row, approx. 60 fields in production).
The following code is cut down to demonstrate.

    $(document).ready(function () {
                        
                var appEditor = new $.fn.dataTable.Editor({
                    ajax: "php/bigQuery.php", //(in prod there are 40 detail fields)
                    table: "#enr_apps",
                    fields: [
                        {
                            label: "one",
                            name: "one"
                        },  {
                            label: "two:",
                            name: "two"
                        }, {
                            label: "three:",
                            name: "three"
                        }, {
                            label: "four:",
                            name: "four"
                        }                        
                    ]
                });
                
            var table = $('#enr_apps').DataTable({
                    dom: "Bfprtlip",
                   "processing": true,
                    ajax: "php/littleQuery.php”, //(in prod there are six summary fields)
                   columns: [
                        {data: “one”},
                        {data: "two"}
                    ],
                    select: {
                        style: 'os',
                        selector: 'td'
                    },
                    buttons: [
                    {extend: "create", editor: appEditor},
                    {extend: "edit", editor: appEditor},
                    {extend: "remove", editor: appEditor}
                    ]
                });
   });

As expected the table shows six columns and when I select a row and click the edit button the editor opens up with the selected row ready to edit.
But in the editor, only fields one and two are populated. Fields three and four in the editor never get populated.
If I add fields three and four to the "little" query, then they show in the editor.
It seems that the editor is relying on the datasource of the table.
So my potentially stupid question is: Why specify an ajax datasource for the editor when the linked "table" property is set?

Obviously my understanding of how Editor works is incomplete - I thought the selected row of the table would pass it's RowID to the editor which would then run the big query and return the complete row.

I'm not asking how to fix this, I know how to build a stand alone editor or add child rows etc. to get what I want to achieve.
It just would have been a nice feature if it worked that way (if, instead of the "table" property link I could specify the RowID as the link).

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,822Questions: 1Answers: 10,127 Site admin
    edited December 2016 Answer ✓

    Why specify an ajax datasource for the editor when the linked "table" property is set?

    Editor's ajax option is telling it where to send the data on submit. That can be to a different source from the DataTable (if using a REST API for example). And Editor will read back information from that source once submitted, but it will not read extra data from that source upon edit(1).

    Instead Editor just reads the local information that the DataTable has and shows that in the editing form.

    What you would need to do instead is listen for the initEdit event and make an Ajax call to the server to get the data for the rest of the fields based on the selected row. You would then use val() to set the value of those fields based on the incoming data.

    Regards,
    Allan

    (1) This is actually a feature I've thought about adding in the past, and with the new template options that moves up the priority list since forms can be more complex quite easily now.

  • brendonsbrendons Posts: 39Questions: 14Answers: 1

    Thanks Allan.
    I was right - it was stupid question!

  • allanallan Posts: 61,822Questions: 1Answers: 10,127 Site admin
    Answer ✓

    I disagree ;-). I think it was a good question and the fact that we both have been thinking of the same feature for Editor just means that you are one step ahead of the game!

    Allan

This discussion has been closed.