How to properly render Mjoin json result?

How to properly render Mjoin json result?

ziv@kpmbro.comziv@kpmbro.com Posts: 73Questions: 28Answers: 4
edited July 2016 in General

Hi

I am running a Mjoin and all the data is fine, the only problem is that in the Mjoin column at the client side
i am getting [object Object] or an error.

fields json:

[  
   {  
      "label":"Geos",
      "name":"general_data_countries[].iso_2_letter_code",
      "type":"select"
   }
]

columns json:

[  
   {  
      "data":"general_data_countries",
      "render":"[, ].name"
   }
]

Server-side:

$this->editor_instance->leftJoin( 'offers_target_geos', 'offers_target_geos.offer_id', '=', 'offers_main_table.id' )
    ->join(
        Mjoin::inst( 'general_data_countries' )
            ->link( 'offers_main_table.id', 'offers_target_geos.offer_id' )
            ->link( 'general_data_countries.iso_2_letter_code', 'offers_target_geos.country_id') 
            ->order( 'name asc' )
            ->fields(
                Field::inst( 'iso_2_letter_code' )
                    ->validator( 'Validate::required' )
                    ->options( 'general_data_countries', 'iso_2_letter_code', 'name' ),
                Field::inst( 'name' )
            )
    );

Ajax json:

[ {  
         "DT_RowId":"row_6",
         "offers_main_table":{  
            "title":"bla bla",
            "network_campaign_id":"bla bla",
            "advertiser_id":"1",
            "offer_payout":"12.75"
           },
         "general_data_countries":[  
            {  
               "iso_2_letter_code":"NL",
               "name":"Netherlands"
            },
            {  
               "iso_2_letter_code":"RU",
               "name":"Russia"
            },
            {  
               "iso_2_letter_code":"US",
               "name":"United States"
            }
         ]
      }]

How should it be rendered?

Thanks :)

Answers

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin
    edited July 2016

    Could you try:

       {
          "data": null,
          "render":"general_data_countries[, ].name"
       }
    

    What you have above should work, but I wonder if it is encountering an issue with [] being the first part of the render...

    Allan

  • ziv@kpmbro.comziv@kpmbro.com Posts: 73Questions: 28Answers: 4

    Hey Allan.

    No it didnt worked...
    still getting :

    DataTables warning: table id=offers_main_table - Requested unknown parameter 'null' for row 0, column 23. For more information about this error, please see http://datatables.net/tn/4

    any ideas ? Thanks :)

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    Could you run the debugger over the table and send me the debug code (six characters)?

    Allan

  • ziv@kpmbro.comziv@kpmbro.com Posts: 73Questions: 28Answers: 4

    Hi Allan, I sent it to your mail. Thanks :)

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    The debugger appears to suggest that the following is used:

    "render": "general_data_countries, .name"

    rather than:

    "render": "general_data_countries[, ].name"

    Is it possible that your modelling code is stripping the square brackets somewhere?

    Allan

  • ziv@kpmbro.comziv@kpmbro.com Posts: 73Questions: 28Answers: 4
    edited July 2016

    Yes Allan that was that, i had a clean string function that remove the '[ ]]. :)

    Now that i can see it i am trying to make that multi-select field so i try this:

    array("label"=>"Geos",
                          "name"=>"general_data_countries[].iso_2_letter_code",
                          "type"=>"select",
                          "multiple"=>"true",
                          "separator"=>",",
                         "options"=>array("1"=>"RU","2"=>"UK","3"=>"AG","4"=>"BE","5"=>"US") 
                          );
    

    I am getting this error:
    "dataTables.editor.min.js:56 Uncaught Unable to automatically determine field from source. Please specify the field name. For more information, please refer to https://datatables.net/tn/11"

    What am i missing?

    Thanks

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    You need to use the editField option that we discussed in order to tell Editor which field it should edit when you click on that column.

    Allan

  • ziv@kpmbro.comziv@kpmbro.com Posts: 73Questions: 28Answers: 4
    edited July 2016

    Hi Allan after installing the select2 it all works good except of the part when i press update i get this. (pic attached).

    this comes out of the console:

        {"fieldErrors":[{"name":"general_data_countries[].iso_2_letter_code","status":"This field is required"}]}
    

    My fields:

    {"label":"Geos","name":"general_data_countries[].iso_2_letter_code","type":"select2","multiple":true,"opts":{"placeholder":"Select Geos","allowClear":true}}
    

    My columns:

    {"data":null,"render":"general_data_countries[, ].name","editField":"general_data_countries[].iso_2_letter_code"}
    

    What did I missed?

    Thanks

  • ziv@kpmbro.comziv@kpmbro.com Posts: 73Questions: 28Answers: 4
    edited July 2016

    I figure it out it was the Validator.

    But still i get no result after updating :( ...

    Allan do i need to use the getFormatter with the Mjoin?

    somthing like :

    $this->editor_instance->leftJoin( 'offers_target_geos', 'offers_target_geos.offer_id', '=', 'offers_main_table.id' )
        ->join(
            Mjoin::inst( 'general_data_countries' )
                ->link( 'offers_main_table.id', 'offers_target_geos.offer_id' )
                ->link( 'general_data_countries.iso_2_letter_code', 'offers_target_geos.country_id')
                ->order( 'name asc' )
                ->fields(
                    Field::inst( 'iso_2_letter_code' )
                        ->validator( 'Validate::required' )
                        ->options( 'general_data_countries', 'iso_2_letter_code', 'name' ),
                    Field::inst( 'name' )->getFormatter( function ($val, $data, $field) {
                            return array("RU"=>"Russia","MX"=>"Mexico") //and so on.....
                        })
                )
        );
    
  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    You have the country name in the database do you not? There should be no need to use a getFormatter. You want it to get the 2 letter code and the name.

    What is the JSON return after an update, and what is the data that is being submitted? You will be able to find that using the network inspector tools in your browser.

    Thanks,
    Allan

  • ziv@kpmbro.comziv@kpmbro.com Posts: 73Questions: 28Answers: 4
    edited July 2016

    Thanks Allan.

    I am getting two answers short and long.

    short:

        {"data":[{"DT_RowId":"row_5","offers_main_table":{"title":"AlbertHeijn - NL - Incent (EUR)_AlbertHeijn - NL - Incent"},"general_data_countries":[{"iso_2_letter_code":"NL","name":"Netherlands"}]}],"filtering":"no_filters"}
    
  • ziv@kpmbro.comziv@kpmbro.com Posts: 73Questions: 28Answers: 4

    I cant post the long answer it have to many chars, i will mail it to you.

    Thanks

This discussion has been closed.