jqgrid:排序时不调用unformat

时间:2011-06-01 16:17:27

标签: jqgrid

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

如您所见,unformat_salary函数中的console.log行永远不会被调用。即使您单击Salary标头对其进行排序。排序似乎工作,但它是词法排序,我想要一个自定义排序。提供'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>

1 个答案:

答案 0 :(得分:0)

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

在您的情况下,您可以使用

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