我试图使用以下代码迭代dojo数据网格的选定值并获取所选行并将其发送到Spring MVC,以便我可以映射到对象列表,但是dojo正在发送所有值在一个数组对象中,somebpdy可以让我知道我是否正在做一些错误的
Getting the selected values from the grid:
function getUpdatedRowsFromDataGrid(){
var items = grid.selection.getSelected();
window.arr = [];
if(items.length) {
// Iterate through the list of selected items.
// The current item is available in the variable
// "selectedItem" within the following function:
var spocUpdatedValues = [];
dojo.forEach(items, function(selectedItem){
if(selectedItem !== null){
// Iterate through the list of attributes of each item.
// The current attribute is available in the variable
// "attribute" within the following function:
var obj = new Object();
dojo.forEach(grid.store.getAttributes(selectedItem), function(attribute){
// Get the value of the current attribute:
var value = grid.store.getValues(selectedItem, attribute);
// Now, you can do something with this attribute/value pair.
// Our short example shows the attribute together
// with the value in an alert box, but we are sure, that
// you'll find a more ambitious usage in your own code:
switch (attribute)
{
case "spocId":
obj.spocId= value;
break;
case "spocEmployeeFirstName":
obj.spocEmployeeFirstNameValue = value;
break;
case "spocEmployeeLastName":
obj.spocEmployeeLastNameValue = value;
break;
case "spocPhoneNmbr":
obj.spocPhoneNmbrValue = value;
break;
case "spocStatusCD":
obj.spocStatusCDValue = value;
break;
case "spocLocationCD":
obj.spocLocationCDValue = value;
break;
default:
break;
}
//obj = {"spocId":spocValue,"spocEmployeeFirstName":spocEmployeeFirstNameValue,"spocEmployeeLastName":spocEmployeeLastNameValue,"spocPhoneNmbr":spocPhoneNmbrValue,"spocStatusCD":spocStatusCDValue,"spocLocationCD":spocLocationCDValue}
// alert('attribute: ' + attribute + ', value: ' + value);
});
spocUpdatedValues.push({"spocId": obj.spocId,"spocEmployeeFirstNameValue": obj.spocEmployeeFirstNameValue,"spocEmployeeLastNameValue":obj.spocEmployeeLastNameValue,"spocPhoneNmbrValue": obj.spocPhoneNmbrValue,"spocStatusCDValue": obj.spocStatusCDValue,"spocLocationCDValue": obj.spocLocationCDValue})
} // end if
})
}
return spocUpdatedValues;
}
Sending it to UI
function sendText(){
my_form=document.createElement('FORM');
my_form.name='myForm';
my_form.method='POST';
my_form.action='/mmworkloadmanager/wlm/updatespoc.html';
my_tb=document.createElement('INPUT');
my_tb.type='HIDDEN';
my_tb.name='spocUpdatedValues';
my_tb.value=dojo.toJson(getUpdatedRowsFromDataGrid());
my_form.appendChild(my_tb);
document.body.appendChild(my_form);
my_form.submit();
}
o/p
[{"spocId":[60],"spocEmployeeFirstNameValue":["Joy "],"spocEmployeeLastNameValue":["M "],"spocPhoneNmbrValue":["9727664994"],"spocStatusCDValue":["A"],"spocLocationCDValue":["50 "]},{"spocId":[61],"spocEmployeeFirstNameValue":["PJ "],"spocEmployeeLastNameValue":["J "],"spocPhoneNmbrValue":["9729969171"],"spocStatusCDValue":["C"],"spocLocationCDValue":["56 "]}]
sending every value as an array , can some body suggest whether i need to modify any part of code to avoid the array.
答案 0 :(得分:0)
我认为这里的问题是你在value
变量中添加一个数组。
因为grid.store.getValues(selectedItem, attribute)
将返回一个值,但是作为一个数组,所以你必须将该值解析为字符串,请尝试以下方法:
var value = grid.store.getValues(selectedItem, attribute)[0];
或者:
像这样使用 JavaScript Array join() Method :
var value = grid.store.getValues(selectedItem, attribute).join();
我认为应该这样做。