Do the JSON objects for joined table contents have not be named the same as a column?

Do the JSON objects for joined table contents have not be named the same as a column?

burncharburnchar Posts: 118Questions: 12Answers: 0
edited October 2012 in Editor
I have a 1:N relationship (EQUIPMENT) working in my joined table, but adding a 1:1 (SPECIES_ID), which should be simpler, gives me a blank select box every time. I've spent hours checking and re-checking the generated JSON and the DT/Editor Javascript and I swear they are perfect.

I would try using a different column name, but I am hoping this isn't required because in my implementation it's pretty important they are the same: The column provides the table ID. It's also how the implementation determines if a join is 1:1 or 1:N.

Applicable code (trimmed):

Editor:
[code]
"fields": [
//...
{"label": "Equipment:", "name": "EQUIPMENT[].id", "type": "checkbox" }, // works
{"label": "Species:", "name": "SPECIES_ID.id", "type": "select" } // !works
[/code]

DataTables:
[code]
{"mData":"PARENT_RECIPE"},
{
"mData": "SPECIES_ID.name",
"sDefaultContent": "foo"
},
{"mData":"EQUIPMENT", "mRender":"[, ].name"},
/// ...
[/code]

Some of the JSON "SPECIES_ID" object, appended to the JSON after "aaData"
[code]
"SPECIES_ID": [{
"value": "1",
"0": "1",
"label": "UNKNOWN (UNKNOWN)",
"1": "UNKNOWN (UNKNOWN)"
},
{
"value": "2",
"0": "2",
"label": "B (Boron)",
"1": "B (Boron)"
},...
[/code]

An entry from aaData containing the species and equipment for one row:
[code]
"SPECIES_ID": {
"id": "3",
"name": "P (Phosphorus)"
},
"EQUIPMENT": [{
"id": "6",
"name": "CIVAR11X"
},
{
"id": "8",
"name": "CIVAR62X"
}],
[/code]


The only thing I can think of is that DTE may not like SPECIES_ID being both a column name and a JSON object. I can't figure out why it would matter though as they are accessed and treated very differently as far as I can tell.
If not, does anyone have a theory as to why my select box is always empty?

Replies

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Okay - this isn't going to work the way you have it. The mData and mRender options can only read information from the data source object - it cannot read data from outside that object. So referencing your external SPECIES_ID array simply isn't going to work without additional code.

    For example, change, in your DataTables initialisation, change `SPECIES_ID.name` to `SPECIES_ID.id` (i.e. a property that does exist within the data source object) and you should see the id inserted. You are getting an empty box because `SPECIES_ID.name` does not exist in the data source object!

    So the way to address this is to use mRender to look up the external data. Something like:

    [code]
    mData: 'SPECIES_ID.name',
    mRender: function ( data, type, row ) {
    return json.SPECIES_ID[ data ];
    }
    [/code]

    Two assumptions here:

    1. `json` can be accessed from your mRender (you'll need to make it available - how that is done will depend upon your setup - assigning it to a 'locally global' variable in fnServerData for example would work.

    2. The `id` (i.e. `data` in the mRender function) is also the array index. If that isn't the case, you'd need to do a lookup, spinning through the SPECIES_ID array looking for the right id (which, if you have a lot of records could slow things down).

    Allan
  • burncharburnchar Posts: 118Questions: 12Answers: 0
    I was unclear. Display of the column in the data table works for both the 1:1 (species) and 1:N (equipment). It's just the select box in the editor that shows empty data.
    Both the SPECIES_ID and EQUIPMENT objects have the same scope

    I'm sorry but I still do not understand why species_id needs to be accessed differently than equipment. I think some pictures will help me describe the issue better than words.

    The data provided for the Editor seems to be the problem, but it seems to be in exactly the same format as the example. Also, EQUIPMENT and SPECIES_ID have the same scope, so why would one be available but not the other?:
    http://i.imgur.com/tY1pr.png

    Entries in aaData for each row (this works fine -- the DataTable shows EQUIPMENT.name for both 1:1 and 1:N):
    http://i.imgur.com/pi3kG.png

    Visually, here's the DataTable and part of the edit form when I click "edit" for the top row:
    http://i.imgur.com/CUOez.png
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    How are you updating the entries in the Species select menu? Are you using the field's `update` method?

    Allan
  • burncharburnchar Posts: 118Questions: 12Answers: 0
    [quote]allan said: How are you updating the entries in the Species select menu? Are you using the field's update method?[/quote]

    That's it. I was missing: [code]editor.field('SPECIES_ID.id').update( json.EQUIPMENT );[/code]
    How unsettling it is to have missed something so obvious. Thank you again, Allan! I hope this comes up in a Google search for someone else who needs to pay more attention to their code so that your time answering it won't be wasted.
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    No worries - good to hear you got it sorted out :-)

    Allan
This discussion has been closed.