如何理解select两级联动查询
更新:HHH   时间:2023-1-7


本篇文章给大家分享的是有关如何理解select两级联动查询,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

    写故障管理系统时,根据主类-平台进行故障搜索,需要用到select两级联动查询

    前端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">请选择主类</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">请选择平台</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'>"+"请选择平台"+"</option>");
                  $.each(platform,function(i){
                  $("#se_platform").append("<option "+"value=\""+platform[i]+"\">"+platform[i]+"</option>")
                     })
              }
          })
      })})
</script>

django后端view

def search(request,page):
    """
    搜索view,接收前端传来的主类名,匹配数据库获得数据,返回给前端,自定义分页
    """
    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两级联动查询,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注天达云行业资讯频道。

返回开发技术教程...