2013-02-14 22 views
3

我想使用Fuel UX數據網格來顯示我從數據庫中檢索的一些數據。該頁面來自Ruby on Rails服務器。如何向Rails中的Fuel UX datagrid提供數據?

建立數據對象的JavaScript代碼示例:

 var dataSource = new StaticDataSource({ 
      columns: [{ 
       property: 'toponymName', 
       label: 'Name', 
       sortable: true 
      }, { 
       property: 'countrycode', 
       label: 'Country', 
       sortable: true 
      }, { 
       property: 'population', 
       label: 'Population', 
       sortable: true 
      }, { 
       property: 'fcodeName', 
       label: 'Type', 
       sortable: true 
      }], 
      data: sampleData.geonames, 
      delay: 250 
     }); 

     $('#MyGrid').datagrid({ 
      dataSource: dataSource, 
      stretchHeight: true 
     }); 

     $('#datagrid-reload').on('click', function() { 
      $('#MyGrid').datagrid('reload'); 
     }); 

如果我理解代碼,我要被界定我的專欄,並在列的一些屬性的數據源變量的內部對象,並數據對象正在由sampleData.geonames加載。

的sampleData在爲here

我能做些什麼使用Rails的慣例更換sampleData.geonames?我試着調整這幾種方法來加載rails對象到這裏。

例如,我修改了我的列的屬性字段以對應於我的用戶模型的一些屬性。我試着更換

data: sampleData.geonames, 

data: <%= @users.to_json %>, 

我有點禁區寶石和版本,目前使用Rails 2.3。

感謝您的任何幫助。

回答

1

如果你想在DataGrid中做背景AJAX請求,從您的應用程序加載數據,請參見本教程你需要什麼,這將是更接近:

http://dailyjs.com/2012/10/29/fuel-ux

這將有益處的即時頁面加載,然後是異步加載數據。

如果你寧願用StaticDataSource中的方法堅持只嵌入類似這樣你的頁面上的一個小腳本:

<script> 
    var myData = { ... }; 
</script> 

然後,負載與:

var dataSource = new StaticDataSource({ 
    columns: [ ... ], 
    data: myData, 
    delay: 250 
}); 
+0

你能提供的示例myData中的'...'應該看起來像什麼? – 2013-03-26 02:57:31

+0

當然,它應該是一個對象數組。例如:https://github.com/ExactTarget/fuelux/blob/master/sample/data.js#L9-L1907 – 2013-03-26 11:13:12

+0

與數據網格上的列和數據選項關聯,如下所示:https://github.com /ExactTarget/fuelux/blob/master/index.html#L114-L136 – 2013-03-26 11:14:14