我之前沒有用過jquery,ajax和json,但我想學習如何使用dojo和dijit來使用它。有很多教程,但很少有C#與SQL數據庫示例。用sql數據庫中的數據填充dijit菜單欄
我已經創建了dijit的菜單欄的數據,看起來像這樣一個JSON輸出從我的數據庫:
[{"MenuItemId":1,"MenuName":"Root","Tooltip":"Root","IsParent":true,"ParentId":0,"IsVisible":false,"SortIndex":null},
{"MenuItemId":3,"MenuName":"Blogg","Tooltip":"Min Blogg","IsParent":false,"ParentId":1,"IsVisible":true,"SortIndex":1000},
{"MenuItemId":4,"MenuName":"Administrasjon","Tooltip":"Viser Administrasjon","IsParent":true,"ParentId":1,"IsVisible":true,"SortIndex":10000},
{"MenuItemId":5,"MenuName":"DropMenu","Tooltip":"Drop menu","IsParent":true,"ParentId":1,"IsVisible":true,"SortIndex":9000},
{"MenuItemId":6,"MenuName":"Menuitem1","Tooltip":"Menuitem1","IsParent":false,"ParentId":5,"IsVisible":true,"SortIndex":9001},
{"MenuItemId":9,"MenuName":"Menuitem2","Tooltip":"Menuitem2","IsParent":false,"ParentId":5,"IsVisible":true,"SortIndex":9002}]
我要綁定這些數據dijit的菜單欄,但我還沒有想出如何鬥還沒完成。 這是我迄今爲止所做的代碼。我一直在來回嘗試,但是我沒有設法從我嘗試獲取的json數據中獲取數據。
這裏是我嘗試從我的JSON數據輸出中獲取數據的示例。樣品試圖寫在控制檯視圖的數據,但我想填充的dijit manybar:
<script>
require([
"dijit/MenuBar",
"dijit/PopupMenuBarItem",
"dijit/Menu",
"dijit/MenuItem",
"dijit/DropDownMenu",
"dojo/dom",
"dojo/request",
"dojo/json",
"dojo/_base/array",
"dojo/domReady!"
], function(MenuBar, PopupMenuBarItem, Menu, MenuItem, DropDownMenu, dom, request, JSON, arrayUtil) {
console.log('hello world');
// Results will be displayed in resultDiv
var resultDiv = dom.byId("resultDiv");
// Request the JSON data from the server
request.get("/api/Menu", {
// Parse data from JSON to a JavaScript object
handleAs: "json"
}).then(function(data) {
// Display the data sent from the server
var html = "";
var pMenuBar = new MenuBar({});
var pSubMenu = new DropDownMenu({});
console.log('data : ' + data.toString());
arrayUtil.forEach(data.items, function(item, i) {
console.log(item[0].value);
//console.log('item :' + item.name + '\r\n');
//console.log('value : ' + item.value + '\r\n');
});
//html += "<dt>" + item.name +
// "</dt><dd>" + item.value +
// " (" + (typeof item.value) + ")</dd>";
//});
//resultDiv.innerHTML = html;
//pMenuBar.placeAt("wrapper");
//pMenuBar.startup();
},
function(error) {
// Display the error returned
resultDiv.innerHTML = error;
});
});
</script>
我需要的是如何遍歷JSON數據來填充dijit的菜單欄的一個例子。
<body class="claro">
<div data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'sidebar', gutters:true, liveSplitters:true" id="borderContainer">
<div data-dojo-type="dijit/layout/ContentPane" data-dojo-props="splitter:true, region:'leading'" style="width: 300px;">
<div id="markup" style="width: 300px; height: 300px"></div>
</div>
<div data-dojo-type="dijit/layout/ContentPane" data-dojo-props="splitter:true, region:'center'">
<div id="wrapper"></div>
<div id="resultDiv"></div>
</div>
</div>
</body>
我真的可以使用這個幫助。感謝所有我能得到的建議。
謝謝! :)
親切的問候,
喬恩·哈康