Architecture for manipulation of data from JSON/as

2019-08-29 10:47发布

问题:

This question is more of like requiring an architecture and correct approach, then actual code. Stating it so that people shouldn't flag it as "We cannot code for you..." etc.

I get a response from the API in JSON which I converted to an associated array using json_decode as:

Array
(
    [statusCode] => 200
    [data] => Array
        (
            [objects] => Array
                (
                    [0] => deals
                    [1] => contacts
                    [2] => accounts
                )

            [deals] => Array
                (
                    [0] => dealName
                    [1] => ApprovedBy
                    [2] => ApprovedDate
                    [3] => CloseDate
                )

            [contacts] => Array
                (
                    [0] => contectName
                    [1] => email
                    [2] => firstName
                    [3] => lastName 
                )

            [accounts] => Array
                (
                    [0] => accountName
                    [1] => creationDate
                    [2] => ApprovedDate
                    [3] => accountNumber
                )
        )

)

So the structure is as: In response's data array against key "objects", I have an array of objects i.e deals, contacts, accounts. And for each element in the objects array as the key, an array of fields exist.

  1. I have to show the objects in a drop down say "Objects" and upon selection of a value say "deals" I want to show elements of deals array in a second drop-down "Fields".

  2. [DONE] Also for the options in the dropdowns, values should be the same as actual elements and not numeric values. json_decode(string, true) gives associated array but with numeric keys. So keys should be same as their associated values like:

    [deals] => deals [contacts] => contacts [accounts] => accounts

  3. In the view, I have a table with a number of rows, each row having its own pair of these drop-downs. If each row has its own drop-down, then I also have to take care avoiding updating second drop-down in wrong places. As each drop-down is an array of objects[] & object_fields[], should I use the index of this array in DOM, get next sibling in DOM, or to assign a unique id to each row for the population of correct drop-down at correct row?

How should I achieve these, what should be the correct way? I cannot use ajax/getJson() as to reduce the number of requests to API and I do have all the required data available. Just need to use it but needed meticulous approach regarding architecture.

Should I pass on Json object to view and process it in javascript in view? Will this be a better approach?

Thanks

UPDATE: Point 2 of the question is DONE.