溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何理解select兩級(jí)聯(lián)動(dòng)查詢

發(fā)布時(shí)間:2021-11-25 22:38:56 來源:億速云 閱讀:136 作者:柒染 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)如何理解select兩級(jí)聯(lián)動(dòng)查詢,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

    寫故障管理系統(tǒng)時(shí),根據(jù)主類-平臺(tái)進(jìn)行故障搜索,需要用到select兩級(jí)聯(lián)動(dòng)查詢

    前端html    

<form id="tab" class="form-inline" role="form" action='/search/' method='GET' >
        <!--<div class="panel-heading">-->
        <div class="form-group">
        <select class="form-control" name="Program" id="se_program">
        <option value="test_program">請(qǐng)選擇主類</option>
        {% for val  in search %}
            <option value="{{ val }}" {% if val == query %}selected{% endif %}>{{ val }}</option>
        {% endfor %}
        </select>
        <select class="form-control" name="PlatForm" id="se_platform">
        <option value="test_platform">請(qǐng)選擇平臺(tái)</option>
        </select>
        </div>
         <button class="btn btn-primary" type="submit"><i class="fa fa-save"></i>search</button>
        <!--</div>-->
        </form>
<script type="application/javascript">
    $(document).ready(function() {
      $('#se_program').change(function () {
          se_program = $('#se_program').val();
          data = {se_program: se_program};
          $.ajax({
              url:"/liandong/",
              data:data,
              type:'post',
                  success: function(platform){
                  $("#se_platform").empty();
                  $("#se_platform").append("<option value='test_platform'>"+"請(qǐng)選擇平臺(tái)"+"</option>");
                  $.each(platform,function(i){
                  $("#se_platform").append("<option "+"value=\""+platform[i]+"\">"+platform[i]+"</option>")
                     })
              }
          })
      })})
</script>

django后端view

def search(request,page):
    """
    搜索view,接收前端傳來的主類名,匹配數(shù)據(jù)庫獲得數(shù)據(jù),返回給前端,自定義分頁
    """
    check_Program=request.GET['Program']
    check_PlatForm=request.GET['PlatForm']
    check_Program=check_Program.encode("utf-8")
    check_PlatForm=check_PlatForm.encode("utf-8")
    if check_Program == 'test_program':
        return HttpResponseRedirect('/index/')
    if check_Program != 'test_program' and check_PlatForm=='test_platform':        
        ch_result = FaultReports.objects.filter(Program = check_Program)
        plat=FaultReports.objects.values_list('PlatForm',flat=True).filter(Program = check_Program)
        plat=set(plat)
        if ch_result:
            islogin=False
            page=gb001.try_int(page, 1)
            totalitems=FaultReports.objects.filter(Program = check_Program).count()
            pageInofobj=html.PageInfo(page,totalitems)
            pager=html.Search_Program_page('/search/',page,pageInofobj.TotalPage(),check_Program)
            all_data=FaultReports.objects.filter(Program = check_Program).order_by("-id")[pageInofobj.From():pageInofobj.To()]
            data=FaultReports.objects.values_list("Program",flat=True)
            data=set(data)
            try:
                islogin=request.session['islogin']
            except Exception,e:
                pass
            if islogin:
                name =request.session['username']
                return render_to_response('search.html',{'login_user':name,'pager':pager,'result':all_data,'search':data,'query':check_Program,'plat':plat})
            else:
                return HttpResponseRedirect('/login/')
    else:
        ch_result = FaultReports.objects.filter(Program = check_Program,PlatForm=check_PlatForm)
        plat=FaultReports.objects.values_list('PlatForm',flat=True).filter(Program = check_Program)
        plat=set(plat)
        print plat
        if ch_result:
            islogin=False
            page=gb001.try_int(page, 1)
            totalitems=FaultReports.objects.filter(Program = check_Program,PlatForm=check_PlatForm).count()
            pageInofobj=html.PageInfo(page,totalitems)
            pager=html.Search_Program_page('/search/',page,pageInofobj.TotalPage(),check_Program,check_PlatForm)
            all_data=FaultReports.objects.filter(Program = check_Program,PlatForm=check_PlatForm).order_by("-id")[pageInofobj.From():pageInofobj.To()]
            data=FaultReports.objects.values_list("Program",flat=True)
            data=set(data)
            try:
                islogin=request.session['islogin']
            except Exception,e:
                pass
            if islogin:
                name =request.session['username']
                return render_to_response('search.html',{'login_user':name,'pager':pager,'result':all_data,'search':data,'query':check_Program,'query_platform':check_PlatForm,'plat':plat})
            else:
                return HttpResponseRedirect('/login/')
def liandong(request):
    if request.method == 'POST':
        data=request.POST.get('se_program')
        print request.POST
        PlatFormList=[]
        contents = FaultReports.objects.filter(Program=data)
        for line in contents:
            if PlatFormList.count(line.PlatForm) ==0:
                PlatFormList.append(line.PlatForm)
        platform = PlatFormList
        #platform=set(platform)
        #print platform
        return HttpResponse(json.dumps(platform),content_type="application/json")

以上就是如何理解select兩級(jí)聯(lián)動(dòng)查詢,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI