【问题标题】:jqgrid: unformat is not called while sortingjqgrid:排序时不调用 unformat
【发布时间】:2011-06-01 16:17:26
【问题描述】:

根据 jqGrid 文档,如果您在 colOptions 中提供自定义格式化程序,您还应该提供一个“取消格式化”键,该键在排序操作期间被调用。但是,我没有看到这种情况发生,即未调用 unformat 函数。这是一个非常简单的例子:

如您所见,unformat_salary 函数中的 console.log 行永远不会被调用。即使您单击薪水标题对其进行排序。排序似乎有效,但它是按词法排序的,我想要一个自定义排序。提供 'sorttype' 作为函数可以满足我的要求,但我想知道为什么 unformat 没有被调用,当文档明确说明它在排序操作期间被调用时。

JQGRID 测试

    <script type="text/javascript" src="http://trirand.com/blog/jqgrid/js/jquery.js"></script>
    <script type="text/javascript" src="http://trirand.com/blog/jqgrid/js/jquery-ui-1.8.1.custom.min.js"></script>
    <script type="text/javascript" src="http://trirand.com/blog/jqgrid/js/i18n/grid.locale-en.js"></script>
    <script type="text/javascript" src="http://trirand.com/blog/jqgrid/js/jquery.jqGrid.min.js"></script>
    <style type="text/css" media="screen">
        th.ui-th-column div{
            white-space:normal !important;
            height:auto !important;
            padding:2px;
        }
    </style>
    <script type="text/javascript">
        $(function() {
            createGrid();
        });
        function createGrid() {
            $("#jqgrid-table").jqGrid({
                colNames:['First<br/>Name', 'Last Name', 'Age', 'Salary', 'Type'],
                colModel:[
                    {name:'firstName',index:'firstName', width:100},
                    {name:'lastName',index:'lastName', width:100},
                    {name:'age', index:'age', width:50},
                    {name:'salary', index: 'salary', width:50, sortable:true, formatter: salary_formatter, unformat:unformat_salary},
                    {name:'type', index:'type', width: 56}
                ],
                width: 800,
                datatype:'local',                   
                pager: '#pager2',
                viewrecords: true,
                caption:"JSON Example"
            });
            var searchOptions = {
                caption: 'Filter...',
                multipleSearch:true,
                closeAfterSearch:true,
                closeAfterReset:true,
                Find: 'Filter'
            };                
            jQuery("#jqgrid-table").jqGrid('navGrid',
                                    '#pager2', 
                                    {search:true, edit:false, add:false, del:false, refresh:false}, 
                                    null, null, null, searchOptions
                                    );
            var data = getData();
            for(var i =0; i < data.length; i++) {
                var r = data[i];
                jQuery("#jqgrid-table").addRowData(r.id, r);
            }
        }

        function getData() {
            return [
                   {id:1, firstName: 'John', lastName: 'XXX',  age:'30',  salary:'1500', type: 'Nice'},
                   {id:2, firstName: 'Ashley', lastName:'YYY', age:'31',  salary:'1300', type:'Nicer'},
                   {id:3, firstName:'Smith', lastName:'ZZZ', age:'23',    salary:'1301', type:'Nicest'},
                   {id:4, firstName:'Sarah', lastName:'Aster', age:'45',  salary:'530',  type:'Nicest'},
                   {id:5, firstName:'Michelle', lastName:'Crazy', age:'30', salary:'1423', type:'Nicest'}
            ];
        }

        function salary_formatter(cellvalue) {
            return cellvalue.replace(/^(\d\d)/,'$1,');
        }

        function unformat_salary(cellvalue) {
            console.log('U : ' + cellvalue); // THIS DOES NOT GET CALLED !
            return Number(cellvalue.replace(/,/g,''));
        }
    </script>
    </head>
    <body>
      <div id='jqgrid-div'>
          <table id='jqgrid-table'></table>
         <div id="pager2"></div>
      </div>
    </body>
    </html>

【问题讨论】:

    标签: jqgrid


    【解决方案1】:

    您误解了unformat 函数的含义。只有当您获得单元格、行或列的包含时,它才会被解决。例如,如果您使用getCellgetRowDatagetCol 或关闭jqGrid methods。如果您使用datatype:'local'(如您的示例中),则sorttype 属性定义列的排序方式。在自定义排序的情况下,您可以使用sorttype as function。该函数可以有两个参数:cellValuerowData。最后一个参数代表当前行的非格式化数据。

    在你的情况下你可以使用

    sorttype:function(cellvalue){
        return unformat_salary(cellvalue);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 2011-05-03
      相关资源
      最近更新 更多