mardi 31 août 2021

Laravel / JQGrid - Order by on laravel dynamic property

I'm currently working with free JQGrid 4.6 version and Laravel 5.8 and I have some issue with a grid that I have develop. So here is the problem recap : i want to sort some columns that contains datas coming from dynamic attributes of my model Contact.

Here is the columns of the grid :

{name: "ID_PROJET", hidden:true},
{name: "DT_REEL_DON", align:'center',formatter:'date',sorttype : 'date', datefmt: 'd/m/Y',formatoptions: {srcformat:'Y-m-d',newformat: "d/m/Y"},excel:true, width:90},
{name:"LIBELLE_PROJ", excel:true,width:250},
{name:"contact.full_identite", excel:true},
{name:"MT_DON",formatter:'currency',align:'right',sorttype:'numeric',excel:true, width:60},
{name:"lib_type_don",excel:true, sortable:false},
{name:"lib_moyen_paiement",excel:true,width:60,sortable:false},
{name:"MAIL", excel:true},
{name:"ADRESSE1", excel:true},
{name:"CDPOST", excel:true,width:50},
{name:"COMMUNE", excel:true},
{name:"PAYS", excel:true,width:70},
{name:"SOLLICITE",excel:true,width:30},
{name: "action",template:"action",sortable:false, width: 75, align:"center",excel:false}  

Datas come from this request that i serialize to JSON :

$dons = $dons->with(['projet','contact'])
     ->join('projet','projet.ID_PROJET',"=",'don.ID_PROJET')
     ->join('contact','contact.ID_CONTACT',"=",'don.ID_CONTACT')
     ->orderBy($sidx,$sord)
     ->select('don.*','contact.MAIL','contact.ADRESSE1','contact.CDPOST','contact.COMMUNE','contact.PAYS','contact.SOLLICITE','projet.LIBELLE_PROJ')
     ->offset($start)->limit($limit)->get();

$sidx, $sord, $start and $limit are GET parameters send by JQGrid when the user have done an action on the grid (paging, sorting etc)

It's working great, but the problem is for the column contact.full_identite which is not a field into my contacts table. It's a laravel dynamic attributes define like that :

    public function getFullIdentiteAttribute() {
        // si entreprise 
        if ($this->TYPE_CONTACT_ == 1) {
            return $this->ORGANISME;
        }
        // si particulier
        else if ($this->TYPE_CONTACT_ == 2) {
            return $this->NOM . ' ' . $this->PRENOM;
        }
    }

The idea is to return the full name of a person if this is a particular or the organisation name if this a company. So i'm searching for a way to apply an orderBy clause (in my request above) on this dynamic attribute when the user want to sort this column. For the moment, i sort on the field NOM and it's working good but this is not the result that i expect.

Here is a screenshot of a structure part of my contact table :

Structure table

Thanks in advance for your help, if i wasn't enough clear don't hesitate to ask me some details



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire