Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

javascript - Cross-filter using drop downs

I have two drop-downs. I want that whenever a selection is made in either of the drop-downs, a function called intersect be called which in turn calls the functions loadData and loadData1.My code is attached here.

<select onchange="intersect()" id="metric" class="wrapper-dropdown">
<option value ="190">190</option>
    <option value ="90">90</option>
    <option value ="100">100</option>
    <option value ="300">300</option>
</select>

<select onchange="intersect()" id="metric1" class="wrapper-dropdown">
<option value ="100">100</option>
    <option value ="200">200</option>
    <option value ="0">0</option>
</select>
<script>

var intersect= function() {

var total_90 = function() {
var data = [
 {date: "2011-11-14T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab"},
 {date: "2011-11-14T16:20:19Z", quantity: 2, total: 190, tip: 100, type: "tab"},
 {date: "2011-11-14T16:28:54Z", quantity: 1, total: 300, tip: 200, type: "visa"},
 {date: "2011-11-14T16:30:43Z", quantity: 2, total: 90, tip: 0, type: "tab"},
 {date: "2011-11-14T16:48:46Z", quantity: 2, total: 90, tip: 0, type: "tab"},
 {date: "2011-11-14T16:53:41Z", quantity: 2, total: 90, tip: 0, type: "tab"},
 {date: "2011-11-14T16:54:06Z", quantity: 1, total: 100, tip: 0, type: "cash"},
 {date: "2011-11-14T16:58:03Z", quantity: 2, total: 90, tip: 0, type: "tab"},
 {date: "2011-11-14T17:07:21Z", quantity: 2, total: 90, tip: 0, type: "tab"},
 {date: "2011-11-14T17:22:59Z", quantity: 2, total: 90, tip: 0, type: "tab"},
 {date: "2011-11-14T17:25:45Z", quantity: 2, total: 200, tip: 0, type: "cash"},
 {date: "2011-11-14T17:29:52Z", quantity: 1, total: 200, tip: 100, type: "visa"}
 ];
 var ndx = crossfilter(data);
 var totalDim = ndx.dimension(function(d) { return d.total; });  
 var total_90 =     totalDim.filter(document.getElementById('metric').selectedOptions[0].value); 
return total_90;
  };
var tip_90 = function() {
var data = [
{date: "2011-11-14T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab"},
{date: "2011-11-14T16:20:19Z", quantity: 2, total: 190, tip: 100, type: "tab"},
{date: "2011-11-14T16:28:54Z", quantity: 1, total: 300, tip: 200, type: "visa"},
{date: "2011-11-14T16:30:43Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:48:46Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:53:41Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:54:06Z", quantity: 1, total: 100, tip: 0, type: "cash"},
{date: "2011-11-14T16:58:03Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T17:07:21Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T17:22:59Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T17:25:45Z", quantity: 2, total: 200, tip: 0, type: "cash"},
{date: "2011-11-14T17:29:52Z", quantity: 1, total: 200, tip: 100, type: "visa"}
];
 var ndx = crossfilter(data);
 var tipDim = ndx.dimension(function(d) { return d.tip; });  
 var tip_90 = tipDim.filter(document.getElementById('metric1').selectedOptions[0].value); 
 return tip_90;
 }
 var result = [];

   for (var i = 0; i < total_90.length; i++){
        if (total_90[i].total == 90 && total_90[i].tip == 0)
        {

            result.push(total_90[i]);
        }
    }

   for (var i = 0; i < tip_90.length; i++){
        if (tip_90[i].total == 90 && tip_90[i].tip == 0)
        {
            result.push(tip_90[i]);
        }
    }
function print_filter(filter){
var f=eval(filter);
if (typeof(f.length) != "undefined") {}else{}
if (typeof(f.top) != "undefined") {f=f.top(Infinity);}else{}
if (typeof(f.dimension) != "undefined") {f=f.dimension(function(d) { return "";}).top(Infinity);}else{}
console.log(filter+"("+f.length+") = "+JSON.stringify(f).replace("[","   [
	").replace(/},/g,"},
	").replace("]","
]"));
    } 
    print_filter("result"); 
     }
    </script>
    </body>

So, at any given instance if I select '90' in the first drop down and '0' is already selected in the second drop down,then I want all the rows from both the JSON arrays which have total=90 and tip=0 to be printed on the console.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I dont know what's so difficult here...

I want that whenever a selection is made in either of the drop-downs, a function called intersect be called which in turn calls the functions loadData and loadData1.

<select onchange="intersect()" id="metric" class="wrapper-dropdown">
    <option value ="190">190</option>
    <option value ="90">90</option>
    <option value ="100">100</option>
    <option value ="300">300</option>
</select>

<select onchange="intersect()" id="metric1" class="wrapper-dropdown">
    <option value ="100">100</option>
    <option value ="200">200</option>
    <option value ="0">0</option>
</select>

with

function loadData() {
    alert("loadData reached");
}
function loadData1() {
    alert("loadData1 reached");
}
function intersect() {
    loadData();    
    loadData1();
}

Example: Fiddle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...