How to Concat two fields in Autocomplete or dropdown options


#1

Is there a way to show more than one field concatenated in the Autocomplete or Dropdown options?

In my case I want to show all cities names as options plus another field called country…

in html will be something like

Miami Beach, United States
Paris, France

I have read all ATK4 things about Autocomplete and dropdown and I have now found any solution for this…

the reason there are many cities in the world with the same name but if I can show the country the use knows the city he/she is looking for…

any ideas thanks


#2

yes, easily.

You know that ‘title_field’ is displayed, so simply define expression with concatination and display that.


#3

Ohh I will try that !!! but how do you use the concat with expression for a modal inside title field title_field? I try many forms and gives me error…

expression is not an object… etc…


#4

In your city:

    public $title_field = 'title';

    function init() {
        parent::init();
        ...

        $this->hasOne('country_id', new country())->addTitle();
        $this->addExpression('title', 'concat([name], "-", [country])');


#5

thanks that makes sense now but…

is it possible to apply the same thing to an autocomplete field? I tried to apply it but it does not work it gives me error:

atk4\core\Exception: Child element not found
parent: Object Airports
element: "airport_name"

I am using the model… with grud.

My code:

class Airports extends Model {
    public $table = 't_airports';
    public $title_field = 'airport_name';

    function init() {
        parent::init();
        $this->addField('name');
        $this->setOrder('name');
    }
}

and then:

    function init() {
        parent::init();

...

             $this- > hasOne('origin_id', [new Airports(),'required' => true, 'caption'=>'Origin Airport', 
            'ui'=>[
                'form'=>['AutoComplete'],    
        ]])->addTitle();

        $this->addExpression('airport_name', 'concat([name], "-", [iata], "-" [country_code])');

I want to create the expression so the options will be displaying airport name and iata code of the airport:

London International Airport, LON - UK

or is it possible to contact fields inside ->addTitle(); ?

maybe I am doing something wrong?
thanks


#6

you are doing everything correctly. Try with the [name] only first then add other fields. To reference title field of “origin_id” use [origin] which is default field name.

Once you define a field it should work everyhere, in crud in auto-complete etc.


#7

ok I will try to do that again and let you know my feedback


#8

It does not work either: try many combinations and possibilities and nothing seems to work sadly:

Here my code:

My SQL table for airports:

CREATE TABLE `airports` (
  `ID` int(11) NOT NULL,
  `iata` varchar(255) NOT NULL,
  `Name` varchar(255) NOT NULL,
  `CityID` varchar(255) NOT NULL,
  `CountryID` varchar(255) NOT NULL,
  `Location` varchar(255) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

here my airport class to include the table Airport in my dropdown autocomplete later…

class Airports extends Model {
    public $table = 'airports';
    public $title_field = 'Name';

    function init() {
        parent::init();
        $this->addField('Name');
        $this->setOrder('Name');
    }
}

here my Deal model that addfield Airports in a dropdown autocomplete field

class dealDates extends Model {
    public $table = 'dealDates';
    public $title = 'Deal Dates';
    public $title_field = 'deal';


    function init() {
        parent::init();

        $this->hasOne('origin_id', [new Airports(),'required' => true, 'caption'=>'Origin Airport', 'ui'=>['form'=>['AutoComplete']]])->addTitle();

        $this->addExpression('Name', 'concat([Name], ",", [iata])');

....

and this is the table dealDates used by the class above

CREATE TABLE `dealDates` (
  `id` int(11) NOT NULL,
  `origin_id` int(11) NOT NULL,
  `date_from` varchar(255) DEFAULT NULL,
  `date_to` varchar(255) DEFAULT NULL,
  `price` varchar(255) DEFAULT NULL,
  `currency` varchar(255) DEFAULT NULL,
  `deal_id` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

Error:

**Fatal Error**
**atk4\core\Exception: Child element not found**

**Exception Parameters:**
**parent: Object Deal**
**element: "iata"**

the logic here it is that table dealDates save the deals for a determined airport origin… and airport name is taken from airport table with those fields I need to concat to show more than just the Name of the airport but iata and countryID

Any Ideas what is the problem here that this error comes all the time?


#9

please escape code blocks

CREATE TABLE dealDates (

into

CREATE TABLE dealDates (

#10

Got it!
I did not know it is possible… now I did it !! thanks for telling me… now it can be read easily; I just edited and posted again with the code syntax highlighted, see above!!

thanks!


#11

I think I found the issue after hours spending trying many possibilities finally I found out that this code below works:

I added first inside Class Airports

$this->addField('iata');
$this->addField('countryID');

and then

$this->addExpression('Objname', 'concat([Name], ", [", [iata],"] - ", [countryID])');

having now this:

class Airports extends Model {
    public $table = 'airports';
    public $title_field = 'Objname';

    function init() {
        parent::init();
        $this->addField('Name');
        $this->addField('iata');
        $this->addField('countryID');
        $this->setOrder('Name');
        $this->addExpression('Objname', 'concat([Name], ", [", [iata],"] - ", [countryID])');
    }
}

then inside Deal class I deleted the addExpression from there …

$this->addExpression('... 

and now I only have this:

class dealDates extends Model {
    public $table = 'dealDates';
    public $title = 'Deal Dates';
    public $title_field = 'deal';


    function init() {
        parent::init();

        $this->hasOne('origin_id', [new Airports(),'required' => true, 'caption'=>'Origin Airport', 'ui'=>['form'=>['AutoComplete']]])->addTitle();

....

Now the dropdown autocomplete search filter and show that I want like:

airport name + iata + country code id

London Stansted, [STN] - UK

I think documentation regarding this topic will be perfect as a sample so next guy do not spend to much time finding how to make it work this in a Model…

thanks for helping a lot with this one!! Now I need to go next thing finding out how to populate cities from countries… still not working that part… but I am happy I found this solution for concat fields inside a dropdown :slight_smile:

thanks


#12

i’m writing tutorials when time permits and publishing them on my medium site: https://medium.com/@romaninsh, but more stories and posts are always welcome.


#13

Regarding drop-downs. You can actually add dropdown or autocomplete anywhere even without form, so something should be possible there.


#14

thanks … yes I will try to check these parts and see how they are going, I think there should be something too to work with them … :slight_smile: