【问题标题】:Tablesorter 2.0 Plugin and Tables with Nested HTMLTablesorter 2.0 插件和带有嵌套 HTML 的表格
【发布时间】:2016-01-12 19:19:43
【问题描述】:

我正在使用来自 https://mottie.github.io/tablesorter/docs/index.html 的 Tablesorter 插件,并且我在 MVC Razor 视图中有下表。

@foreach (var item in Model.Products)
{
    decimal? vatValue = (item.UnitPrice * item.Quantity) * 0.2M;
    decimal? goodsValue = (item.UnitPrice * item.Quantity);
    <tr>
        <td class="product-title" data-thumbnail="/_includes/img/uploads/product-thumb.jpg"><span>@item.ProductName</span>
        </td>
        <td class="product-id">
            <span class="hl">Product code: </span>
            @item.ProductCode
        </td>
        <td class="price">&pound;@item.UnitPrice 
            <small class="hl">unit price</small>
        </td>
        <td class="units">
            <input type="number" class="choose-quantity" placeholder="0" value="@item.Quantity" min="0" readonly="readonly">
        </td>
        <td class="vat">
            <small class="hl">VAT:</small>
            &pound;@(vatValue)
        </td>
        <td class="goods-value">
            <small class="hl">Goods value:</small>
            &pound;@(goodsValue)
        </td>
    </tr>
}

增值税栏和商品价值栏没有数字。我理解为什么会这样,这是因为我们在正在排序的标签内有一个嵌套标签,并且插件可能会将 HTML 解释为要排序的字符串的一部分,因此不会按数字排序。

无论如何我可以解决这个问题吗?我可以在此表上设置任何配置设置以使其仅查找数字吗?

谢谢!

【问题讨论】:

    标签: javascript asp.net-mvc sorting html-table tablesorter


    【解决方案1】:

    如果将值包装在跨度中,这可能是最简单的:

    <tr>
        <td class="product-title" data-thumbnail="/_includes/img/uploads/product-thumb.jpg">
            <span>@item.ProductName</span>
        </td>
        <td class="product-id">
            <span class="hl">Product code: </span>
            @item.ProductCode
        </td>
        <td class="price">
            <span>&pound;@item.UnitPrice</span>
            <small class="hl">unit price</small>
        </td>
        <td class="units">
            <input type="number" class="choose-quantity" placeholder="0" value="@item.Quantity" min="0" readonly="readonly">
        </td>
        <td class="vat">
            <small class="hl">VAT:</small>
            <span>&pound;@(vatValue)</span>
        </td>
        <td class="goods-value">
            <small class="hl">Goods value:</small>
            <span>&pound;@(goodsValue)</span>
        </td>
    </tr>
    

    然后您可以使用textExtraction function 定位范围并提取相关信息(demo):

    $(function () {
        $('table').tablesorter({
            theme: 'blue',
            textExtraction: {
                // header class names
                '.price, .vat, .goods-value' : function (node) {
                    return $(node).find('span').text();
                }
            },
            widgets: ['zebra', 'columns']
        });
    });
    

    *注意* 用于定位列的类名必须与thead 中的单元格的类名匹配,而不是tbody 中的单元格。

    【讨论】:

    • 这和我其实做的很相似。我遇到了这个问题和另一个问题,由于编码 £ 导致排序不起作用字符代码。我在每个字段中都写了一个隐藏的跨度,并把它放在第一位。它没有在表中看到,但允许搜索有效地工作。我还要说,你的插件做得很棒!非常有帮助,继续努力,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 2015-08-29
    • 2014-11-25
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    相关资源
    最近更新 更多