是否可以禁用数据表的默认自定义搜索构建器绘制?

问题描述 投票:0回答:0

我的 Django Web 应用程序使用具有服务器端处理和自定义搜索生成器的数据表。我正在尝试更改 URL 以包含来自 searchbuilder 的查询参数以用于添加书签。使用 ajaxSucesss settings.url 对象和 HTML History.pushState 方法,我可以获得必要的参数并将它们附加到 URL。问题是,每次发出 ajax 请求时,datatabels 都会添加一个“&&”,后跟请求的默认绘制。 有没有办法禁用默认数据表绘制?

在 Django html 模板中:

$( document ).on( "ajaxSuccess", function( event, xhr, settings ) {
    // collect everything in the URL after the "?"
    draw_string = settings.url.split("dt-json/?")[1]
    // split the original URL at the "&&" and keep only the first half.
    // the second half is the default dt draw
    let query_string = draw_string.split("&&")[0]
    // if the datatable is filtered.
    if (draw_string.split('&&').length === 2 && !draw_string.split('&&')[1].includes("draw=1&")) {
        // replace the string with the second draw
        query_string = draw_string.split('&&')[1];
    }
    url_params = "/?" + query_string
    // and replace the URL with the new URL without reloading the page.
    history.pushState(null, "", url_params)
} );

在视图中,处理ajax请求:

def query_string_to_dict(query_string_arg):
    # When adding params manually to the index page for bookmarking, 
    # datatables adds the params to the existing URL  by default which concatinates the to query strings with a '&&'.
    # To handle this we first split the string by '&&'.
    query_string = query_string_arg.split('&&')[0]
    # check if there are two queries (a.k.a double draw) AND the 2nd draw doen not have the 'draw=1&' sub-string which
    # represents the default query string added by the datatables
    if len(query_string_arg.split('&&')) == 2 and "draw=1&" not in query_string_arg.split('&&')[1]:
        # if there are two queries and the 2nd one does not have a 'draw=1&' consider 2nd draw as query string
        query_string = query_string_arg.split('&&')[1]
    query_string = query_string.replace('%5B', '[').replace('%5D', ']')
    query_dict = {}
    // convert utf-8 string to dict
    for parameter in query_string.split('&'):
        key_value_pair = parameter.split('=')
        key = key_value_pair[0]
        value = key_value_pair[1].replace('%3D', '')
        if value == '':
            value = '='
        if value == '%3C':
            value = '<='
        if value == '%3E':
            value = '>='
        if '[' in key:
            keys = key.split('[')
            for i in range(len(keys)):
                if ']' in keys[i]:
                    keys[i] = keys[i].replace(']', '')
            nested_dict = query_dict
            for k in keys[:-1]:
                if k not in nested_dict:
                    nested_dict[k] = {}
                nested_dict = nested_dict[k]
            nested_dict[keys[-1]] = value
        else:
            query_dict[key] = value
    return query_dict
jquery django ajax datatables bookmarks
© www.soinside.com 2019 - 2024. All rights reserved.