jQuery DataTables && Django serializer

jQuery DataTables

  • https://www.datatables.net

本文参考的官方示例

  • http://datatables.net/release-datatables/examples/ajax/objects.html

关于Python序列化

  • http://www.cnblogs.com/linyawen/archive/2012/03/22/2411381.html

Django中的Models序列化

  • http://www.weiguda.com/blog/19/

Python的super用法

  • http://www.cnblogs.com/dkblog/archive/2011/02/24/1980654.html

models.py

from django.db import models
from django.contrib import admin

# Create your models here.
class BankData(models.Model):
    acc_no = models.CharField(max_length=45,primary_key=True)
    name   = models.CharField(max_length=45,blank=True,default=‘‘)
    bank   = models.CharField(max_length=45,blank=True,default=‘‘)

class BankDataAdmin(admin.ModelAdmin):
    list_display = (‘acc_no‘,)

admin.site.register(BankData, BankDataAdmin)

serializers.py

from rest_framework import serializers
from myapp.models import BankData

class BankDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = BankData
        fields = (‘acc_no‘,‘name‘,‘bank‘)

views.py

#! /usr/bin/env python
# -*- coding:utf-8 -*-

from myapp.models import BankData
from django.http import HttpResponse
from myapp.serializers import BankDataSerializer
from rest_framework.renderers import JSONRenderer

#json
class JSONResponse(HttpResponse):
    def __init__(self, data, **kwargs):
        content = JSONRenderer().render(data)
        kwargs[‘content_type‘] = ‘application/json‘
        super(JSONResponse, self).__init__(content,**kwargs)

#jsonp
class JSONPResponse(HttpResponse):
    def __init__(self, data, callback=‘callback‘,**kwargs):
        content = JSONRenderer().render(data)
        jsonp_content = "(" + callback+ "(" + content + ")"
        kwargs[‘content_type‘] = ‘application/json‘
        super(JSONPResponse, self).__init__(jsonp_content,**kwargs)

#json for DataTables
class JSONDTResponse(HttpResponse):
    def __init__(self, data, **kwargs):
        content = JSONRenderer().render(data)
        dt_content = "{\"data\":" + content + "}"
        kwargs[‘content_type‘] = ‘application/json‘
        super(JSONDTResponse, self).__init__(dt_content,**kwargs)

def datas(request):
    bds = BankData.objects.all()
    serializer = BankDataSerializer(bds,many=True)
    return JSONDTResponse(serializer.data)

bank_data.html

<!DOCTYPE html>
<head>
    <title>银行信息</title>
    <link rel="stylesheet" type="text/css" href="/static/css/jquery.dataTables.min.css">
    <script type="text/javascript" src="/static/js/jquery.js"></script>
    <script type="text/javascript" src="/static/js/jquery.dataTables.min.js"></script>
</head>
<body>
<script>
    $(document).ready(function() {
        $(‘#example‘).dataTable( {
            "bProcessing":true,
            "ajax":‘/myapp/datas/‘,
            "columns":
            [
                {‘data‘:‘acc_no‘},
                {‘data‘:‘name‘},
                {‘data‘:‘bank‘},
            ]
        });
    });
</script>
<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>账号</th>
            <th>姓名</th>
            <th>银行</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>账号</th>
            <th>姓名</th>
            <th>银行</th>
        </tr>
    </tfoot>
</table>
</body>
时间: 2025-01-02 18:46:43

jQuery DataTables && Django serializer的相关文章

jquery.dataTables动态列

jquery.dataTables  版本1.10.7 直接上代码: 0.table <table id="popReportTable"> <thead> <tr id="trId"> <td>显示名,这里可以给tr一个id,然后用$("#trId").html("列名...")来改变显示列名</td> </tr> </thead> <

Jquery DataTables 获取表格数据及行数据

注意table变量是 1.jQuery DataTables 行号获取 $("#example tbody tr").on("click", function() {var index = $(this).context._DT_RowIndex; //行号}); 2.获取表格所有数据 function getTableContent(){ var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行,nTrs[i]表示第i行t

jquery.dataTables插件使用例子详解

DataTables是一个jQuery的表格插件.这是一个高度灵活的工具,依据的基础逐步增强,这将增加先进的互动控制,支持任何HTML表格 效果图 代码 <!doctype html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>jquery.dataTables插件</title>     <link rel

jquery datatables设置每列不同的颜色

html代码如下,主要通过设置aoColumnDefs的属性, aTargets表示具体哪一列.希望对大家有用啊,自己也是查了好多外文网站,最后才搞定这种办法 引用常用的库是: <script type="text/javascript" language="javascript" src="js/jquery.js"></script> <script type="text/javascript"

jQuery DataTables and ASP.NET MVC Integration

part 1 : http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part Introduction The jQuery DataTables plug-in is an excellent client-side component that can be used to create rich-functional tables in the web brows

JQuery Datatables

最近项目中用了Bootstrap的样式风格,控件用了JQuery Datatables,主要有几下几点目标: 实现BootStrap风格的table,使用Ajax获取数据,并有勾选项 可以实现全选,单击勾选 双击行弹出编辑对话框,点击保存使用Ajax传送到服务端保存 实现批量删除 分页操作(下次再记录) 查询操作(下次再记录) 排序操作(下次再记录) 第一部分: 编写一个只有thead的table,tbody会在JS函数中自动生成. HMTL代码如下: 1 <table id="examp

jQuery DataTables Plugin Meets C#

Over the weekend, I was doing some work on the internal CMS we use over at eagleenvision.net and I wanted to scrap my custom table implementation for a table system that would use JSON to return data rather than have the data be statically allocated

jquery datatables 的常见参数配置

1:导入包: URL:http://www.datatables.net/ 分别导入css和js文件 Html代码   <</span>style type="text/css" title="currentStyle"> @import "css/demo_page.css"; @import "css/demo_table.css"; @import "css/demo_table_jui

jquery DataTables表格插件的使用(网页数据表格化及分页显示)

DataTables - 非常强大的 jQuery 表格插件,可变宽页码浏览,现场过滤. 多列排序,自动探测数据类型,智能列宽,可从几乎任何数据源获取数据. 那么在Bootstrap下如何使用DataTables: 只需要在html或ejs下引用写好的样式表css和脚本文件js,对应代码如下: 1 <head> 2 <link href="http://apps.bdimg.com/libs/bootstrap/3.0.3/css/bootstrap.min.css"