【问题标题】:reinitialize datatables with external form data (server-side django app)使用外部表单数据重新初始化数据表(服务器端 django 应用程序)
【发布时间】:2013-07-18 04:42:12
【问题描述】:

我正在开发一个 django 网络应用程序,并成功实现了出色的 jQuery 数据表,以通过服务器端处理来呈现我的数据。我无权访问原始文件(这是我工作场所的一个项目,他们无法访问互联网,我现在在家),但它看起来像这样:

模板:

<script type="text/javascript">
    $(document).ready(function() {
    $('#example').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "/user_table/"
    } );
} );

</script>


<table id='example'>
    <thead>
        <th>name</th>
        <th>state</th>
        <th>email</th>
    </thead>
    <tbody>
        <td colspan="3" class="dataTables_empty">Loading data from server</td>
    </tbody>

</table>

服务器端:

def main_view(request):
    return render_to_response('index.html')

def datatables_view(request):
    start = request.GET['iDisplayStart']
    length = request.GET['iDisplayLength']

    query = myUser.objects.all()

    if request.GET.has_key('sSearch'):
        # filtering...


    query = query[start:start+length]
    response = ["aaData": [(q.name, q.state, q.email) for q in query]]

    # return serialized data

再次,就像我提到的,我对此没有任何问题。集成工作正常。甚至比罚款好,真的。我喜欢它。

不过,我真正需要的是比数据表附带的默认过滤器更复杂的过滤器。对于我的工作,有非常具体的搜索类型非常有用。所以我有一个表格垂直显示在表格上方。假设它看起来像这样:

<form>
    <label for='name'>name:</label>
    <input type='text' name='name'></input>

    <label for'costumer'>costumer?</label>
    <input type='checkbox' name='costumer'></input>

    <select multiple="multiple">
        <option id='regular'>regular</option>    
        <option id='new'>new</option>
    </select>

    <input type='submit' value='filter!'> </input>
</form>

我希望当用户单击提交按钮时,它会发送表单数据并使用我的定制过滤重新初始化数据表。然后我想要另一个按钮来刷新和重新加载数据表并取消它正在发送的任何初始数据(就好像你刷新了页面,实际上没有)。

我对 javascript 不是很有经验,所以简单的解决方案是最好的,但非常感谢任何帮助。

【问题讨论】:

  • 你试过了吗?如果您没有尝试过,我们无能为力。并且通过一些代码,我们会更好地理解你想要什么。
  • 看看 ajax jquery 方法(post、get、load)和 jquery 序列化方法。
  • 遗憾的是,这是针对我的工作场所的,我们那里没有互联网,所以我无法上传我的代码。但我认为这没有必要,我试图理解一些非常基本的东西 -如何做到这一点:stackoverflow.com/questions/13020629/…,但具有从表单收集数据的功能
  • 拜托了!任何人?我找不到关于这类事情的任何信息(或者我找到了一些东西但不理解)

标签: ajax django datatables


【解决方案1】:

我解决了!终于……

我使用了jquery-datatables-column-filter 插件。它很体面且易于使用,他们的网站how to use an external form 上有一个示例。确实,我实际上并没有过滤特定的列,但是由于我使用的是服务器端,所以这并不重要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 2018-07-18
    相关资源
    最近更新 更多