【问题标题】:jqGrid toolbar search and DjangojqGrid 工具栏搜索和 Django
【发布时间】:2014-07-30 17:01:53
【问题描述】:

我正在使用 jqGrid 添加工具栏搜索,如工具栏搜索示例中的示例 并且已经实现了基础和高级示例,例如站点教程,我的页面就像this 但我不知道如何在 Django 中使用此功能!!!我的网格顶部的那些输入类型的名称是什么!??
例子不清楚!你能帮助我吗 ?? [这是我在 github 上的源代码][4]

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index Page</title>
    <!-- external scripts -->

    <!-- jQuery & Bootstrap -->
    <script type="text/javascript" src="{% static "js/jquery.js" %}"/></script>
        <script type="text/javascript" src="{% static "js/jquery.jqGrid.min.js" %}"/></script>
    <link rel="stylesheet" type="text/css" media="all" href="{% static "css/bootstrap.min.css?id=1" %}"/>
    <script type="text/javascript" src="{% static "js/bootstrap.min.js" %}"/></script>
    <!-- jQuery & Bootstrap -->

    <!-- jqGrid -->
    <link rel="stylesheet" type="text/css" media="screen" href="{% static "css/ui.jqgrid.css" %}" />
    <link rel="stylesheet" type="text/css" media="screen" href="{% static "css/ui-lightness/jquery-ui.min.css" %}" />
    <script type="text/javascript" src="{% static "js/grid.locale-en.js" %}"/></script>
    <!-- jqGrid -->

    <!-- own implemented scripts -->
    <!-- <script type="text/javascript" src="{% static "js/script.js" %}"/></script>  -->
    <!-- own implemented scripts -->

    <!-- external scripts-->

    <script type="text/javascript">
    $(function () {
        $("#list").jqGrid({
            url: "http://localhost:8000/getdata",
            datatype: "json",
            mtype: "GET",
            colNames: ["id", "name", "english_title", "capacity"],
            colModel: [
            { name: "id", index:"id", width: 55 },
            { name: "name", width: 80 },
            { name: "english_title", width: 130, align: "right" },
            { name: "capacity", width: 80, align: "right" },
            ],
            rowNum:10,
            rowList:[10,20,30],
            pager: '#pager',
            loadonce:true,
            sortname: 'id',
            viewrecords: true,
            sortorder: "desc",
            rownumbers: true,
            rownumWidth: 40,
            gridview: true,
            multiselect: false,
            caption: "Rooms",
            onSelectRow: function(ids) {
                if(ids == null) {
                    ids=0;
                    if(jQuery("#list_d").jqGrid('getGridParam','records') >0 )
                    {

                        console.log(ids);
                        jQuery("#list_d").jqGrid('setGridParam',{url:"getpricelist?q=1&id="+ids,page:1}).trigger('reloadGrid');

                    }
                } else {
                    console.log(ids);
                    jQuery("#list_d").jqGrid('setGridParam',{url:"getpricelist?q=1&id="+ids,page:1}).trigger('reloadGrid');
                jQuery("#list_d").jqGrid('setCaption',"Price Detail of room : "+ids)

                }
    }
    });
    jQuery("#list").jqGrid('navGrid','#pager',{del:false,add:false,edit:false,search:false});
  jQuery("#list").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false});


    jQuery("#list_d").jqGrid({
        height: 100,
    width:345,
        url:'getpricelist?q=1&id=2',
        datatype: "json",
        colNames:['from','to', 'price'],
        colModel:[
        {name:'from',index:'from', width:100},
        {name:'to',index:'to', width:100},
        {name:'price',index:'price', width:80, align:"right"},
        ],
        rowNum:5,
        rowList:[5,10,20],
        pager: '#pager_d',
        sortname: 'item',
        viewrecords: true,
        sortorder: "asc",
        multiselect: false,
        caption:"Price Detail"
    }).navGrid('#pager_d',{add:false,edit:false,del:false});

        }); 

    </script>

    </head>
    <body>

        <table id="list"><tr><td></td></tr></table> 
        <div id="pager"></div> 

        <table id="list_d"></table>
        <div id="pager_d"></div>
    </body>
</html>

models.py:

from django.db import models
from datetime import datetime


class room_type(models.Model):
    id = models.IntegerField(primary_key = True)
    code = models.CharField(max_length = 40)
    name = models.CharField(max_length= 40 )
    title = models.CharField(max_length = 40)
    english_title = models.CharField(max_length=40)
    capacity = models.IntegerField()
    extra_capacity = models.IntegerField()
    description = models.CharField(max_length=255)

    class Meta:
        db_table = 'room_types'
    def __unicode__(self):
        return u'%d' % (self.id)


class room_icon(models.Model):
    id = models.IntegerField(primary_key = True)
    status  =  models.IntegerField()
    color_of_icon =  models.CharField(max_length=40)
    path_of_icon  =  models.CharField(max_length=255)

    #foreign_key : a room has only one icon
    rt_id = models.ForeignKey(room_type)
    class Meta:
        db_table = 'room_icons'
    def __unicode__(self):
        return u'%d' % (self.id)


class attachment(models.Model):

    id = models.IntegerField(primary_key = True)
    path_of_pic = models.CharField(max_length=255)

    #foreign key : a room has many images
    rt_id = models.ForeignKey(room_type)
    class Meta:
        db_table = 'attachments'
    def __unicode__(self):
        return u'%d' % (self.id)

views.py:

from django.shortcuts import render
from django.utils import simplejson
from django.http import HttpResponse
from rooms.models import *
from django.db.models import Q
from django.core import serializers


def index(request):
    return render(request, 'index.html')

def getdata(request):
    data=room_type.objects.all()
    json=[]
    for o in data:
        json.append({'id':o.id, 'name':o.name, 'english_title':o.english_title, 'capacity':o.capacity})

    return HttpResponse(simplejson.dumps(json), mimetype='application/json',content_type='application/json' )


def getpricelist(request):

    requested_room_id = request.GET.get('id', '')

    room = room_type.objects.get(id = requested_room_id)
    price_list_set = room.price_list_set.all()

    json=[]
    for price_list in price_list_set:
        json.append({'from':price_list.from_date, 'to':price_list.to_date, 'price':price_list.price})


    return HttpResponse(simplejson.dumps(json), mimetype='application/json',content_type='application/json' )

    class price_list(models.Model):

        id = models.IntegerField(primary_key = True)
        from_date = models.CharField(max_length=10)
        to_date   = models.CharField(max_length=10)
        price = models.IntegerField()
        #foreign key : a room has a pricee list
        rt_id = models.ForeignKey(room_type)
        class Meta:
            db_table = 'price_lists'
        def __unicode__(self):
            return u'%d' % (self.id)

最终编辑:
如果我编辑了 html 文件,现在所有文件都正确,如果您需要 jqGrid 示例的 django 源代码(工具栏搜索、高级主详细信息和加载 json 数据),您可以使用我的,工作正常;)! ! Tnx 再次@Oleg 提供很大帮助;)

【问题讨论】:

  • 在此处发布您的代码,而不是提供 github url。您的 github 存储库明天可能不再存在,但这个问题永远存在。因此,这个问题包含所有相关代码并且本身是完整的,这一点很重要。

标签: jquery django search jqgrid


【解决方案1】:

我不是 Django 开发人员,但服务器端代码看起来像是返回了 all 未排序项的数组。在这种情况下,您应该使用 jqGrid 的 loadonce: true 选项。在这种情况下,jqGrid 将所有数据保存在内部参数data_index 中,并在第一次加载后将datatype 选项更改为"local"。之后,搜索在客户端工作,您无需对服务器代码进行任何更改。

如果您使用loadonce: true 选项并且您需要从服务器重新加载数据(例如在更改url 之后)那么您必须将datatype 重置为其原始值datatype: "json"

$("#list_d").jqGrid("setGridParam", {
    url: "getpricelist?q=1&id=" + encodeURIComponent(ids),
    page: 1,
    datatype: "json"
}).trigger("reloadGrid");

我建议考虑在第二个(详细信息网格)中另外使用datatype: "local"。当前代码使用url:'getpricelist?q=1&amp;id=2', datatype: "json"。所以第二个网格将在第一个网格中选择的一行之前填充。使用 datatype: "local" 会阻止加载。

我建议您在两个网格中使用gridview: trueautoencode: true 选项,并将key: true 属性添加到第一个网格中"id" 列的定义中。如果您这样做,那么&lt;tr&gt; 元素(网格的行)中的id 将被分配给id 列中的值。它可以简化编辑操作。

最后一句话:jqGrid 将 always id 属性(称为 rowid)分配给网格的每一行。 id 在整个页面中必须是唯一的。如果您有多个网格,则网格中可能存在 id 冲突。为了确保您没有问题,您可以考虑为不同的网格(来自网格之一)使用不同的idPrefix 参数(例如idPrefix: "g1_"idPrefix: "g2_")。在这种情况下,rowid 值将从 idPrefix 和从您的数据分配的 id 构建(例如第一个网格中的 id 列)。如果您需要剪切前缀(例如将其用作 URL 中的参数),您可以使用$.jgrid.stripPref 方法。例如,您可以在构建详细网格的url 时使用$.jgrid.stripPref($(this).jqGrid("getGridParam", "idPrefix"), ids) 而不是id

【讨论】:

  • 非常感谢您的帮助! :) 问题是我忘记了“loadonce:ture”!!
猜你喜欢
  • 2012-06-04
  • 1970-01-01
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多