0

我在这里遇到了挑战,花了几个小时来实现这一点,但没有运气。我的数据有一个产品 ID 和一个时间戳。我已经设法按产品 ID 组合数据,但还需要按月打破这些记录(在时间戳的帮助下)。

目前它显示所有记录,无论一个月,但我想实现这样的视图:

产品 1 | 一月 (2) | 二月 () | 三月 (1) | 四月 () | 等
产品 2 | 一月 () | 二月 (2) | 三月 () | 四月 (3) | ETC

我使用的 jquery 代码是:

$(window).load(function() {
var obj ={};
$.each($('.single-record .product'), function(index, item) {
    var record = $(item).data('record');
    if ($('#product-id-'+record).length > 0) obj[record] = $('div[data-record="'+record+'"]').length;
});
$.each(obj,function(i, el){
    $('#product-id-'+i).append('<span>' + el + '</span>');
});
});

我将衷心感谢您的帮助。先感谢您。

请随时编辑我的小提琴:http: //jsfiddle.net/b4zxV/

4

3 回答 3

1

对您的代码几乎没有修改,我认为它有效,

$(window).load(function () {
    var obj = {};
    $.each($('.single-record .product'), function (index, item) {
        var record = $(item).data('record');
        if ($('#product-id-' + record).length > 0) {
            var dateMillis=parseFloat($(item).next('.date').text().trim());
            console.log(dateMillis);
            console.log(new Date(dateMillis));
            var month = new Date(dateMillis).getMonth();
            console.log(month);
            if(obj[record]==undefined){obj[record]=[];}
            if(obj[record][month]==undefined){
                obj[record][month]=1;
            }else{
                ++obj[record][month];
            }
            console.log("record:"+record+" month:"+month+"  - "+obj[record][month]);
        }
    });
    $.each(obj, function (i, el) {
        $prdEl = $('#product-id-' + i);
        el.forEach(function(n,month){
            $prdEl.append('<span>' +getMonthName(month) +'('+ el[month] + ')</span>');
        });
    });
});

function getMonthName(index) {
    var month = new Array();
    month[0] = "January";
    month[1] = "February";
    month[2] = "March";
    month[3] = "April";
    month[4] = "May";
    month[5] = "June";
    month[6] = "July";
    month[7] = "August";
    month[8] = "September";
    month[9] = "October";
    month[10] = "November";
    month[11] = "December";
    return month[index];
}

http://jsfiddle.net/b4zxV/3/

于 2013-10-10T21:26:32.693 回答
1

这应该有效:

$(window).load(function() {
    var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
    var productMonthMappings = [];

    $.each($('.single-record .product'), function(index, item) {
        // get productId
        var productId = $(item).data('record');

        // Create month mappings for this product if not existent
        if (typeof productMonthMappings[productId] === 'undefined' 
            || productMonthMappings[productId] === null) {
            productMonthMappings[productId] = [];

            for (var i = 0; i < 12; i++) {
                // Set initial count to 0
                productMonthMappings[productId][i] = 0;
            }
        }

        // Parse the date and get the month for the current record
        var date = new Date(1000 * $(item).next().html());
        var month = date.getMonth();
        var monthName = months[month];

        // Increment the count for that date for the product
        productMonthMappings[productId][month]++;
    });

    // Print counts per month
    $.each(productMonthMappings, function(i, productMonthMapping){
        if (productMonthMapping !== undefined
            && productMonthMapping !== null
            && $('#product-id-'+i).length > 0) {
                $.each(productMonthMapping, function(j, count){
                    if (count === undefined || count === null) {
                        count = 0;
                    }

                    $('#product-id-'+i).append(
                        ' | ' + months[j] + '(' + count + ')'
                    );
                });
        }
    });
});

小提琴:http: //jsfiddle.net/pjyb8/

于 2013-10-10T22:21:02.623 回答
0

我不太清楚您要做什么,但这听起来像是 jQuery $.map() 的任务。如果您有 jQuery 1.6 或更高版本,请尝试使用 jQuery Map 来获取您想要的集合,如下所示:

var byMonth = $.map($('.single-record .product'), function(val) {
 //check the value against the month or something
  if(val.find('.date').text() =='whatever you want') {
    months.push(......);
    return val;
  }
});

byMonth 将包含满足您条件的所有对象的数组。您还可以使用回调函数推送到数组或修改对象等,只要适合您的总体目标。

JQuery map 遍历一个数组或对象并对每个值执行一个操作,并返回一个修改值的数组。它对于将类似数组的对象转换为数组很有用,但也适用于类似于您想要实现的目标。

请参阅http://api.jquery.com/jQuery.map/上的文档

于 2013-10-10T21:21:22.113 回答