如何在javascript中每次添加新行时都增加ordre值
How to have an incremented value of ordre each time I add a new row in javascript
我有这个代码来增加订单值:
$scope.addElement = function (_ordre) {
console.log("_ordre 1"+_ordre);
if(_ordre == undefined){
_ordre = 0;
if( _ordre == 0){
_ordre = (parseInt(_ordre) + 1).toString();
}
}
console.log("_ordre 2 "+_ordre);
console.log("_ordre 3 "+_ordre);
var tmpElement = {
ordre:(_ordre === undefined) ? (parseInt($scope.finalOperationsList[$scope.finalOperationsList.length-1].ordre) + 1 ): _ordre,
temps : '',
designation:'',
disable : false
};
if ($scope.finalOperationsList.indexOf(tmpElement) == -1) {
$scope.finalOperationsList.push(tmpElement);
var t = parseInt(_ordre)+1; //_ordre is a string
_ordre =t;
}
console.log("dern "+_ordre);
};
当我试图在表中添加新行时,我总是在控制台中得到这样的结果:
_ordre 1undefined
_ordre 2 1
_ordre 3 1
dern 2
问题是,当我试图在我的表中添加一个新的行,每次都增加一个顺序时,我会在控制台中得到这个结果(我被困在1个顺序值中)
那么,我该如何更正我的代码,以便每次添加新行时都有一个递增的ordre值呢?
谢谢你的帮助。
更新:感谢zerohero先生的回复,但总是有同样的问题,这是我根据你的回复所做的:
$scope.addElement = function (_ordre) {
console.log("_ordre 1"+_ordre);
if(_ordre == undefined){
_ordre = 0;
}
_ordre = (parseInt(_ordre) + 1).toString();
console.log("_ordre 2 "+_ordre);
_ordre = (parseInt(_ordre) + 1).toString();
console.log("_ordre 3 "+_ordre);
var tmpElement = {
ordre:(_ordre === undefined) ? (parseInt($scope.finalOperationsList[$scope.finalOperationsList.length-1].ordre) + 1 ): _ordre,
temps : '',
designation:'',
codeOperation : 0,
disable : false
};
if ($scope.finalOperationsList.indexOf(tmpElement) == -1) {
$scope.finalOperationsList.push(tmpElement);
}
console.log("dern "+_ordre);
};
我总是在行中得到等于2的"ordre"值我想不想密码里有什么??再次感谢的帮助
这只会触发一次,当_orde未定义时,将_orde设置为1:
if(_ordre == undefined){
_ordre = 0;
if( _ordre == 0){
_ordre = (parseInt(_ordre) + 1).toString();
}
}
然后依次输出:
console.log("_ordre 2 "+_ordre);
console.log("_ordre 3 "+_ordre);
由于没有对_ordre进行任何更改,因此在这两个实例中都将显示1。
将_ordre设置为1以外的任何值的唯一时间是:
var t = parseInt(_ordre)+1; //_ordre is a string
_ordre =t;
这发生在您将tmpElement的ordre设置为1之后。
你的逻辑非常有缺陷。
这样做:
if(_ordre == undefined){
_ordre = 0;
}
_ordre = (parseInt(_ordre) + 1).toString();
console.log("_ordre 2 "+_ordre);
_ordre = (parseInt(_ordre) + 1).toString();
console.log("_ordre 3 "+_ordre);
相关文章:
- NodeJS Fork-每次子流程发送新行时都进行React
- 当行增加时自动递增序列号,并在 jQuery 中删除行时自动重新调整编号顺序
- 在使用 javascript 动态添加行时,我正在增加索引值,但出现错误
- 当数组达到大于 x 的数字时,继续新行
- 如何在javascript中每次添加新行时都增加ordre值
- 在Spring.message属性中添加新行时出错
- 当上一行/当前行被填充时,将新行添加到HTML表中
- 当新行添加到表中时,使用AJAX更新页面
- 当单击在表中创建新行时,它只会短暂出现
- 当添加新行时,输入名称将增加1
- 添加新行时的日期增量
- 当添加新行时,总持续时间值应该会更改,但不会更改;t
- 在向表中添加新行时进行拦截
- 添加新行时,jQuery代码不起作用
- 在jQuery中添加新行时,禁用其他选择框的选择选项
- Jquery在创建新行时不向最后一行添加索引
- 在dojo.gridX中添加新行时如何聚焦第二个Cell
- 在HTML表中加载每行时增加延迟
- 在contenteditable中添加新行时字数错误
- 在添加具有模态形式的新行时,使用jqGrid添加附加参数以发布数据