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条)

报告相同问题?