gpt4 book ai didi

javascript - JsGrid 无法强制更新值

转载 作者:行者123 更新时间:2023-12-03 02:32:24 25 4
gpt4 key购买 nike

我试图在编辑时将格式值强制到“导入”列中。它应该是一个字符串,表示具有 2 个固定十进制数字的数字。示例:1152.30。这是代码。

HTML

<div id="grid"></div>

JS代码

const ACCOUNT_GRID_FIELD_NAME = "account";
const IMPORT_GRID_FIELD_NAME = "import";

$("#grid").jsGrid(createGridConfiguration());

function createGridConfiguration() {
let result;

result = {
inserting: false,
editing: true,
sorting: false,
paging: false,

data: [],

onItemInserting: onGridItemInserting,
onItemUpdating: onGridItemUpdating,

fields: [{
name: ACCOUNT_GRID_FIELD_NAME,
title: "Account",
type: "text",
width: 300,
}, {
name: IMPORT_GRID_FIELD_NAME,
title: "Import",
type: "text",
width: 200,
}, {
type: "control",
width: 150,
editButton: true,
modeSwitchButton: false,
headerTemplate: onRenderAddRowHeadder,
}]
}

return result;
}

function onGridItemInserting(args) {
let validAmount;

validAmount = formatAmountForOutput(0);
args.item[IMPORT_GRID_FIELD_NAME] = validAmount;
}

function onGridItemUpdating(args) {
let validAmount, previousAmount;

previousAmount = args.item[IMPORT_GRID_FIELD_NAME];
validAmount = formatAmountForOutput(previousAmount);
args.item[IMPORT_GRID_FIELD_NAME] = validAmount; // Here we have the correct value
}

function formatAmountForOutput(amount) {
let result, valueAsNumber;

if (amount === undefined) {
valueAsNumber = 0;
} else {
valueAsNumber = parseFloat(amount);
if (isNaN(valueAsNumber)) {
valueAsNumber = 0;
}
}

result = valueAsNumber.toFixed(2);

return result;
}

function onRenderAddRowHeadder() {
let result;

result = $("<input>");
result.addClass("jsgrid-button jsgrid-insert-button")
result.attr("type", "button");
result.on("click", onGridInsertItemClick);

return result;
}

function onGridInsertItemClick() {
let grid;

grid = $("#grid");
grid.jsGrid("insertItem");
}

<强> JsFiddle

当我单击插入时,它会触发 onGridItemInserting,因此它会插入正确的值,但是当我尝试在编辑时使用相同的技术时,如果触发 onGridItemUpdating,并生成正确的值(查看注释行),但网格(及其内部的数据)以错误的值结束。

示例:我单击“插入”,然后出现“0.00”。那是对的。现在我点击“0.00”,并修改它,所以我写“120.1234”。当我点击确认时,我期望变成“120.12”,但它存储并显示“120.1234”。

我怎样才能强制获得所需的值?

最佳答案

Its fixed here: http://jsfiddle.net/rn30w2r0/

我在文档中看到编辑后必须刷新网格,请查看 jsGrid github。使用 $("#grid").jsGrid("refresh"); GitHub

您在另一台上设置了错误的数据

args.item[IMPORT_GRID_FIELD_NAME] = validAmount;

谢谢

关于javascript - JsGrid 无法强制更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48680272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com