datable ajax进行函数运算,并通过post传递函数参数的方法

def citypowerdisplay(request):

pdetailcolumns = ['局站名称', '用电起始日期', '用电截止日期', '月均支付金额变动图', '总额',
                         '本次日均成本', "同比日均成本增长", "环比日均成本增长"]
pdetailjs = []
for x in pdetailcolumns:
    temp = {}
    temp["data"] = x
    pdetailjs.append(temp)
pdetailjs = json.dumps(pdetailjs)


# 电量分析
ndetailcolumns = ['局站名称', '用电起始日期', '用电截止日期', '月均用电量变动图', "结算电量",
                     "本次日均电量", "同比日均电量增长", "环比日均电量增长"]
ndetailjs = []
for x in ndetailcolumns:
    temp = {}
    temp["data"] = x
    ndetailjs.append(temp)
ndetailjs = json.dumps(ndetailjs)


# 实际单价分析
sdetailcolumns = ['局站名称', '用电起始日期', '用电截止日期', '月均实际单价变动图', "合同电价",
                     "本次电费单价", "同比电费单价增长", "环比电费单价增长"]
sdetailjs = []
for x in sdetailcolumns:
    temp = {}
    temp["data"] = x
    sdetailjs.append(temp)
sdetailjs = json.dumps(sdetailjs)

return render(request, 'SimpleSite-Siteana-citys.html',
              {'pdetailcolumns': pdetailcolumns, 'pdetailjs': pdetailjs,
               'ndetailcolumns': ndetailcolumns, 'ndetailjs': ndetailjs,
               'sdetailcolumns': sdetailcolumns, 'sdetailjs': sdetailjs,
               })

@csrf_exempt
def pdetail(request):
if request.method == “POST”:
print(request,‘ooooooooo’)
city=request.POST.get(‘city’)
print(city,‘ppppppppppppppppppppppppppppppppppp’)
orgdetail=getorgdetail(city)
orgdetail.fillna(‘0’,inplace=True)
# 能耗分析
all_result = orgdetail[[‘局站名称’, ‘用电起始日期’, ‘用电截止日期’, ‘月均支付金额变动图’, ‘总额’,
‘本次日均成本’, “同比日均成本增长”, “环比日均成本增长”]]

        # 数据条数
    recordsTotal = len(all_result)
    recordsFiltered = recordsTotal
        # 第一条数据的起始位置
    start = int(request.POST['start'])
        # 每页显示的长度,默认为10
    length = int(request.POST['length'])
        # 计数器,确保ajax从服务器返回是对应的
    draw = int(request.POST['draw'])
        # 全局收索条件
    new_search = request.POST['search[value]']
        # 排序列的序号
    new_order= request.POST['order[0][column]']
        # 排序列名
    by_name = request.POST['columns[{0}][data]'.format(new_order)]
        # 排序类型,升序降序
    fun_order = request.POST['order[0][dir]']
        # 排序开启,匹配表格列
    if by_name:
            if fun_order == "asc":
                all_result = all_result.sort_values(by_name)
            else:
                all_result = all_result.sort_values("{0}".format(by_name))
        # 模糊查询,包含内容就查询
    if new_search:
        all_result = all_result[(all_result.局站名称.str.contains(new_search)) | (all_result.用电起始日期==new_search)]
        recordsTotal = len(all_result)
        recordsFiltered = recordsTotal

        # 获取首页的数据
    datas = all_result[start:(start+length)]
        # 转为字典
    resp = datas.to_dict('records')
    print(resp)
    # resp=json.dumps(resp)
    # resp

    #返回计数,总条数,返回数据
    result = {
        'draw': draw,
        'recordsTotal': recordsTotal,
        'recordsFiltered': recordsFiltered,
        'data': resp,
        }
    print('oooodoodosjs')
    print(result)
    # result=json.loads(result)
    # print(result)
    return HttpResponse(json.dumps(result), content_type="application/json")

1、citypowerdisplay为主函数,用于构造页面的数据(定义列名)
2、pdetail为ajax所指向函数用于生成数据
3、 “ajax”:{
“type”: ‘POST’ ,
“url”: ‘pdetail’,
“dataType”: ‘json’,
// data :JSON.stringify({datumId:1}) ,
“data”:{‘city’:“铜陵”}
},
用于指定url进行函数运算,
同时给函数通过data传递参数
4、对于传递的post参数,使用django的city=request.POST.get(‘city’)方法获取

你可能感兴趣的:(gis,sql,web,json,javascript,前端)