【发布时间】:2014-12-17 21:00:06
【问题描述】:
我使用 jqGrid 并通过 AJAX 从服务器加载数据。我的数据是{id: number, abberaviation: string, rate: float}(ID 与数据库中的 ID 相同)。然后我将数据传递给jqGrid。用户正在页面上编辑数据。然后用户点击提交,我想向服务器发送数据,但是数据的数据类型错误{id: string, abberaviation: string, rate: string}。
我正在通过getRowData 函数获取数据,但我不知道如何发送与最初从服务器接收到的数据类型相同的数据。
我必须在正确的数据类型上重新键入所有数据类型,这是服务器所期望的。 更新 参数 = 空 通知 = 空
$(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"
【问题讨论】:
-
您写道您的问题是将数据发送到服务器,但您没有包含相应的 Ajax 调用和准备发送数据的代码(
getRowData等的用法)。你能否包括你的代码部分是你的问题的根源? -
嗨,奥列格,我读了一些关于 jqGrid 的回答 :) 我更新了我的问题。双击行进行编辑后,焦点单元格还有另一个问题。我找到了您的解决方案 stackoverflow.com/questions/6536654/…,但我认为此解决方案不适用于 4.7.0 - jQuery Grid,我不知道 :(
-
可能有误解什么是rowid。 jqGrid 使用 id 属性,如果输入项来分配
<tr>元素的 id 属性,这些元素构建网格的行。隐藏id列的使用是多余的。无论如何,即使您将任何数据保存在 HTML 页面的属性中或 HTML 项目内部,您也只保存了一个字符串。不会发送类型信息。所以从 jqGrid 读回的所有数据也是字符串。如果您需要在发送到服务器之前更改类型,您应该在调用 Ajax 之前自己执行此操作。顺便说一句,如果您想显示更多为 20 行,则必须使用rowNum: 10000选项 -
您也可以添加
pager或toppager: true选项以允许对数据进行本地分页。无论如何,我强烈建议您使用 jqGrid 的gridview: true和autoencode: true选项,并使用datatype: "local"而不是datatype: "clientSide"。我建议你从colModel中删除所有index属性。我建议您使用表格和列模板(请参阅here),它们会引导您了解列中的数据类型。 -
不客气!我建议您使用 Chrome/Internet Explorer/Firefox 的开发者工具,并在您的不同测试中检查
<tr>元素的id属性。您还应该考虑使用$("#table").jqGrid("getGridParam", "data")和$("#table").jqGrid("getGridParam", "_index")来获取保存为JavaScript 对象而不是HTML DOM 元素的信息。getRowData数据仅适用于纯 HTML 页面。