Get Access to returned Json key

Get Access to returned Json key

Pliachas PaschalisPliachas Paschalis Posts: 62Questions: 12Answers: 1
edited July 2020 in Editor

Good morning.
This is the returned Json string form my php file

{
    "data": [
        {
            "DT_RowId": "row_10",
            "tblMembers": {
                "nminstitution": "rtyert",
                "nminstitutionlocal": "ertert",
                "distinctivetitle": "tyrt5ywe5r",
                "LglStatusId": "1",
                "LevelId": "2",
                "CountryId": "1",
                "RegionId": "1",
                "webst": "trhytrhrst",
                "address": "Address 8",
                "phone": "9999999999",
                "fax": "5464022670",
                "email": "k256@ert.gr"
            },
            "tblLglStatus": {
                "LegalName": "Private"
            },
            "tblLevel": {
                "NameOfLevel": "Regional"
            },
            "TblCountries": {
                "CountryName": "Country1"
            },
            "TblRegions": {
                "NameOfRegion": "Region East"
            }
        },
        {
            "DT_RowId": "row_11",
            "tblMembers": {
                "nminstitution": "municipality of Opppp",
                "nminstitutionlocal": "BlahNlah",
                "distinctivetitle": "municipality of BlahBlah",
                "LglStatusId": "2",
                "LevelId": "2",
                "CountryId": "1",
                "RegionId": "1",
                "webst": "www.kkkkkk.gr",
                "address": "my address",
                "phone": "",
                "fax": "",
                "email": "pt@oote.gr"
            },
            "tblLglStatus": {
                "LegalName": "Public"
            },
            "tblLevel": {
                "NameOfLevel": "Regional"
            },
            "TblCountries": {
                "CountryName": "Country1"
            },
            "TblRegions": {
                "NameOfRegion": "Region1"
            }
        }
    ],
    "options": {
        "tblMembers.LglStatusId": [
            {
                "label": "Ngo",
                "value": "3"
            },
            {
                "label": "Private",
                "value": "1"
            },
            {
                "label": "Public",
                "value": "2"
            }
        ],
        "tblMembers.LevelId": [
            {
                "label": "Local",
                "value": "3"
            },
            {
                "label": "National",
                "value": "1"
            },
            {
                "label": "Regional",
                "value": "2"
            }
        ],
        "tblMembers.CountryId": [
            {
                "label": "Country2",
                "value": "2"
            },
            {
                "label": "Country1",
                "value": "1"
            }
        ],
        "tblMembers.RegionId": [
            {
                "label": "Region2",
                "value": "2"
            },
            {
                "label": "Region3",
                "value": "3"
            },
            {
                "label": "Region4",
                "value": "4"
            },
            {
                "label": "Region1",
                "value": "1"
            }
        ]
    },
    "files": [],
    "searchPanes": {
        "options": []
    }
}

how i get access to this key? "CountryId": "1"
i am trying to use this one you described here: https://datatables.net/blog/2017-09-01#.NET, but with no success until now.
Thanks in advance

EDIT: Code formatting updated by Kevin

Answers

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765

    Does the Nested Objects example help?

    Kevin

  • Pliachas PaschalisPliachas Paschalis Posts: 62Questions: 12Answers: 1

    I'll try that and let you know.

  • Pliachas PaschalisPliachas Paschalis Posts: 62Questions: 12Answers: 1

    Sorry but i can't find out how this works.
    This is the place i need the returned value.

    ```

        editor.dependent(
    
            "tblMembers.CountryId",
            function (val, data, callback) {
    
                Object.keys(data).forEach(function (key) {
                    console.log(key, data[key]);
                });
    
                console.log(data.tblMembers[0].CountryId)
    
                // console.log(data.data['tblMembers.CountryId']);
                var fCountryCode = $('tblMembers.CountryId').val();
                console.log(fCountryCode);
                $.ajax({
                    url: 'php/regions.php',
                    dataType: 'json',
                    data: ({
                        fCountryCode: '1'//data.values.CountryId                        
                    }),
                    success: function (json) {
                        callback(json);
                        console.log(json);
                    }
                }
                )
            },
            {
                event: 'keyup change'
            }
        );
    

    i tried to get fCountryCode using jquery but with no success.

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Where do you want access to it? For use in a DataTable column you would use data: 'tblMembers.CountryId'.

    Allan

  • Pliachas PaschalisPliachas Paschalis Posts: 62Questions: 12Answers: 1

    my table.members.js file is like that:

    ````

           var editor = new $.fn.dataTable.Editor({
            ajax: 'php/table.tblMembers.php',
            table: '#tblMembers',
            fields: [
                //some fields...
              {
                    "label": "Country:",
                    "name": "tblMembers.CountryId",
                    type: "select",
                    placeholder: "Select a Country",
                    //options: json.options['tblMembers.RegionId']//.RegionId
                },
                {
                    "label": "Region:",
                    "name": "tblMembers.RegionId",
                    type: "select",
                    placeholder: "Select a Region"
                },
               //other fields...
            ]
        });
       editor.dependent(
    
            "tblMembers.CountryId",
            function (val, data, callback) {
    
                var pCountryId = this.field( 'tblMembers.CountryId');
                var cfCountryCode =pCountryId.val();
                console.log(cfCountryCode);
                if (  pCountryId.val() ) {
                    console.log("pas");
                    $.ajax({
                        url: 'php/regions.php',
                        dataType: 'json',
                        data: ({
                            fCountryCode: cfCountryCode                     
                        }),
                        success: function (json) {
                            console.log(json);
                            console.log("pas");
                            callback(json);
    
                        }
                    })
                }
    
            },
            {
                event: 'keyup change'
            }
        );
       var table = $('#tblMembers').DataTable({
            dom: 'Bfrtip',
            ajax: 'php/table.tblMembers.php',
            columns: [
               //some columns....
                {
                    "data": "TblCountries.CountryName"
                },
                {
                    "data": "TblRegions.NameOfRegion"
                },
               //other columns....
            ],
            select: true,
            lengthChange: false,
            buttons: [
                { extend: 'create', editor: editor },
                { extend: 'edit', editor: editor },
                { extend: 'remove', editor: editor }
            ]
        });
    });
    

    my php file returns:
    {"options":{"tblMembers.RegionId":[{"label":"West Macedonia","value":"1"}]}}

    but until now i am unable to fill the "select" "name": "tblMembers.RegionId", with the returned json values.
    Am i missing the obvious?

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Not unless I'm missing it as well! That response should cause the Editor field to have one option in the select list.

    Can you give me a link to the page so I can help to debug it please?

    Thanks,
    Allan

  • Pliachas PaschalisPliachas Paschalis Posts: 62Questions: 12Answers: 1

    thanks for your response. This is the link
    https://pliachaspaschalis.com/4plus-project/mainpage_demo.php

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Thank you - but I'm afraid I'm not understanding then. Loading the page and clicking the New button shows me that the tblMembers.RegionId select is being populated with 4 options.

    Allan

  • Pliachas PaschalisPliachas Paschalis Posts: 62Questions: 12Answers: 1

    But the problem is that when the user selects the first country it should show only the records (from Regions table) that have the countrycode =1 or when select the second country only the records with countrycode=2.
    Now you see all the records instead of 3 in first case and one on the second case.

  • Pliachas PaschalisPliachas Paschalis Posts: 62Questions: 12Answers: 1
    edited July 2020

    After making some corrections to region.php file it's working

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Thanks for the update. Good to hear you’ve got it working.

    Allan

This discussion has been closed.