【问题标题】:How to display a collection of results to a grid cell using jqGrid如何使用 jqGrid 将结果集合显示到网格单元格
【发布时间】:2014-08-02 11:24:16
【问题描述】:

我有一个包含多对多关系的数据库,所以一个帖子可以有多个标签,一个标签可以分配给多个帖子

我正在使用 jqgrid 使用以下代码在网格中显示所有帖子:

Javascript:

//grid
jQuery(document).ready(function () {
    //post grid
    jQuery("#tablePosts").jqGrid({
        url: '/Admin/ListPosts/',
        datatype: 'json',
        mtype: 'GET',
        colNames: ['ID', 'Title', 'Short Description', 'Description', 'Category', 'Tags', 'Published', 'Posted Date', 'Modified Date', 'UrlSlug', 'Meta'],
        colModel: [
            { name: 'PostID', index: 'PostID', width: 50, stype: 'text' },
            { name: 'Title', index: 'Title', width: 150 },
            { name: 'ShortDescription', index: 'ShortDescription', width: 150, sortable: false },
            { name: 'Description', index: 'Description', width: 200, sortable: false },
            { name: 'Category', index: 'Category', width: 100 },
            { name: 'Tags', index: 'Tags', width: 100, sortable: false},
            { name: 'Published', index: 'Published', width: 80 },
            { name: 'PostedOn', index: 'PostedOn', width: 130 },
            { name: 'Modified', index: 'Modified', width: 130 },
            { name: 'UrlSlug', index: 'UrlSlug', width: 80, sortable: false },
            { name: 'Meta', index: 'Meta', width: 80, sortable: false }
        ],
        rowNum: 10,
        rowList: [5, 10, 20, 50],
        viewrecords: true,
        pager: '#pagerPosts',
        height: '100%',
        sortname: 'PostedOn',
        sortorder: "desc",
        //width to null && shrink to false so the width of the grid inherit parent and resizes with the parent
        width: null,
        shrinkToFit: false
    });
});

这是我的控制器操作:

public ActionResult ListPosts(string sidx, string sord, int page, int rows)
{
    int pageNo = page - 1;
    int pageSize = rows;

    //for paging
    int totalRecords = repository.TotalPosts(true) + repository.TotalPosts(false);
    int totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows); //round up to smallest integral number greater than returned valued

    //for records
    var posts = repository.AllPosts(pageNo, pageSize, sidx, sord == "asc");

    var jsonData = new
    {
        total = totalPages,
        page = page,
        records = totalRecords,
        rows = (
            from post in posts
            select new
            {
                id = post.PostID,
                cell = new string[] {
                    post.PostID.ToString(), 
                    post.Title, 
                    post.ShortDescription, 
                    post.Description, 
                    post.Category.Name,
                    post.Tags.ToString(),
                    post.Published.ToString(), 
                    post.PostedOn.ToString(), 
                    post.Modified.ToString(), 
                    post.UrlSlug,
                    post.Meta
                }
            }).ToArray()
    };
    return Json(jsonData, JsonRequestBehavior.AllowGet);
}

在我的行中,我将帖子标签定义为字符串只是为了消除错误,但我不知道如何将标签显示为列表,这是我的网格:

如您所见,Tags 列没有显示 Tag 名称,如何正确显示?

【问题讨论】:

  • @Humayun - 建议编辑只是更改一些代码缩进确实没有任何价值,尤其是当代码看起来不错时。
  • @DavidMakogon 只是稍微减少了水平滚动的需要。

标签: javascript jquery asp.net-mvc json jqgrid


【解决方案1】:

post.Tags.ToString() 使用 string.Join(",",post.Tags.Select(t => t.Name)) 安装

或者将标签数组传递给您的视图并使用custom formatter

格式化程序可能如下所示:

function tagFormatter(cellvalue, options, rowObject)
{
    var text = ""
    if (cellvalue.length > 0)
    {
        for (var i = 0; i < cellvalue.length; i++)
        {
            text += cellvalue[i].Name;
            if (i < cellvalue.length - 1)
            {
                text += ", ";
            }
        }
    }
    return text;
}

它将显示逗号分隔的标签名称; 和标签行:

{ name: 'Tags', index: 'Tags', width: 100, sortable: false, formatter: tagFormatter },

有用吗?

【讨论】:

  • 我尝试了您的建议,但网格不再显示任何内容
  • 您确定操作ListPosts 会返回某些内容吗?
  • 如果 Tag 是一个字符串那么一切都很好,一旦它是一个数组,网格就变空了,选择末尾的 .ToArray() 有效果吗?因为它就像数组中的数组
  • 更好的主意;) post.Tags.ToString() 使用 string.Join(",",post.Tags.Select(t =&gt; t.Name)) 并从 js 中删除标签格式化程序。如果可行,我将编辑我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多