dataTables 서버측 페이지 나누기 서버사이드(for django)

4839 단어 django
html
    
ip_address host_name lease_expiry_time lease_duration time_span

js
    $(document).ready(function () {
        $("#tb").DataTable({
            "paging": true,
            "searching": true,
            "processing": true,
            "serverSide": true,
//            "destroy": true,
//            "pageLength": 10,
            "ordering": false,
//            "renderer": "bootstrap",//    :Bootstrap jquery-ui
//            "pagingType": "simple_numbers",//    :simple,simple_numbers,full,full_numbers
//            "autoWidth": true,
//            "stateSave": true,//      , comTable.fnDraw(false);    
            "ajax": {
                "url": "/example/paging/paging/",
                "type": "post"
            },
//            "columns": [          //    data   ,       
//               { "data": "id" },
//               { "data": "name" },
//               { "data": "dept" },
//               { "data": "phone" }
//            ]
            "columnDefs": [  //    
                {
                    "render": function (data, type, row) { //data:      row:    
                        if (data == "10.199.50.125") {
                            return "" + data + "";
                        } else {
                            return data;
                        }
                    },
                    "targets": 0 //     
                },
                {
                    "render": function (data, type, row) {
                        if (row[0] == "10.199.50.139") {  //  IP       
                            return "" + data + "";
                        } else {
                            return data;
                        }
                    },
                    "targets": 1 //     
                }
            ]

        });
    });

urls.py
    #   :    url      url    
    url(r'^example/paging/$', paging.index),
    url(r'^example/paging/paging/$', paging.paging),

views.py
def index(request):
    return render(request, 'example/paging/body01.html')

#           
def paging(request):
    draw = int(request.POST.get('draw'))  #       
    start = int(request.POST.get('start'))  #     
    length = int(request.POST.get('length'))  #     
    search_key = request.POST.get('search[value]')  #      
    order_column = request.POST.get('order[0][column]')  #       
    order_column = request.POST.get('order[0][dir]')  #    :ase/desc

    # sql       ,    ,    
    # if search_key:
    #     result = query(search_key)
    # else:
    #     result = select_all()
    # data = result[start:start+length]
    # count = len(result)

    # sql   ,   
    if search_key:
        result, count = query(search_key)
        data = result[start:start + length]
    else:
        data = select_by_page(start, start + length)
        count = all_count()

    dic = {
        'draw': draw,
        'recordsTotal': count,
        'recordsFiltered': count,
        'data': data,
    }

    return HttpResponse(json.dumps(dic), content_type='application/json')


#   all
def select_all():
    cursor = connection.cursor()
    # Object of type 'datetime' is not JSON serializable , to_char  
    # ORA-00911: invalid character ,    
    sql = "select ip_address,host_name,to_char(lease_expiry_time),to_char(lease_duration),to_char(time_span) from example_paging order by ip_address"
    cursor.execute(sql)
    result = cursor.fetchall()
    cursor.close()
    return result

# sql   
def select_by_page(start, end):
    cursor = connection.cursor()
    # Object of type 'datetime' is not JSON serializable , to_char  
    # ORA-00911: invalid character ,    
    # oracle  , rownum    1      ,       rownum,   
    sql = """
        select * from
            (select ip_address,host_name,to_char(lease_expiry_time),to_char(lease_duration),to_char(time_span),rownum as rn
            from example_paging  order by ip_address )
        where rn>=%s and rn

참조 자료:
https://www.datatables.net/examples/server_side/post.html

좋은 웹페이지 즐겨찾기