【问题标题】:JqGrid sorting on href tag and not contentJqG​​rid对href标签而不是内容进行排序
【发布时间】:2011-03-28 17:12:21
【问题描述】:

我目前正在试用 jqGrid 插件。一切运行良好,期望在特定列上进行排序。

我有一个正在尝试应用插件的现有表。

<script type="text/javascript">
    $(document).ready(function () {
        tableToGrid("#myTable", {})
    });
</script>

<table id="myTable">
    <thead>
        <tr>
            <th>
                Web Site
            </th>
        </tr>
    </thead>
<tbody>
        <tr>
            <td>
                <a href="http://www.jsmith.com/4093">Hello</a>
            </td>
        </tr>
        <tr>
            <td>
                <a href="http://www.jsmith.com/4094">Bob</a>
            </td>
        </tr>
        <tr>
            <td>
                <a href="http://www.jsmith.com/4093">Loblaws</a>
            </td>
        </tr>
        <tr>
            <td>
                <a href="http://www.jsmith.com/4093">Wahoo</a>
            </td>
        </tr>
    </tbody>
</table>

当我对列进行排序时,顺序显示为

你好,Loblaws,Wahoo,Bob

而不是

Bob,你好,Loblaws,Wahoo

看起来它是在对 href 标记进行排序,而不是对内容进行排序。

和这个问题很相似(只是插件不同)-Table sorter issue with content

【问题讨论】:

    标签: c# jquery asp.net jquery-ui jqgrid


    【解决方案1】:

    您之所以有如此奇怪的排序顺序,是因为在您当前使用 jqGrid 的方式中,您创建的网格有一列包含 string 数据。字符串数据将是:

    "\n                <a href=\"http://www.jsmith.com/4093\">Hello</a>\n            "
    "\n                <a href=\"http://www.jsmith.com/4094\">Bob</a>\n            "
    "\n                <a href=\"http://www.jsmith.com/4093\">Loblaws</a>\n            "
    "\n                <a href=\"http://www.jsmith.com/4093\">Wahoo</a>\n            "
    

    你怎么能看到包含“Bob”子串的字符串之前有“4094”。所以该字符串将是排序顺序中的最后一个字符串。

    您可以使用tableToGrid 的第二个options 参数来改善这种情况,但最好的方法是明确信息分隔关于列中显示的文本(如“ Bob"、"Hello") 等来自 url 数据。然后对列的排序将完全符合您的要求。所以最好不要对你的数据使用tableToGrid函数,直接创建jqGrid。

    因为我不知道您从哪个来源获得有关文本 url 的信息,所以很难为您提供最佳实施建议。您可以在 jqGrid herehere 中找到如何构建链接的示例。我认为您可以根据自己的目的轻松修改示例。

    【讨论】:

    • 我从表中删除了 href,然后将其放入表中的 Id 列,然后使用 .hideCol 隐藏它。毕竟,当用户单击该行时,我输入了一些 jquery 代码来重定向用户。感谢您的所有意见。
    【解决方案2】:

    您可以使用自定义格式化程序custom_formatter。它会帮助您在 jqgrid 中根据 href 内容进行排序的情况下,我觉得这是不可能的。

    或者,您可以再传递一个包含 href 内容的列,并基于此对 href 列进行排序,尽管这不是首选。

    【讨论】:

    • 我尝试查找自定义格式化程序,但由于 tableToGrid 方法预先填充了 colModel,因此这很棘手。
    【解决方案3】:

    tableToGrid 不会对您的表格进行排序,它只会将您的表格变成网格。 对于客户端排序,这可能会对您有所帮助。 jqGrid sorting on client side

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多