我想创建 jqwidgets 数据网格,我希望所有列都作为字符串,但是当我通过 javascript 添加新行而不是某些字符串值时,组件将我设置为 0、1、2、3 等。
这是代码:
var source ={
datatype: "json",
cache:false,
contentType: 'application/json; charset=utf-8',
datafields: [
{ name: 'uid', type:'string'},
<%
int count = 1;
for(String col : us.columns) {
if(count == us.columns.length) {
%>
{ name: '<%= col %>' }
<%
}
else {
%>
{ name: '<%= col %>' },
<%
}
++count;
}
%>
],
url:"<%= request.getContextPath() %>/test/list/",
addrow: function (rowid, rowdata, position, commit) {
//alert(rowdata.uid +" "+ rowdata.ime);
//alert($.param(rowdata));
$.ajax({
url: "<%= request.getContextPath() %>/test/add/",
type: "POST",
data: $.param(rowdata),
success: function(data) {
//alert(data.error + " " + data.result + " " + data.mess);
alert(data.ime + " " + data.columns);
$("#historygrid").jqxGrid('updatebounddata');
increment++;
commit(true);
},
error:function(data) {
alert(data);
commit(false);
}
});
},
deleterow: function (rowid, commit) {
//commit(true);
}
};
var dataAdapter = new $.jqx.dataAdapter(source);
$("#testgrid").jqxGrid({
width: 659,
height: 300,
source: dataAdapter,
theme: 'office',
editable: true,
selectionmode: 'singlecell',
autoheight: false,
altrows: false,
showtoolbar: true,
rendertoolbar: function (toolbar) {
var me = this;
var container = $("<div style='margin: 5px;'></div>");
toolbar.append(container);
container.append('<input id="addrowbutton" type="button" value="Dodaj novi red" />');
container.append('<input style="margin-left: 5px;" id="deleterowbutton" type="button" value="Obrisi oznaceni red" />');
//container.append('<input style="margin-left: 5px;" id="updaterowbutton" type="button" value="Update Selected Row" />');
$("#addrowbutton").jqxButton();
$("#deleterowbutton").jqxButton();
// create new row.
$("#addrowbutton").on('click', function () {
var datarow = newrow();
var commit = $("#testgrid").jqxGrid('addrow', null, datarow);
});
// delete row.
$("#deleterowbutton").on('click', function () {
var selectedrowindex = $("#testgrid").jqxGrid('getselectedrowindex');
var rowscount = $("#testgrid").jqxGrid('getdatainformation').rowscount;
if (selectedrowindex >= 0 && selectedrowindex < rowscount) {
var id = $("#testgrid").jqxGrid('getrowid', selectedrowindex);
var commit = $("#testgrid").jqxGrid('deleterow', id);
}
});
},
columns: [
{ text: 'UID', datafield: 'uid', editable:false, width: '10%'},
<%
count = 1;
for(String col : us.columns) {
if(count == us.columns.length) {
%>
{ text:'<%= col %>', datafield: '<%= col %>', width: '45%'}
<%
}
else {
%>
{ text:'<%= col %>', datafield: '<%= col %>', width: '45%'},
<%
}
++count;
}
%>
]
});
var newrow = function (i) {
var row = {};
row["uid"] = "hallo";
<%
for(String col : us.columns) {
%>
row["<%=col%>"] = "Default Value";
<%
}
%>
return row;
}
检查 uid 的 newrow 函数我设置了值“Hallo”,但我得到了 0,并且每下一行我得到增量值,这看起来像计数还是?如何解决这个问题?