【问题标题】:Datatables ajax for sorting by different parameter用于按不同参数排序的数据表 ajax
【发布时间】:2015-03-02 15:03:40
【问题描述】:

我正在使用数据表 jquery 插件和 AJAX 方法。我的 AJAX 返回类似于以下内容:

[
["1463","Example title 1",{"display":"02\/03\/2015 12:15:00","timestamp":"1425297601"},1,"1",0,0,0,0,0,0,0,0],
["1462","Example title 2",{"display":"02\/03\/2015 11:45:00","timestamp":"1425295802"},1,"1",0,0,0,0,0,0,0,0],
["1461","Example title 3",{"display":"02\/03\/2015 11:30:00","timestamp":"1425295220"},1,"1",0,0,0,0,0,0,0,0]
]

它应该在字段 2 中显示第一个值但按第二个排序。当数据在页面加载时提供给表时,这可以正常工作,但不是在 ajax 调用中。

我的表格 HTML/脚本是:

<script type="text/javascript">
$(document).ready(function() {                
        $('#resultsLatestPosts').DataTable({
                dom: 'T<"clear">frtip',
                autoWidth: false,
                iDisplayLength: 50,
                scrollY: "420px",
                paging: false,
                tableTools: {sSwfPath:'/includes/js/DataTables-1.10.4/extensions/TableTools/swf/copy_csv_xls_pdf.swf',aButtons:["copy","xls","pdf"]},                
                ajax: 'index.php?ajax=1&function=getLatestPosts',
                ajaxDataProp: '',                                                      
        })
})
</script>

<table id="resultsLatestPosts" class="display">
  <thead>
  <tr>
  <th>Post Title</th>
  <th>Sent</th>
  <th>Accounts Sent to</th>
  <th>Posts Sent</th>
  <th>Social Leads</th>
  <th><img src="/images/icons/vendor_postdetails/download-icon-32.png" data-toggle="tooltip" data-placement="left" alt="Downloads" title="Downloads" /><span style="display:none">Downloads</span></th>
  <th><img src="/images/icons/vendor_postdetails/followers-icon-32.png" data-toggle="tooltip" data-placement="left" alt="Connections" title="Connections" /><span style="display:none">Connections</span></th><th><img src="/images/icons/vendor_postdetails/retweet-icon-32.png" data-toggle="tooltip" data-placement="left" alt="Retweets" title="Retweets" /><span style="display:none">Retweets</span></th>
  <th><img src="/images/icons/vendor_postdetails/like-icon-32.png" data-toggle="tooltip" data-placement="left" alt="Likes" title="Likes" /><span style="display:none">Likes</span></th>
  <th><img src="/images/icons/vendor_postdetails/favourite-icon-32.png" data-toggle="tooltip" data-placement="left" alt="Favourites" title="Favourites" /><span style="display:none">Favourites</span></th>
  <th><img src="/images/icons/vendor_postdetails/reply-icon-32.png" data-toggle="tooltip" data-placement="left" alt="Comments/Replies" title="Comments/Replies" /><span style="display:none">Comments/Replies</span></th>
  <th><img src="/images/icons/vendor_postdetails/extended-followers-icon-32.png" data-toggle="tooltip" data-placement="left" alt="Extended Reach" title="Extended Reach" /><span style="display:none">Extended Reach</span></th></tr>
  </thead>
</table>         

澄清一下。如果我的 json 返回:

["1463","Test post",{"render":{"display":"02\/03\/2015 12:15:00","filter":"1425297601"}},1,"1",0,0,0,0,0,0,0,0]

如何模仿此处的排序操作: https://datatables.net/reference/option/columns.render#object

请有人解释我缺少什么。谢谢。

【问题讨论】:

    标签: jquery datatables jquery-datatables


    【解决方案1】:

    在我看来,您好像误解了 render 应该在哪里。

    你应该改用:

    $('#resultsLatestPosts').DataTable({
                    ... snip
                    render: {
                          "filter": "filter",
                          "display": "display"
                           }                                                
                    });
    

    你的 JSON 应该是这样的:

    ["1463","Test post", "display":"02\/03\/2015 12:15:00","filter":"1425297601",1,"1",0,0,0,0,0,0,0,0]
    

    examples

    注意:这些都没有经过测试,我也没有使用过这部分数据表。

    【讨论】:

      猜你喜欢
      • 2017-12-11
      • 2012-05-14
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 2012-12-19
      • 1970-01-01
      • 2010-11-28
      • 2010-11-28
      相关资源
      最近更新 更多