Editor 'select' element and data returned by the server

Editor 'select' element and data returned by the server

gravstengravsten Posts: 17Questions: 5Answers: 0

Description of problem:
I followed Alan's answer to this question:
https://datatables.net/forums/discussion/25357/why-is-the-select-display-showing-the-value-instead-of-the-label
which refers to the documentation:
https://datatables.net/manual/tech-notes/11

This works, but not entirely how I expected.
I have configured the Editor thus:

      fields: [
        { name: 'title.id', type: 'select', options: [{"label":"M.","value":1},{"label":"Mme","value":2}] },
      ],

And datatables thus:

        columns = [
          { name: 'title', data: 'title.name', editField: 'title.id' },
        ],

The data sent to server is fine:
{"data":{"4":{"ok":"","title":{"id":"2"}}},"action":"edit"}

But the response returned by the server must also include the 'name' field:
{"data":[{"id":4,"title":{"id":2,"name":"Mme"}]}

Otherwise, an error is shown and the label is not updated !

Given the aforementioned configuration (fields -> options) that links each value to a label, shouldn't the 'name' field in the response be entirely superfluous?

In my case, the server has to load a full 150K of translations just to get the 'name' field in the correct language.

Was the response's mandatory 'name' field really the Editor's intended behaviour ?

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Given the aforementioned configuration (fields -> options) that links each value to a label, shouldn't the 'name' field in the response be entirely superfluous?

    Kind of. You are correct that it would be possible to look up the label on the client-side from the existing list of options, but Editor doesn't do that. Instead there is an assumption that the response from the server will be the full data for the row, in exactly the same format as when the table was initially loaded. You can see that in the example here.

    The reason Editor doesn't do the look up on the client-side is that I figured the server-side would run the same "get" code for the result of the edit as it would for the initial load, so there is no need for additional work at the client-side (which is exactly what the server-side libraries I publish for Editor do). There is also the small possibility that the label might have changed at the server-side between the updates, so this would make sure that the latest one is returned.

    Allan

  • gravstengravsten Posts: 17Questions: 5Answers: 0

    Thanks for the detailed explanation.
    While the Editor's behaviour w.r.t. select options differs from regular HTML, it certainly makes sense.

    As a suggestion for a future version, you may consider looking up the label on the client-side from the existing list of options if the 'name' field is missing in the server response.

    Which is what I implemented through events:

        }).on('preSubmit', function (e, data, action) {
          var id, e;
          for (id in data.data) {
            e = data.data[id];
            if (e.hasOwnProperty('title'))
              e.title = e.title.id;
          }
        }).on('postSubmit', function (e, data, action) {
          var id, e, values = ["","Mr.","Mrs."];
          for (id in data.data) {
            e = data.data[id];
            if (e.hasOwnProperty('title'))
              e.title = { id: e.title, name: values[e.title] };
          }
        }),
    

    This way my server only needs to return the option's 'id' field, not having to load a full 150K of translations (among them the 'name' field in the correct language).

    Best regards,
    Guilhem.

Sign In or Register to comment.