0

ここで課題が発生しました。これを実現するために数時間を費やしましたが、運がありませんでした。私のデータには製品 ID とタイムスタンプがあります。製品 ID ごとにデータを組み合わせることができましたが、これらの記録を月ごとに分割する必要もあります (タイムスタンプの助けを借りて)。

現時点では、月に関係なくすべてのレコードが表示されますが、次のようなビューを実現したいと考えています:

製品 1 | 1月 (2) | 2 月 () | 3月 (1) | 4月 () | など
製品 2 | ヤン () | 2月 (2) | 3 月 () | 4月 (3) | 等

私が使用する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 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 に答える