datatables排序插件使用记录以及Uncaught TypeError: Cannot read property ‘1‘ of null

因项目有一个点击表格汉字列排序,但是按照按照数字排序,网上查找datatables的排序插件可以实现这个效果

插件官方地址:https://www.datatables.net/plug-ins/sorting/

插件提供了多重排序功能,根据需求选择  标题数字:根据空元素上的属性对数据进行数字排序

   function addAsc(){
           jQuery.extend( jQuery.fn.dataTableExt.oSort, {
               "title-numeric-pre": function ( a ) {
                   var x = a.match(/title="*(-?[0-9\.]+)/)[1];
                   return parseFloat( x );
               },

               "title-numeric-asc": function ( a, b ) {
                   return ((a < b) ? -1 : ((a > b) ? 1 : 0));
               },

               "title-numeric-desc": function ( a, b ) {
                   return ((a < b) ? 1 : ((a > b) ? -1 : 0));
               }
           } );
       }
 $(document).ready(function () {
                   addAsc();
                    getdatacount();
        }
        )
  function initTableDataApiCall(data) {
            const _data = data;
            var t = $('#exampleTable').DataTable({
 columns: [
                    {
                        data: null,
                        className: 'text-center'
                    },
                    {
                        data: 'applyOrgName',
                        render: function(data, type, full) {
                            if (full.applyorgnamecount!=null){
                                return "<span title="+full.applyorgnamecount+">"+data+"</span>";
                            }else {
                                return "<span title=0>"+data+"</span>";
                            }

                        },
                        className: 'text-center'
                    },
                    {
                        data: 'supplyOrgName',
                        render: function(data, type, full) {
                            if (full.orgnamecount!=null){
                                return "<span title="+full.orgnamecount+">"+data+"</span>";
                            }else {
                                return "<span title=0>"+data+"</span>";
                            }

                        },
                        className: 'text-center'
                    },
                    {
                        data: 'count',
                        className: 'text-center'
                    },
                ],
    "columnDefs": [
                    { type: 'title-numeric', targets: 1 },
                    { type: 'title-numeric', targets: 2 }
                ]

   });

该排序函数对将使用空span元素(或其他任何值)的'title'属性进行数字排序(例如<span title="1000000"><span>1'000'000)。

但是在使用过程中会出现:Uncaught TypeError: Cannot read property '1' of null

排查发现正则表达式未匹配到

所以return "<span title="+full.applyorgnamecount+">"+data+"</span>";而不是return "<span title='"+full.applyorgnamecount+"'>"'+data+"</span>";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白爱吃猪头肉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值