Django 应用过滤器重新加载数据表
Posted
技术标签:
【中文标题】Django 应用过滤器重新加载数据表【英文标题】:Django Applying Filters To Reload DataTable 【发布时间】:2018-02-16 13:54:50 【问题描述】:所以我有几个过滤器,用户可以应用它们来更改表格中显示的信息。我有一个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】:简单的解决方案:
$.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
);
【讨论】:
以上是关于Django 应用过滤器重新加载数据表的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Ajax 加载过滤后的数据,而无需在 laravel 中重新加载整个页面