2015-11-03 67 views
1

我想用日期列排序一個handontable。我正在使用Moment.js將數據庫日期格式設置爲德語區域設置dateformat。Handsontable日期列排序

這是我的代碼:

var myData = [ 
    {'date':moment('2012-12-01').locale('de').format('L'), 'Text':'abc'}, 
    {'date':moment('2015-09-12').locale('de').format('L'), 'Text':'def'}, 
    {'date':moment('2015-01-31').locale('de').format('L'), 'Text':'ghi'}, 
    ], 
    container = document.querySelector('#exampleGrid'); 

var hot = new Handsontable(container, { 
    data: myData, 
    //cols 
    colHeaders: ['Datum','Text'], 
    columns: [ 
     {data:'date', type:'date', dateFormat:'DD.MM.YYYY', correctFormat:true}, 
     {data:'Text'} 
     ], 
    columnSorting: true, 
    sortIndicator: true 
}); 

你有任何想法,爲什麼它沒有在這個fiddle工作?

+1

我認爲日期被視爲字符串。根據日期排序不起作用。 –

+0

嗯,但我只從我的數據庫中得到一個字符串的日期。我必須轉換它嗎?如何? – Scuzzle

+0

即使它的日期,在html視圖中是一個字符串。 –

回答

0

替代JS電子表格,使這更容易。

<html> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 

<script src="http://cdn.bossanova.uk/js/jquery.jexcel.js"></script> 
<script src="http://cdn.bossanova.uk/js/jquery.jcalendar.js"></script> 
<link rel="stylesheet" href="http://cdn.bossanova.uk/css/jquery.jexcel.css" type="text/css" /> 
<link rel="stylesheet" href="http://cdn.bossanova.uk/css/jquery.jcalendar.css" type="text/css" /> 

<div id="my"></div> 

<script> 
data = [ 
    ['Mazda', 2001, 2000, '2006-01-01'], 
    ['Pegeout', 2010, 5000, '2005-01-01'], 
    ['Honda Fit', 2009, 3000, '2004-01-01'], 
    ['Honda CRV', 2010, 6000, '2003-01-01'], 
]; 

$('#my').jexcel({ 
    data:data, 
    colHeaders: ['Model', 'Date', 'Price', 'Date'], 
    colWidths: [ 300, 80, 100, 100 ], 
    columns: [ 
     { type: 'text' }, 
     { type: 'numeric' }, 
     { type: 'numeric' }, 
     { type: 'calendar', options: { format:'DD/MM/YYYY' } }, 
    ] 
}); 
</script> 
</html> 

http://www.bossanova.uk/jexcel/using-a-calendar-column-type