简体   繁体   English

JQWidget Grid-在声明之前更改行数据

[英]JQWidget Grid - rowdata changing even before statement to do so

I am having some trouble with a JQWidget-jqxGrid i'm developing. 我正在开发的JQWidget-jqxGrid遇到一些麻烦。 I will like to change the format information is being send to an API from a row edition. 我想更改从行版本发送到API的格式信息。 I need to change some Dates to a specific string notation. 我需要将某些日期更改为特定的字符串表示法。 This is the code i am using right now: 这是我现在正在使用的代码:

updaterow: function (rowid, rowdata, commit)
            {
                //console.log(rowdata);
                var output = rowdata;

                for (property in output) {
                    if (output[property] instanceof Date && schema.properties[property].format === "time") {
                        output[property] = output[property].getHours() + ":" + output[property].getMinutes();
                        //console.log('hola');
                    }
                    if (output[property] instanceof Date && schema.properties[property].format === "date")
                    {
                        output[property] = output[property].getFullYear() + "-" + output[property].getMonth() + 1 + '-' + output[property].getDate();
                    }
                }

                /*console.log(event.args.row);*/
                console.log(output);
                console.log(rowdata);

                var token = $('input[name="__RequestVerificationToken"]').val();

                $.ajax({
                    url: "/api/" + entity + "/" + rowdata.uid,
                    cache: false,
                    method: "put",
                    data: JSON.stringify(rowdata),
                    processData: false,
                    headers: {
                        "RequestVerificationToken": token,
                        "Content-type": "Application/json"
                    },
                    success: function () {
                        console.log("Okey dokey!");
                        commit(true);
                    },
                    error: function () {
                        alert("El dato no se ha actualizado correctamente");
                    }
                });
            }

I tried many things before doing this. 在这样做之前,我尝试了很多事情。 Originally, i was doing the update on a “oncelledit” event, the problem was the same (And this is like some kind of paranormal activity for me): 最初,我是在“ oncelledit”事件上进行更新的,问题是相同的(这对我来说就像某种超自然活动):

As you can see, i am doing the reformating of data in output variable, instead of rowdata. 如您所见,我正在重新格式化输出变量中的数据,而不是rowdata。 Even so, before output variable gets modified for the ajax request, if i uncomment “console.log(rowdata);”, data will already be changed even before the “for” scope where data is modified. 即使这样,在为ajax请求修改输出变量之前,如果取消注释“ console.log(rowdata);”,即使在修改数据的“ for”作用域之前,数据也已经被更改。 How this is really possible? 这怎么可能呢? I have checked cache by using another browser, but no luck. 我已经使用其他浏览器检查了缓存,但是没有运气。

Although data is correctly posted to my API with this code, data format is changed on the grid displayed to the user, and data gets ouwfull. 尽管使用此代码将数据正确地发布到了我的API,但是在显示给用户的网格上数据格式发生了变化,并且数据变得不完整。 I will like to send the information in the appropriate format, but not to change format data shown in the grid. 我想以适当的格式发送信息,但不希望更改网格中显示的格式数据。

This is how the data is shown before edition: 这是在编辑之前显示数据的方式:
fecha de inicio | fecha de inicio | hora de inicio 霍里-德因尼西奥
1/10/2018 | 1/10/2018 | 8:00 AM 8:00 AM

And this is after edition: 这是以后的版本:
fecha de inicio | fecha de inicio | hora de inicio 霍里-德因尼西奥
2001-01-1 |13:0 2001-01-1 | 13:0

I post my full code just in case: 我发布我的完整代码以防万一:

—JSGrid.js— —JSGrid.js—

 $.ajaxSetup({ cache: false }); var FKSources = []; var FKAdapters = {}; var dataFieldsArr = []; var columnsArr = []; var FKPropertySelection; var entity; var schema; function createGrid() { $.ajax({ url: "/api/" + entity + "/schema", success: function (data) { schema = data; for (property in data.properties) { if (data.properties[property]["type"].indexOf("lhcrud") > -1) { FKSources.push({ datatype: "json", datafields: [ { name: data.fkAttributes[property] }, { name: 'id', type: 'int' } ], id: 'id', url: '/api/' + data.properties[property]["type"].substring(data.properties[property]["type"].indexOf("models.") + "models.".length) }); FKAdapters[property] = new $.jqx.dataAdapter(FKSources[FKSources.length - 1]); dataFieldsArr.push({ name: property + 'Id', value: property + 'Id', values: { source: FKAdapters[property].records, value: 'id', label: data.fkAttributes[property] }, type: 'int' }); dataFieldsArr.push({ name: property + 'Nombre', type: 'string', map: property + '>' + data.fkAttributes[property] }); columnsArr.push( { text: data.properties[property]["title"], columntype: 'dropdownlist', datafield: property + 'Id', width: 150, filtertype: 'checkedlist', displayfield: property + 'Nombre', createeditor: function (row, value, editor) { editor.jqxDropDownList({ source: FKAdapters[FKPropertySelection], displayMember: data.fkAttributes[FKPropertySelection], valueMember: 'id' }); } } ); } else if (data.properties[property]["type"].indexOf("integer") > -1) { dataFieldsArr.push({ name: property, type: 'int' }); columnsArr.push({ text: data.properties[property]["title"], datafield: property, width: 80, cellsalign: 'right' }); } else if (data.properties[property]["type"].indexOf("boolean") > -1) { dataFieldsArr.push({ name: property, type: 'bool' }); columnsArr.push({ text: data.properties[property]["title"], datafield: property, width: 65, columntype: 'checkbox' }); } else if (data.properties[property]["format"].indexOf("date") > -1) { dataFieldsArr.push({ name: property, type: 'date' }); columnsArr.push({ text: data.properties[property]["title"], datafield: property, width: 150, columntype: 'datetimeinput', cellsalign: 'right', cellsformat: 'd' }); } else if (data.properties[property]["format"].indexOf("time") > -1) { dataFieldsArr.push({ name: property, type: 'date' }); columnsArr.push({ text: data.properties[property]["title"], datafield: property, width: 100, columntype: 'datetimeinput', cellsalign: 'right', cellsformat: 't', createeditor: function (row, column, editor) { editor.jqxDateTimeInput({ showTimeButton: true, showCalendarButton: false }); } }); } else { dataFieldsArr.push({ name: property, type: 'string' }); columnsArr.push({ text: data.properties[property]["title"], datafield: property, width: 150 }); } } columnsArr.push({ text: 'Delete', datafield: 'Delete', columntype: 'button', width: 90, cellsrenderer: function () { return "Delete row"; }, buttonclick: function (row) { var deleteConfirm = confirm("Sure?"); if (deleteConfirm) { var id = $("#jqxgrid").jqxGrid('getrowid', row); deleteEntity(entity, id, $('input[name="__RequestVerificationToken"]').val()); $('#jqxgrid').jqxGrid('deleterow', id); } } }); var source = { datatype: "json", datafields: dataFieldsArr, id: 'id', url: '/api/' + entity, addrow: function (rowid, rowdata, position, commit) { var token = $('input[name="__RequestVerificationToken"]').val(); //console.log(rowid); //console.log(rowdata); $.ajax({ url: "/api/" + entity, method: "post", data: JSON.stringify(rowdata), processData: false, headers: { "RequestVerificationToken": token, "Content-type": "Application/json" }, success: function () { commit(true); //reload Data in order to manage correctly new data $("#jqxgrid").jqxGrid( { source: dataAdapter, sortable: true, filterable: true, editable: true, showeverpresentrow: true, everpresentrowposition: "top", selectionmode: 'singlecell', editmode: 'dblclick', theme: 'light', columns: columnsArr }); }, error: function (xhr) { console.log(xhr); commit(false); } }); }, updaterow: function (rowid, rowdata, commit) { //console.log(rowdata); var output = rowdata; for (property in output) { if (output[property] instanceof Date && schema.properties[property].format === "time") { output[property] = output[property].getHours() + ":" + output[property].getMinutes(); //console.log('hola'); } if (output[property] instanceof Date && schema.properties[property].format === "date") { output[property] = output[property].getFullYear() + "-" + output[property].getMonth() + 1 + '-' + output[property].getDate(); } } /*console.log(event.args.row);*/ console.log(output); console.log(rowdata); var token = $('input[name="__RequestVerificationToken"]').val(); $.ajax({ url: "/api/" + entity + "/" + rowdata.uid, cache: false, method: "put", data: JSON.stringify(rowdata), processData: false, headers: { "RequestVerificationToken": token, "Content-type": "Application/json" }, success: function () { console.log("Okey dokey!"); commit(true); }, error: function () { alert("El dato no se ha actualizado correctamente"); } }); } }; var dataAdapter = new $.jqx.dataAdapter(source); $("#jqxgrid").jqxGrid( { source: dataAdapter, width: '100%', sortable: true, filterable: true, editable: true, showeverpresentrow: true, everpresentrowposition: "top", selectionmode: 'singlecell', editmode: 'dblclick', theme: 'light', columns: columnsArr }); }, error: function () { alert("Error Getting Data"); } }); } $("#jqxgrid").on('cellselect', function (event) { FKPropertySelection = event.args.datafield.substring(0, event.args.datafield.indexOf('Id')); }); 

—JSGrid.cshtml— -JSGrid.cshtml-

 @{ ViewData["Title"] = "JSGrid"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>JSGrid for @ViewBag.entity</h2> @Html.AntiForgeryToken() <div id="jqxgrid"> </div> @section scripts { <link rel="stylesheet" href="~/lib/jqwidgets/styles/jqx.base.css" type="text/css" /> <link rel="stylesheet" href="~/lib/jqwidgets/styles/jqx.light.css" type="text/css" /> <script type="text/javascript" src="~/lib/jqwidgets/jqxcore.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxdata.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxbuttons.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxscrollbar.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxmenu.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxlistbox.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxdropdownlist.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxcheckbox.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxgrid.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxdatetimeinput.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxcalendar.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxtooltip.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxgrid.selection.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxgrid.columnsresize.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxgrid.filter.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxgrid.sort.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxgrid.pager.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxgrid.grouping.js"></script> <script type="text/javascript" src="~/lib/jqwidgets/jqxgrid.edit.js"></script> <script type="text/javascript" src="~/js/JSGrid.js"></script> <script> entity = '@ViewBag.entity'; createGrid(); </script> } 

Thanks in advance! 提前致谢!

All right! 好吧! I found my solution here: 我在这里找到了解决方案:

https://www.jqwidgets.com/community/topic/rowdata-changed-even-before-statement-to-do-so/#post-98256 https://www.jqwidgets.com/community/topic/rowdata-changed-even-before-statement-to-do-so/#post-98256

and here: 和这里:

https://www.jqwidgets.com/community/topic/difference-between-refreshdata-and-updatebounddata/ (¡Haya PAZ! - Les luthiers) https://www.jqwidgets.com/community/topic/difference-between-refreshdata-and-updatebounddata/(¡Haya PAZ!-Les luthiers)

I partially solved my issue reloading binded data with 'updatebounddata' method from jqxgrid. 我部分解决了用jqxgrid的“ updatebounddata”方法重新加载绑定数据的问题。

Just in case someone has the same problem, i will leave here my updateRow function updated. 万一有人遇到相同的问题,我将在这里将updateRow函数更新。 Look at the ajax block: 看一下ajax块:

updaterow: function (rowid, rowdata, commit)
                {
                    //console.log(rowdata);
                    var output = rowdata;

                    for (property in output) {
                        if (output[property] instanceof Date && schema.properties[property].format === "time") {
                            output[property] = output[property].getHours() + ":" + output[property].getMinutes();
                            //console.log('hola');
                        }
                        if (output[property] instanceof Date && schema.properties[property].format === "date")
                        {
                            output[property] = output[property].getFullYear() + "-" + (output[property].getMonth() + 1) + '-' + output[property].getDate();
                        }
                    }

                    //console.log(event.args.row);
                    //console.log(output);
                    //console.log(rowdata);

                    var token = $('input[name="__RequestVerificationToken"]').val();

                    $.ajax({
                        url: "/api/" + entity + "/" + rowdata.uid,
                        cache: false,
                        method: "put",
                        data: JSON.stringify(rowdata),
                        processData: false,
                        headers: {
                            "RequestVerificationToken": token,
                            "Content-type": "Application/json"
                        },
                        success: function () {
                            console.log("Okey dokey!");
                            commit(true);
                            $("#jqxgrid").jqxGrid('updatebounddata');
                        },
                        error: function () {
                            alert("El dato no se ha actualizado correctamente");
                            $("#jqxgrid").jqxGrid('updatebounddata');
                        }
                    });
                }

Hope this will help someone in the future! 希望这对以后的人有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 setcellvalue不适用于JQWidget网格中的所有页面 - setcellvalue not working on all pages in JQWidget grid 推送覆盖先前的ui-grid行数据 - push overwriting previous ui-grid rowdata 如果 rowData 为空,如何隐藏 ag-grid? - How to hide ag-grid if rowData is null? 在jQWidget Grid中的超链接click事件上警报选择性数据字段 - Alert selective datafield in jQWidget Grid on hyperlink click event react 甚至在获取 params.id 之前就尝试渲染页面,因此每次只运行 if 语句 - react is trying to render the page even before the params.id is being fetched so only the if statement is being running every time AgGrid / Angular 2:无法访问setRowData()。 网格渲染,不显示rowData - AgGrid/Angular 2: No access to setRowData(). grid renders, no rowData is displayed 为 rowData 赋值,但数组给出长度为 0 - Ag-grid - Assigning value to rowData but Array giving length 0 - Ag-grid 有没有办法动态更改 JQwidget 网格列的数据源以及列名 - Is there a way to dynamically change data source of JQwidget grid column as well as column names 如何在带有文本的 if 语句中修复“缺少;在语句之前” - How do I fix "Missing ; before statement" in a if statement with text 即使之前的 console.log 显示该语句应该运行,if 语句也不会运行 - if statement doen't run even though the console.log before that shows the statement should run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM