Django应用过滤器重新加载DataTable

时间:2017-09-07 18:27:22

标签: javascript jquery ajax django python-3.x

所以我有几个过滤器,用户可以应用这些过滤器来改变表格中显示的信息。我有一个Ajax调用,当用户点击其中一个过滤器时会触发该调用:

        $.ajax({
            url: '/fund_monitor/fund_directory',
            type: 'GET',
            data:JSON.stringify({
                filter_dict: filter_dict
            }),
            success: function (data) {
            }
        });

非常简单的东西。接下来,我的Class Based View将投放此Ajax Call

def get(self, request, *args, **kwargs):
    filtered_ids = []
    if request.is_ajax():
        #filter_dict = request.GET.getlist('filter_dict')
        request_data = json.dumps(request.GET)
        request_data = json.loads(request_data)
        for each in request_data:
            each = json.loads(each) # Just for parsing
            for key, value in each['filter_dict'].items():
                if key == 'Structure':
                    objects = Fund.objects.filter(structure__in=value).values('fund_account_id').distinct()
                    for fund_account_id in objects:
                        filtered_ids.append(fund_account_id['fund_account_id'])
                elif key == 'Fund Class':
                    objects = Fund.objects.filter(fund_class__in=value).values('fund_account_id').distinct()
                    for fund_account_id in objects:
                        filtered_ids.append(fund_account_id['fund_account_id'])
                elif key == 'Designated Broker':
                    objects = Fund.objects.filter(designated_broker__in=value).values('fund_account_id').distinct()
                    for fund_account_id in objects:
                        filtered_ids.append(fund_account_id['fund_account_id'])

        filtered_ids = set(filtered_ids)
        context = self.get_context_data(filtered_ids, **kwargs)
        self.template_name = 'fund_monitor/fundaccount_table_list.html'
        return self.render_to_response(context)
    context = self.get_context_data(**kwargs)
    return self.render_to_response(context)

因此,在我收集了要在表格中显示的所有ids之后,我将其传递给get_context_data函数,该函数获取所有这些ids并获取所需的信息在ids

def get_context_data(self, filtered_ids=None, **kwargs):
    context = super(__class__, self).get_context_data(**kwargs)
    context['nbar'] = 'fund_monitor'
    context['sbar'] = 'fundaccount_list'
    context['transmission'] = 3

    if filtered_ids:
        context['fundaccount_list'] = self.model.objects.filter(id__in=filtered_ids)
    else:
        context['fundaccount_list'] = self.model.objects.all()
    url_dict = defaultdict(list)
    for fund in context['fundaccount_list']:
        end_date = str(NAV.objects.filter(fund= fund.id, transmission=3).values('valuation_period_end_date').order_by('-valuation_period_end_date')[0]['valuation_period_end_date'])
        start_date = str(NAV.objects.filter(fund= fund.id, transmission=3).values('valuation_period_end_date').order_by('-valuation_period_end_date')[1]['valuation_period_end_date'])

        url_dict[fund.account_description].extend([fund.id,context['transmission'],
                                                   start_date, end_date])
    context['obj_items'] = dict(url_dict)
    return context

所以在它返回上下文之后,在get函数中,行:

self.template_name = 'fund_monitor/fundaccount_table_list.html'   
return self.render_to_response(context)

应该使用修改后的template_name运行。此template_name基本上只包含表HTML。基本上,我只希望表刷新自己,而不是整个页面...但是,由于过滤器context正在更新,表没有重新加载。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

简单的解决方案:

   $.ajax({
        url: '/fund_monitor/fund_directory',
        type: 'GET',
        data:JSON.stringify({
            filter_dict: filter_dict
        }),
        success: function (data) {
            $("#myTable").html(data); // Add this line to your based on your table div
        }
    });