具有两个规则的数据表表顺序
Posted
技术标签:
【中文标题】具有两个规则的数据表表顺序【英文标题】:Datatables table order with two rules 【发布时间】:2017-10-15 16:29:41 【问题描述】:我正在按照here 中的示例制作表格,除了我无法弄清楚的一件事之外,我已经全部运行了:
当这些值为“XXX/YY”时,我如何使用“numeroregisto”列的值来订购和设置表格的初始顺序 - 我需要它以便它对数字进行排序首先是斜线的右边,然后是左边的。 右边的数字总是两位数,左边的数字可以从一到五位不等。
在另一个场合,使用不同的方法,我被告知这样做:
ORDER BY cast(substring_index(numeroregisto, '/', - 1) AS signed INTEGER), cast(substring_index(numeroregisto, '/', 1) AS signed INTEGER) ASC
但我似乎无法弄清楚如何使用这种方法来实现这一点:
var $table_registos = 'registos';
var $column_order_registos = array('numeroregisto','dataregisto','amostra','especies.especie','requisitante',null); //set column field database for datatable orderable
var $column_search_registos = array('registos.numeroregisto','registos.dataregisto','registos.amostra','especies.especie','requisitante'); //set column field database for datatable searchable just firstname , lastname , address are searchable
var $order_registos = array('numeroregisto' => 'ASC'); // default order
private function _get_datatables_query_registos()
$this->db->select('registos.*, origemmaterial.id AS omid, origemmaterial.origem, meioconservacao.id AS mcid, meioconservacao.meio, especies.id AS esid, especies.especie');
$this->db->from('registos, origemmaterial, meioconservacao, especies');
$this->db->where('registos.origemid=origemmaterial.id AND registos.meioid=meioconservacao.id AND registos.especieid=especies.id');
$i_registos = 0;
foreach ($this->column_search_registos as $item) // loop column
if($_POST['search']['value']) // if datatable send POST for search
if($i_registos===0) // first loop
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
$this->db->like($item, $_POST['search']['value']);
else
$this->db->or_like($item, $_POST['search']['value']);
if(count($this->column_search_registos) - 1 == $i_registos) //last loop
$this->db->group_end(); //close bracket
$i_registos++;
if(isset($_POST['order'])) // here order processing
$this->db->order_by($this->column_order_registos[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
else if(isset($this->order_registos))
$order_registos = $this->order_registos;
$this->db->order_by(key($order_registos), $order_registos[key($order_registos)]);
任何帮助将不胜感激,在此先感谢!
【问题讨论】:
当前代码有什么问题?您能否将自己的代码缩小到您认为问题所在的部分? 我已按照您的建议编辑了帖子以缩小范围,认为现在应该清楚了。谢谢! 【参考方案1】:问题已解决,完全由我自己解决! 只是希望我能想出一个更优雅的解决方案:
private function _get_datatables_query_registos()
$this->db->select('registos.*, origemmaterial.id AS omid, origemmaterial.origem, meioconservacao.id AS mcid, meioconservacao.meio, especies.id AS esid, especies.especie');
$this->db->from('registos, origemmaterial, meioconservacao, especies');
$this->db->where('registos.origemid=origemmaterial.id AND registos.meioid=meioconservacao.id AND registos.especieid=especies.id');
$i_registos = 0;
foreach ($this->column_search_registos as $item) // loop column
if($_POST['search']['value']) // if datatable send POST for search
if($i_registos===0) // first loop
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
$this->db->like($item, $_POST['search']['value']);
else
$this->db->or_like($item, $_POST['search']['value']);
if(count($this->column_search_registos) - 1 == $i_registos) //last loop
$this->db->group_end(); //close bracket
$i_registos++;
if(isset($_POST['order'])) // here order processing
if($_POST['order']['0']['column']=='0' && $_POST['order']['0']['dir']=='asc')
$this->db->order_by('substring(numeroregisto,-2,2) ASC');
elseif($_POST['order']['0']['column']=='0' && $_POST['order']['0']['dir']=='desc')
$this->db->order_by('substring(numeroregisto,-2,2) DESC');
else
$this->db->order_by($this->column_order_registos[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
else if(isset($this->order_registos))
$order_registos = $this->order_registos;
$this->db->order_by('substring(numeroregisto,-2,2) ASC');
【讨论】:
以上是关于具有两个规则的数据表表顺序的主要内容,如果未能解决你的问题,请参考以下文章
SqlSever基础 在指定的数据库中添加一个表(具有两个列),设置列是否做主键