2016-06-08 60 views
0

我正在加載數據,然後允許使用複選框對其進行過濾。每當數據被複選框更新時,我都必須解析數據中的日期字段。以下是我的腳本。這適用於第一次點擊複選框但失敗後續點擊......併產生此錯誤:任何想法,爲什麼發生這種情況?使用D3解析過濾的數據

Uncaught TypeError: t.slice is not a function

var apiData = [ {resource_type: "Books", date_posted: "3/8/2007",total_donations:43}, 
        {resource_type: "Supplies", date_posted: "11/11/2002",total_donations:65}]; 

$(":checkbox").change(function() { 
    var dataSet = apiData.filter(function(el) { 
    var checkboxes = document.getElementsByName('result'); 
    var index = 0; 
    var found = false; 
    while (index < checkboxes.length) { 
     if (checkboxes[index].checked && el.resource_type == checkboxes[index].value) found = true; 
     ++index; 
    } 
    return found; 
    }); 
    ///Need to figure out how to evaluate this!!!***********************************************************************************  
    var dateFormat = d3.time.format("%m/%d/%Y"); 
    dataSet.forEach(function(d) { 
    d.date_posted = dateFormat.parse(d.date_posted); 
    d.date_posted.setDate(1); 
    d.total_donations = +d.total_donations; 
    }); 
}); 
+1

我們可以看到'apiData'的值嗎? – torresomar

+0

我認爲這就是數據通過的方式。我可能已經發現了一個線索......它看起來像在第一次點擊複選框時解析數據。然後在第二次點擊它嘗試再解析已經解析的數據......可能是一個問題?也許如果我加載apiData並解析它,然後應用過濾它將工作...將嘗試和更新。 –

+0

更新!這是問題所在。一旦數據被解析,它就不能再被解析。因此,在第一次更改後,複選框會發生變化。我使用了複選框更改功能之外的解析功能。感謝您的幫助。 –

回答

0

所需的date_posted字段到.change()函數的外面只是一次進行解析。解析的字段不能再被解析。

var apiData = [ {resource_type: "Books", date_posted: "3/8/2007",total_donations:43}, 
         {resource_type: "Supplies", date_posted: "11/11/2002",total_donations:65}]; 

    var dateFormat = d3.time.format("%m/%d/%Y"); 
    apiData.forEach(function(d) { 
    d.date_posted = dateFormat.parse(d.date_posted); 
    d.date_posted.setDate(1); 
    d.total_donations = +d.total_donations; 
    }); 

$(":checkbox").change(function() { 
    var dataSet = apiData.filter(function(el) { 
    var checkboxes = document.getElementsByName('result'); 
    var index = 0; 
    var found = false; 
    while (index < checkboxes.length) { 
     if (checkboxes[index].checked && el.resource_type == checkboxes[index].value) found = true; 
     ++index; 
    } 
    return found; 
    }); 
});