我想将从1开始的数字添加到我的循环中添加的每个元素。
这是代码:
function(data, status){
var cleanData = $.parseJSON(data);
var i = 1;
$.each(cleanData, function(key, value, i) {
$( "#list" ).append(
"<div class='large-12 columns'><div class='large-1 columns'>" + (++i) + "</div><div class='large-10 columns'><p> " + value.title + "</p></div><div class='large-1 columns'>" + value.count +"</div></div>" );
});
}
我也尝试过(i+1)
,但后来我得NaN
所以,最终结果将是这样的:
1 title count
2 someOtherTitle count
3 anotherTitle count
答案 0 :(得分:2)
所以我不完全确定出了什么问题,但也许尝试从函数参数中删除i,因为它在范围内无论如何。也可以使用i ++(读取增量)或设置i = 0.否则你将从2开始。
function(data, status){
var cleanData = $.parseJSON(data);
var i = 0;
$.each(cleanData, function(key, value) {
$( "#list" ).append(
"<div class='large-12 columns'><div class='large-1 columns'>" + (i++) + "</div><div class='large-10 columns'><p> " + value.title + "</p></div><div class='large-1 columns'>" + value.count +"</div></div>" );
});
}
答案 1 :(得分:1)
如果您的cleanData
是一个列表,请使用$.each()
这样的
$.each(cleanData, function(key, value) {
...
...ns'>" + (key + 1) + "</di...
答案 2 :(得分:0)
IF OBJECT_ID('tempdb..#my_table') IS NOT NULL
DROP TABLE #my_table
CREATE TABLE #my_table
(
id BIGINT IDENTITY PRIMARY KEY
,userID INT
,login_time DATETIME
,logout_time DATETIME
);
DECLARE @MT TABLE
(
id BIGINT
,userID INT
,login_time DATETIME
,logout_time DATETIME
);
DECLARE @DRes TABLE (
userID INT,
logtime INT
)
DECLARE @Counter1 INT = 0,
@login_time1 DATETIME,
@logout_time1 DATETIME,
@login_time2 DATETIME
INSERT INTO #my_table VALUES
(222222, '2016-05-19 01:06:00.000', '2016-05-19 01:10:00.000')
,(222222, '2016-05-19 01:12:00.000', '2016-05-19 01:20:00.000')
,(333333, '2016-05-24 14:44:00.000', '2016-05-24 14:47:00.000')
,(333333, '2016-05-24 14:59:00.000', NULL)
,(444444, '2016-05-24 14:48:00.000', '2016-05-24 14:49:00.000')
,(444444, '2016-05-24 14:50:00.000', NULL)
,(444444, '2016-05-24 14:51:00.000', NULL)
,(444444, '2016-05-24 14:53:00.000', '2016-05-24 14:59:00.000')
INSERT INTO @MT
SELECT * FROM #my_table
;WITH MaxLog
AS (
SELECT userID, MAX(login_time) AS max_login
FROM @MT
GROUP BY userID
),
DelRec
AS (
SELECT ml.userID
FROM MaxLog ml
LEFT JOIN @MT mt
ON ml.userID = mt.userID
WHERE mt.logout_time IS NULL
AND ml.max_login = mt.login_time
)
DELETE mt
FROM @MT mt
INNER JOIN
DelRec dr
ON mt.userID = dr.userID
WHERE mt.logout_time IS NOT NULL
;WITH StillIn
AS (
SELECT userID, COUNT(*) AS cnt
FROM @MT
GROUP BY userID
HAVING COUNT(*) = 1
)
UPDATE mt
SET logout_time = DATEADD(mi,-1,login_time)
FROM @MT mt
JOIN StillIn si
ON si.UserID = mt.UserId
WHILE @Counter1 < (SELECT MAX(id) FROM @MT)
BEGIN
SET @Counter1 += 1
SET @login_time1 = (SELECT login_time FROM @MT WHERE id = @Counter1)
SET @logout_time1 = (SELECT logout_time FROM @MT WHERE id = @Counter1)
IF @logout_time1 IS NULL
BEGIN
IF @login_time2 IS NULL
BEGIN
SET @login_time2 = @login_time1
END
END
ELSE
BEGIN
IF @login_time2 IS NULL
BEGIN
INSERT INTO @DRes
SELECT userID, DATEDIFF(mi,@login_time1,@logout_time1)
FROM @MT
WHERE id = @Counter1
END
ELSE
BEGIN
INSERT INTO @DRes
SELECT userID, DATEDIFF(mi,@login_time2,@logout_time1)
FROM @MT
WHERE id = @Counter1
SET @login_time2 = NULL
END
END
END
SELECT userID, SUM(logtime)
FROM @DRes
GROUP BY userID