将相关模型的方法分为jqgrid的单独列 [英] Taking method of the related model into a separate column of jqgrid

查看:86
本文介绍了将相关模型的方法分为jqgrid的单独列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django 1.4与django_gems包中的jqgrid包
以下代码试图通过连接名字和姓氏将虚拟列引入网格。但是,无法将关键字client_get_fullname'解析成字段失败。



有没有可以接受的方法来实现?



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

$ b class Client(models.Model):
first_name = models.CharField(max_length = 50)
last_name = models.CharField(max_length = 50)
def get_fullname self):
返回'%s%s'%(self.first_name,self.last_name)

from django_gems.jqgrid import JqGrid
class CarGrid(JqGrid):
queryset = Car.objects.all()
fields = ['number','client__get_fullname']

jqgrid config = {altRows:true,rowList:[10,25,50,100],sortname:id,viewrecords:true,autowidth forcefit:false,shrinkToFit:false,height:auto,colModel:[{index:id,editable:false,name id,label:ID},{index:number,editable:false,name:number,label:number},{index first_name,editable:false,name:client__first_name,label:first name}],caption:Cars,datatype:json,gridview ,sortorder:asc,viewsortcols:true,url:main / examplegrid,rowNum:10,pager:#pager,jsonReader:{repeatitems }}



sample data = {total:1,records:1,rows:[{client__first_name:Bill,client__last_name :Clinton,id:1,number:111222}],page:1}

解决方案

p> OK!让我们获得JSON数据

  {
total:1,
record:1,
rows:[
{
client__first_name:Bill,
client__last_name:Clinton,
id:1,
number:111222
}
],
page:1
}

,jqGrid包含一个附加列

  {name:client__full_name,标签:全名} 

来自 client__first_name client__last_name 。在这种情况下,最简单的方法是使用 beforeProcessing 回调函数:

  $(#list)jqGrid({
url:main / examplegrid ,
datatype:json,
colModel:[
{name:id,label:ID},
{name:client__first_name,label:名字},
{name:client__last_name,label:last name},
{name:client__full_name,label:full name}
],
gridview:true,
jsonReader:{repeatitems:false},
// ...其他参数
beforeProcessing:function(data){
var items = data.rows ,n = items.length,i,item;
for(i = 0; i< n; i ++){
item = items [i];
item.client__full_name = item。 client__first_name +''+
item.client__last_name;
}
}
}) ;

回调函数 beforeProcessing 将被调用在从服务器接收数据并且数据将被处理之前的jqGrid。所以以简单的方式我们可以实现任何虚拟列。


I am using django 1.4 together with jqgrid from django_gems package The following code is trying to bring the "virtual" column onto the grid by concatenating first and last name. However it fails with "Cannot resolve keyword 'client__get_fullname' into field."

Is there any acceptable way to achieve this?

class Car(models.Model):
    number = models.CharField(max_length=50) 
    client = models.ForeignKey('Client')

class Client(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    def get_fullname(self):
        return '%s %s' % (self.first_name, self.last_name)

 from django_gems.jqgrid import JqGrid
 class CarGrid(JqGrid):
     queryset = Car.objects.all()
     fields = ['number', 'client__get_fullname']

jqgrid config = {"altRows": true, "rowList": [10, 25, 50, 100], "sortname": "id", "viewrecords": true, "autowidth": false, "forcefit": false, "shrinkToFit": false, "height": "auto", "colModel": [{"index": "id", "editable": false, "name": "id", "label": "ID"}, {"index": "number", "editable": false, "name": "number", "label": "number"}, {"index": "first_name", "editable": false, "name": "client__first_name", "label": "first name"}], "caption": "Cars", "datatype": "json", "gridview": true, "sortorder": "asc", "viewsortcols": true, "url": "main/examplegrid", "rowNum": 10, "pager": "#pager", "jsonReader": {"repeatitems": false}}

sample data = {"total": 1, "records": 1, "rows": [{"client__first_name": "Bill", "client__last_name": "Clinton", "id": 1, "number": "111222"}], "page": 1}

解决方案

OK! Let us you get the JSON data

{
    "total": 1,
    "records": 1,
    "rows": [
        {
            "client__first_name": "Bill",
            "client__last_name": "Clinton",
            "id": 1,
            "number": "111222"
        }
    ],
    "page": 1
}

and the jqGrid contains an additional column

{name: "client__full_name", label: "full name"}

which should be constructed from client__first_name and client__last_name. In the case the most simplest way would be to use beforeProcessing callback function:

$("#list").jqGrid({
    url: "main/examplegrid",
    datatype: "json",
    colModel: [
        {name: "id", label: "ID"},
        {name: "client__first_name", label: "first name"},
        {name: "client__last_name", label: "last name"},
        {name: "client__full_name", label: "full name"}
    ],
    gridview: true,
    jsonReader: { repeatitems: false },
    //... other parameters
    beforeProcessing: function (data) {
        var items = data.rows, n = items.length, i, item;
        for (i = 0; i < n; i++) {
            item = items[i];
            item.client__full_name = item.client__first_name + ' ' +
                item.client__last_name;
        }
    }
});

The callback function beforeProcessing will be called by jqGrid after the data are received from the server and before the data will be processed. So in the simple way we can implement any "virtual" column.

这篇关于将相关模型的方法分为jqgrid的单独列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆