jqGrid的 - 改性的数据类型更改为字符串 [英] jqGrid - type of modifed data are changed to string
问题描述
我使用jqGrid的,我加载数据从服务器通过AJAX。我的数据是 {ID:号,abberaviation:字符串,利率:浮动}
(Id为同样喜欢标识数据库)。然后,我将数据传递给jqGrid的。用户在编辑页面上的数据。然后用户会点击提交,我想将数据发送到服务器,但数据有错误的数据类型 {ID:字符串,abberaviation:字符串,速度:字符串}
我通过 getRowData code>函数获取数据,我不知道该怎么最初从服务器接收与发送相同的数据类型的数据。
我必须重新输入右边数据类型的所有数据类型,这些服务器要求。 更新时间: PARAMS = NULL 注意= NULL
$(文件)。就绪() - >
PARAMS = JSON.parse $(#PARAMS)。HTML()
通知= $(#的通知)
表= $(#表)
$ .jgrid.no_legacy_api =真
lastSelectedRowId = NULL
table.jqGrid(
数据类型:从阵列中的JS客户方#负荷
数据:params.data#数据数组
宽:100%
高度:100%
colNames:['','Mena的','库尔兹','Akce']
colModel:
{
指数:'身份证',
名称:'身份证',
宽度:55,
编辑:假的,
排序:假的,
隐藏:真正的,
},{
索引:缩写,
名称:缩写,
宽度:90,
对齐:中心,
编辑:真正的,
排序:假的
},{
指数:'速度',
名称:'速度',
宽度:100,
对齐:中心,
编辑:真正的,
排序:假的
},{
名称:'行动',
宽度:40,
对齐:中心,
编辑:假的,
格式:'行动',
固定的:真正的,
调整:假的,
formatoptions:{键:真,editbutton:假}
}
]
寻呼机:#status
editurl:clientArray#数据将不会被发送到服务器,而是只保存到网格
#sortname:'身份证'
#排序顺序:递减
rowList:[]#禁用页面大小下拉
pgbuttons:像下一个,后退按钮虚假#禁用页面控制
pgtext:像第10 0'空#禁止寻呼机文本
viewrecords:真正#显示记录总数查看X到Y出的Z'
onSelectRow:(ROWID) - >
的console.logROWID#{ROWID}#{lastSelectedRowId}
如果lastSelectedRowId和ROWID心不是lastSelectedRowId
table.jqGridsaveRow,lastSelectedRowId
ondblClickRow:(ROWID,iRow,ICOL,E) - GT;
的console.logROWID#{ROWID}#{lastSelectedRowId}
如果ROWID和ROWID心不是lastSelectedRowId
的console.log e.target
table.jqGrideditRow,ROWID,真
$(输入选择,e.target).focus()
lastSelectedRowId = ROWID
)
table.bind(jqGridInlineAfterSaveRow,(即ROWID,orgClickEvent) - >
的console.logjqGridInlineAfterSaveRow
的console.loglastSelectedRowId = NULL
lastSelectedRowId = NULL
)
table.bind(jqGridInlineAfterRestoreRow,(即ROWID,orgClickEvent) - >
的console.logjqGridInlineAfterRestoreRow
的console.loglastSelectedRowId = NULL
lastSelectedRowId = NULL
)
$(#加行)点击(E) - GT;
即preventDefault()
#table.jqGrideditCell,1,1,假
table.jqGridaddRowData,不确定的,最后
$(#拯救)点击(E) - GT。
即preventDefault()
showNoticeProbíháukládání呐服务器...
数据= table.jqGridgetRowData
因为我在[0 ... data.length] 1
项目=数据[I]
item.id =数字item.id
如果item.id为0
删除数据[I]
否则,如果item.id是NAN
item.id = NULL
item.order = I
item.rate =号码item.rate
jsonData = JSON.stringify数据
#$就
#网址:params.action
#类型:POST
#数据:令牌:params.token,数据:jsonData
#成功:(RES) - >
#记号= res.token
#的console.log res.data
#showNotice数据bylaúspěšněuložena。
#错误:(错误) - >
#errText = JSON.parse error.responseText
#showNotice响应#{error.status}#{errText.msg}
#的console.log错误
showNotice =(MSG) - >
notice.html味精
notice.parent()。removeClass移除隐藏
在你的问题很长的讨论中,我能理解你的问题的来源正在发生变化的地方数据的JavaScript数据类型的
参数。您可以使用内联编辑编辑数据。所以,你可以使用 aftersavefunc
回调来解决这个问题。
我prepared 的演示这表明解决方案。您可以验证 $ grid.jqGrid(getGridParam,数据);
将返回修改其中的数据的类型不变化的数据。该酒店关闭
修改后的数据将stayboolean和属性税
,金额
和总
将留号码。 aftersavefunc 的$ C $的 c所用的
editRow
参数如下:
aftersavefunc:功能(ROWID,RESP,TMP,O){
VAR rowItem = $(本).jqGrid(getLocalRow,ROWID);
rowItem.closed = rowItem.closed ===是;
rowItem.tax = parseFloat(rowItem.tax);
rowItem.amount = parseFloat(rowItem.amount);
rowItem.total = parseFloat(rowItem.total);
}
I use jqGrid and I loads data from server via AJAX. My data are {id: number, abberaviation: string, rate: float}
(Id is same like Id in database). Then I will pass data to jqGrid. User is editing data on page. Then user will click on submit and I want to send data to server, but data has wrong datatypes {id: string, abberaviation: string, rate: string}
.
I am getting data via getRowData
function and I don't know how to send data with the same datatypes as originally received from the server.
I must retyped all datatypes on right datatypes, which server is expecting. UPDATED params = null notice = null
$(document).ready () ->
params = JSON.parse $("#params").html()
notice = $("#notice")
table = $("#table")
$.jgrid.no_legacy_api = true
lastSelectedRowId = null
table.jqGrid(
datatype: "clientSide" # load from array in js
data: params.data # data array
width: "100%"
height: "100%"
colNames: ['','Měna', 'Kurz', 'Akce']
colModel: [
{
index: 'id',
name: 'id',
width: 55,
editable: false,
sortable: false,
hidden: true,
}, {
index: 'abbreviation',
name: 'abbreviation',
width: 90,
align: "center",
editable: true,
sortable: false
}, {
index: 'rate',
name: 'rate',
width: 100,
align: "center",
editable: true,
sortable: false
}, {
name: 'action',
width: 40,
align: "center",
editable: false,
formatter:'actions',
fixed: true,
resize: false,
formatoptions: { keys: true, editbutton: false }
}
]
pager: "#status"
editurl: "clientArray" # data won't be posted to the server but rather is saved only to the grid
# sortname: 'id'
# sortorder: "desc"
rowList: [] # disable page size dropdown
pgbuttons: false # disable page control like next, back button
pgtext: null # disable pager text like 'Page 0 of 10'
viewrecords: true # show total number of records 'View X to Y out of Z'
onSelectRow: (rowId) ->
console.log "rowId #{rowId} #{lastSelectedRowId}"
if lastSelectedRowId and rowId isnt lastSelectedRowId
table.jqGrid 'saveRow', lastSelectedRowId
ondblClickRow: (rowId, iRow, iCol, e) ->
console.log "rowId #{rowId} #{lastSelectedRowId}"
if rowId and rowId isnt lastSelectedRowId
console.log e.target
table.jqGrid 'editRow', rowId, true
$("input, select", e.target).focus()
lastSelectedRowId = rowId
)
table.bind("jqGridInlineAfterSaveRow", (e, rowid, orgClickEvent) ->
console.log "jqGridInlineAfterSaveRow"
console.log "lastSelectedRowId = null"
lastSelectedRowId = null
)
table.bind("jqGridInlineAfterRestoreRow", (e, rowid, orgClickEvent) ->
console.log "jqGridInlineAfterRestoreRow"
console.log "lastSelectedRowId = null"
lastSelectedRowId = null
)
$("#add-row").click (e) ->
e.preventDefault()
# table.jqGrid "editCell", 1, 1, false
table.jqGrid "addRowData", undefined, "last"
$("#save").click (e) ->
e.preventDefault()
showNotice "Probíhá ukládání na server..."
data = table.jqGrid "getRowData"
for i in [0...data.length] by 1
item = data[i]
item.id = Number item.id
if item.id is 0
delete data[i]
else if item.id is "NaN"
item.id = null
item.order = i
item.rate = Number item.rate
jsonData = JSON.stringify data
# $.ajax
# url: params.action
# type: "POST"
# data: token: params.token, data: jsonData
# success: (res) ->
# token = res.token
# console.log res.data
# showNotice "Data byla úspěšně uložena."
# error: (error) ->
# errText = JSON.parse error.responseText
# showNotice "Response #{error.status}: #{errText.msg}"
# console.log error
showNotice = (msg) ->
notice.html msg
notice.parent().removeClass "hidden"
After a long discussion of your problem I could understand that the origin of your problem is changing of type of JavaScript data of local data
parameter. You use inline editing to edit the data. So you can use aftersavefunc
callback to fix the problem.
I prepared the demo which demonstrates the solution. You can validate that $grid.jqGrid("getGridParam", "data");
will returns modified data where the types of the data are not changes. The property closed
of the modified data will stayboolean and the properties tax
, amount
and total
will be stay numbers. The code of aftersavefunc
used as editRow
parameter is the following:
aftersavefunc: function (rowid, resp, tmp, o) {
var rowItem = $(this).jqGrid("getLocalRow", rowid);
rowItem.closed = rowItem.closed === "Yes";
rowItem.tax = parseFloat(rowItem.tax);
rowItem.amount = parseFloat(rowItem.amount);
rowItem.total = parseFloat(rowItem.total);
}
这篇关于jqGrid的 - 改性的数据类型更改为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!