DataTables組件ServerSide的用法

最先認識datatables是由於AdminLTEhtml

AdminLTE 是一個基於bootstrap3的後臺類網站模板,開源,也有收費版。做爲運維狗想作web前端是個很好的啓蒙前端

還有一個不錯的傢伙叫 easyui,但UI像是上古時代的產物,注重面子的仍是算了vue

DataTables 則是一個不錯的jq組件,除了AdminLTE外,在sbadmin之類的模板中也能看到python

感受一些前端的東西都說不清與後端交互的方法,包括我如今在用的layuiadmin,以及我在學習的vue-admin,仍是我我的打開文檔的姿式不對?web

不羅嗦了,直接上乾貨吧,先來看html及js怎麼寫ajax

<div id="divTable">
   <table id="table" class="table table-bordered" style="width:100%">
    <thead>
      <tr>
        <th>姓名</th>
        <th>帳號</th>
        <th>手機</th>
        <th>郵箱</th>
      </tr>
    </thead>
  </table>
</div>
<script>
// datatables 英翻中
var datatableLanguage = {
  "sProcessing": "載入中...",
  "sLengthMenu": "顯示 _MENU_ 項結果",
  "sZeroRecords": "沒有匹配結果",
  "sInfo": "顯示第 _START_ 至 _END_ 項結果,共 _TOTAL_ 項",
  "sInfoEmpty": "顯示第 0 至 0 項結果,共 0 項",
  "sInfoFiltered": "(由 _MAX_ 項結果過濾)",
  "sInfoPostFix": "",
  "sSearch": "搜索:",
  "sUrl": "",
  "sEmptyTable": "沒有對應的數據",
  "sLoadingRecords": "載入中...",
  "sInfoThousands": ",",
  "oPaginate": {
    "sFirst": "首頁",
    "sPrevious": "上頁",
    "sNext": "下頁",
    "sLast": "末頁"
  },
  "oAria": {
    "sSortAscending": ": 以升序排列此列",
    "sSortDescending": ": 以降序排列此列"
  }
}
// 表格初始化
var table = $("#table").DataTable({
    deferRender: true,
    lengthMenu: [ [10, 25, -1], [10, 25, "全部"] ],
    searchDelay: 500,
    pagingType: "simple_numbers",
    autoWidth: false,
    select: true,
    ordering: false,
    processing: true,
    serverSide: true,
    ajax: "{% url 'table_data_api' %}",
    language: datatableLanguage,
});
</script>

後端以django爲例django

if request.method == 'GET':
    kwargs = dict(request.GET)
    filter_args = {
        'draw': int(kwargs['draw'][0]),
        'start': int(kwargs['start'][0]),
        'length': int(kwargs['length'][0]),
        'search': kwargs['search[value]'][0]
    }
    return namedtuple('D2N', list(filter_args.keys()))(**filter_args)
raise HttpBadRequest('額,好像不是約定的datatable請求格式')

能夠看到datatable serverside只須要4個關鍵參數bootstrap

  1. draw:不知道幹什麼的,傳什麼返回什麼就對了
  2. start:開始位置
  3. length:數據長度(分頁)
  4. search:搜索內容

拿到參數後,就能夠着手數據生成的返回工做後端

# 獲取上面datatables的請求參數
datatable_args = get_datatable_args()

# 基礎過濾和排序
queryset = User.objects.filter(enable=True, delete=False)

# 處理表格搜索
if datatable_args.search != '':
    queryset = queryset.filter(
        Q(name__contains=datatable_args.search)
        | Q(account__contains=datatable_args.search)
    )

# 返回數據格式
ret = {
    'draw': datatable_args.draw,
    'recordsTotal': queryset.count(),  // 頁面上的共有*條數據
}

# 處理數據本體
data = []
if queryset.count() != 0:
    if datatable_args.length == -1:
        # length = -1 爲顯示全部,不分頁
        item_list = queryset
        
    else:
        # 按length要求處理分頁
        # Paginator 分頁函數的來源是 from django.core.paginator import Paginator
        p = Paginator(queryset, datatable_args.length)
        if datatable_args.start == 0:
            page_num = 1
        else:
            page_num = datatable_args.start / datatable_args.length + 1
        item_list = p.page(page_num).object_list

    # 開始生成表格內容
    for item in item_list:
        data.append(
            [
                item.name,
                item.account,
                item.mobile,
                item.email
            ]
        )
        
# 以datatables可識別格式返回,records開頭的兩個變量的做用爲:
# 「共 total_num 條數據,顯示 filter_num 條
# ret = {
#     'recordsFiltered': filter_num, 
#     'recordsTotal': total_num,
#     'data': data,
#     'draw': datatable_args.draw,
# }
ret.update(
    **{'recordsFiltered': queryset.count(), 'data': data}
)
return JsonResponse(ret)

好了,搞定api

相關文章
相關標籤/搜索