自然排序 Ajax Codeigniter 中的数据表

Natural sorting Ajax Datatables in Codeigniter

努力弄清楚如何使用 Codeigniter 活动记录在 AJAX 数据 table 中设置自然排序。 在大多数情况下,应该排序的字段只有数字......在其他情况下是一个字符串,因此 MySQL table 字段设置为 VARCHAR。

我需要自然地在数据tables中显示字段。

活动记录 Codeigniter 查询如下。

function list_all($limit,$start,$col,$dir)
{   
    $this->rmi_db->select (" 
        $this->table_dev.id,
        $this->table_dev.fl,
        $this->table_dev.mm,
        $this->table_dev.batch,
        $this->table_dev.n,
        $this->table_dev.ditta,
        $this->table_dev.tipo,
        $this->table_dev.costruzione,
        $this->table_dev.motori,
        $this->table_dev.nc,
        $this->table_dev.serie,
        $this->table_dev.ca,
        $this->table_dev.consegna,
        $this->table_dev.matr_usaf AS usaf,
        $this->table_dev.matr_usn AS usn,
        $this->table_dev.matr_caf AS caf,
        $this->table_dev.matr_raf AS raf,
        $this->table_dev.codici,
        $this->table_dev.note,
        $this->table_dev.reg_civili,
        $this->table_dev.matricola_civ,
        $this->table_dev.prima_reg,
        $this->table_dev.n_contratto,
        $this->table_dev.data_contratto,
        $this->table_dev.importo_contratto,
        $this->table_dev.note_contratto,
        $this->table_dev.f29,
        $this->table_dev.f30,     
        ");
    $this->rmi_db->from("$this->table_dev");
    $this->rmi_db->where("$this->table_dev.mm !=", "");
    $this->rmi_db->limit($limit, $start);
    $this->rmi_db->order_by($col, $dir);
    $query = $this->rmi_db->get();

    if($query->num_rows()>0)
    {
        return $query->result(); 
    }
    else
    {
        return null;
    }
}

mm 字段应该自然排序。我不知道如何以及是否有可能解决这个问题。 我尝试了本次讨论 solutions 中的解决方案,Bin 方式,但 select 无法正常工作(出现 500 服务器错误)

非常感谢您的帮助

使用Solution,试试下面的方法。它应该可以工作,但未经测试。

function list_all($limit,$start,$col,$dir)
{   
$this->rmi_db->select (" 
    $this->table_dev.id,
    $this->table_dev.fl,
    $this->table_dev.mm,
    $this->table_dev.mm, CAST($this->table_dev.mm as SIGNED) AS casted_column,//changed
    $this->table_dev.batch,
    $this->table_dev.n,
    $this->table_dev.ditta,
    $this->table_dev.tipo,
    $this->table_dev.costruzione,
    $this->table_dev.motori,
    $this->table_dev.nc,
    $this->table_dev.serie,
    $this->table_dev.ca,
    $this->table_dev.consegna,
    $this->table_dev.matr_usaf AS usaf,
    $this->table_dev.matr_usn AS usn,
    $this->table_dev.matr_caf AS caf,
    $this->table_dev.matr_raf AS raf,
    $this->table_dev.codici,
    $this->table_dev.note,
    $this->table_dev.reg_civili,
    $this->table_dev.matricola_civ,
    $this->table_dev.prima_reg,
    $this->table_dev.n_contratto,
    $this->table_dev.data_contratto,
    $this->table_dev.importo_contratto,
    $this->table_dev.note_contratto,
    $this->table_dev.f29,
    $this->table_dev.f30,     
    ");
$this->rmi_db->from("$this->table_dev");
$this->rmi_db->where("$this->table_dev.mm !=", "");
$this->rmi_db->limit($limit, $start);
$this->rmi_db->order_by($col, $dir);
$this->rmi_db->order_by('casted_column', 'ASC'); // changed
$this->rmi_db->order_by($this->table_dev.mm, 'ASC'); // changed
$query = $this->rmi_db->get(); //changed

if($query->num_rows()>0)
{
    return $query->result(); 
}
else
{
    return null;
}
}

如果您遇到任何问题,请发表评论