我目前正在使用免費的JQGrid 4.6版本和Laravel 5.8,我在開發一個網格時遇到了一些問題。問題是這樣的:我想對一些列進行排序,這些資料來自我的模型Contact的動態屬性。
這里是網格的列:
{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"/span>, excel: true,width:50},
{name:"COMMUNE", excel:true},
{name:"PAYS"/span>, excel: true,width:70}。
{name:"SOLLICITE",excel。 true,width:30}。
{name: "action"/span>,template: "action",sortable:false, width: 75, align:"center",excel:false}。
資料來自這個請求,我將其序列化為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)
->選擇('don.*','contact.MAIL','contact.ADRESSE1','contact. CDPOST','contact.COMMUNE','contact.PAYS','contact.SOLLICITE','projet.LIBELLE_PROJ')
->offset($start)-> limit($limit)-> get()。
$sidx, $sord, $start 和 $limit 是JQGrid在用戶對網格進行操作(分頁、排序等)時發送的GET引數
它作業得很好,但問題出在contact.full_identite列上,它不是我的contacts表中的欄位。這是一個laravel動態屬性的定義:
public function getFullIdentiteAttribute(/span>) {
// si entreprise
if ($this-> TYPE_CONTACT_ == 1) {
return $this->ORGANISME。
}
// si particulier
else if ($this-> TYPE_CONTACT_ == 2) {
return $this->NOM . ' '/span> . $this->PRENOM;
}
}
我們的想法是,如果這是一個特定的人,就回傳他的全名;如果這是一個公司,就回傳組織名稱。
所以我正在尋找一種方法,當用戶想對這一列進行排序時,在這個動態屬性上應用一個orderBy子句(在我上面的請求中)。目前,我在欄位NOM上進行排序,效果不錯,但這并不是我期望的結果。
下面是我的contact表的一個結構部分的截圖:
提前感謝您的幫助,如果我說得不夠清楚,不要猶豫,請問我一些細節
。uj5u.com熱心網友回復:
如果有人感興趣,我解決了我的問題,在$sidx屬性上添加條件,并使用Laravel的sortBy,sortByDesc和splice方法。
不確定這是實作的最佳方式,但這是可行的。
代碼:
if ($sidx == " contact.full_identite") {
$dons = $dons->with(['projet','contact'] )
->join('projet','projet.ID_PROJET',"=",'don.ID_PROJET')
->加入('contact','contact.ID_CONTACT',"=",'don.ID_CONTACT')
->選擇('don.*','contact.MAIL','contact.ADRESSE1','contact. CDPOST','contact.COMMUNE','contact.PAYS','contact.SOLLICITE','projet.LIBELLE_PROJ')
->get()。
if (strtoupper($sord) == "ASC") {
$dons = $dons-> sortBy(function($don) {
return $don-> contact->identite;
});
}
if (strtoupper($sord) == "DESC") {
$dons = $dons-> sortByDesc(function($don) {
return $don-> contact->identite;
});
}
$dons = $dons->splice($start,$limit)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/320606.html
標籤:
上一篇:如何回滾Laravel命令
下一篇:laravel中的多個與關閉

