dth62818 2014-06-03 15:51
浏览 68
已采纳

PHP / Datatables:数字按文本排序

I am using Datatables to style a table that I fill with PHP.

Everything works as intended except for a column containing amounts. If I click on the Datatables column header for this column in order to sort the table by this column it sorts it but only as text instead of numbers so that 20 appears before 3 etc.

Is there a way I can prevent this in PHP by changing the format ?

Example cell:

<td class="colAmount ralign"><?php echo number_format(doubleval($requests->amount), 2, ".", ","); ?></td>

If an amount in my database ($requests->amount) is e.g. 100000 then the above would format this as 100,000.00 as I need to display it with the thousands and decimal separators - this is then probably interpreted as text.

Many thanks in advance, Tim.

  • 写回答

2条回答 默认 最新

  • doufan9290 2014-06-04 06:56
    关注

    You can use Datatables Orthogonal Data feature for this. This way, you can use different formats for display and sorting. There are two approaches here, I would go for the HTML 5 style, which uses data attributes (from the example there):

    <tr>
        <td data-search="Tiger Nixon">T. Nixon</td>
        <td>System Architect</td>
        <td>Edinburgh</td>
        <td>61</td>
        <td data-order="1303682400">Mon 25th Apr 11</td>
        <td data-order="3120">$3,120/m</td>
    </tr>
    

    In this example the data for the last two columns would be sorted using the data-order attributes. There should be no further configuration needed, Datatables should detect these attributes automatically.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配