【问题标题】:jqGrid - type of modifed data are changed to stringjqGrid - 修改数据的类型更改为字符串
【发布时间】: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 选项
  • 您也可以添加pagertoppager: true 选项以允许对数据进行本地分页。无论如何,我强烈建议您使用 jqGrid 的gridview: trueautoencode: 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 页面。

标签: ajax types jqgrid


【解决方案1】:

在长时间讨论您的问题后,我可以理解您的问题的根源是本地 data 参数的 JavaScript 数据类型发生变化。您使用内联编辑来编辑数据。所以你可以使用aftersavefunc回调来解决问题。

我准备了the demo,它演示了解决方案。您可以验证$grid.jqGrid("getGridParam", "data"); 将在数据类型未更改的情况下返回修改后的数据。修改后数据的属性closed 将保持布尔值,属性taxamounttotal 将保持数字。用作editRow参数的aftersavefunc代码如下:

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);
}

【讨论】:

  • 嗨,这个解决方案似乎是最好的。但是如果我用分隔符','输入数量属性十进制数,那么 parseFloat 会抛出错误。如何解决?
  • @positive:我发现最简单的解决方案是将千位分隔符替换为空字符串,然后将小数分隔符替换为"."。如果不使用千位分隔符并且, 将用作小数分隔符,那么您可以执行类似parseFloat(String(rowItem.amount).replace(",", ".")) 的操作。或者,您可以使用 globalize plagin 来解析数字。见the answer
  • 好的,非常感谢您,您的演示非常有用。我会和它一起玩。查看解决方案,我为 4.7.0 版的单元格焦点找到了该解决方案。 LINK
  • @positive:不客气!我建议你也使用focusField 选项。你应该只小心它的价值。不要使用任何静态值并在 colModel 中搜索特定的 name 属性。 colModel 项与name 的索引是您需要使用的。在使用editRow 之前直接进行索引计算。考虑一下由于columnChooser 或使用jqGrid 的sortable: true 选项可能会改变列的顺序,它允许用户通过拖放列标题来更改列的顺序。
  • @positive:我建议您阅读the post,其中描述了您有权每天投票支持 30 个答案或问题。投票是给搜索引擎提示的最重要的权利。因此,为了帮助其他人找到问题和答案我建议您投票支持您在 stackoverflow 上找到的所有有用信息。您将以其他用户的方式提供帮助。
猜你喜欢
  • 2021-11-02
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 2014-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多