2

我对angularjs和flot条形图很陌生

我试图在基于过滤器的点击列表中显示过滤后的数据。

我创建了一个自定义指令来绘制图表和一个自定义过滤器,我将其与通过 click 方法获取的数据一起使用,但未显示列表。

请帮忙看看

http://jsfiddle.net/6h1gL2sh/22/

App.directive('chart', function () {
    return {
        restrict: 'EA',
        link: function (scope, elem, attrs) {
            var chart = null,
                options = {
                    series: {
                        stack: true,
                        bars: {
                            show: true,
                            clickable: true,
                            barWidth: 0.1,
                            align: "center"
                        }
                    },
                    axisLabels: {
                        show: true
                    },

                    xaxis: {
                        axisLabel: 'Test Dates',
                        axisLabelUseCanvas: true,
                        axisLabelFontSizePixels: 12,
                        axisLabelPadding: 5,
                        mode: "categories",
                        tickLength: 0
                    },
                    yaxis: {
                        axisLabel: 'Pass/Fail Count',
                        axisLabelUseCanvas: true,
                        axisLabelFontSizePixels: 12,
                        axisLabelPadding: 5

                    },
                    grid: {
                        hoverable: true,
                        clickable: true
                    }

                }
            var data = scope[attrs.ngModel];

            var getKeyByValue = function (obj, value) {
                for (var prop in obj) {
                    if (obj.hasOwnProperty(prop)) {
                        if (obj[prop] === value) return prop;
                    }
                }
            }


            scope.$watch(attrs.ngModel, function (v) {
                if (!chart) {
                    chart = $.plot(elem, v, options);
                    elem.show();
                } else {
                    chart.setData(v);
                    chart.setupGrid();
                    chart.draw();
                }
            });
            elem.bind("plotclick", function (event, pos, item) {
                if (item) {
                    scope.show = false
                    scope.plotdate = getKeyByValue(item.series.xaxis.categories, item.dataIndex);
                    switch (item.series.label) {
                        case 'Passed':
                            scope.details = scope.arr.passdetails;
                            break;
                        case 'Failed':
                            scope.details = scope.arr.faildetails;
                            break;
                            scope.$apply();
                    }
                }
                console.log('details= ', scope.details)
                console.log('Plotdate= ', scope.plotdate)
                console.log('Show= ', scope.show)


            });


        }
    }

});
App.filter('getdata', function () {

    return function (items, date) {

        var arrayToReturn = [];
        for (var i in items) {
            if (items[i].start_date == date) {
                arrayToReturn.push(items[i]);
            }
        }

        return arrayToReturn;
    };
});
4

1 回答 1

1

我终于修好了。我在我的案例语句块中应用了 scope.$apply() 。

我还在指定控制器的 div 之外的 div 中调用我的表达式

见 js Fiddle http://jsfiddle.net/6h1gL2sh/24/

 elem.bind("plotclick", function (event, pos, item) {
            if (item) {
                scope.show=false
                scope.plotdate= getKeyByValue(item.series.xaxis.categories, item.dataIndex);
                scope.stats=item.series.label
                switch (scope.stats){
                case 'Passed':
                    scope.details = scope.arr.passdetails;
                    break;
                case 'Failed':
                    scope.details = scope.arr.faildetails;
                    break;

                }
            }
            scope.$apply();
            console.log('details= ', scope.details)
             console.log('Plotdate= ', scope.plotdate)
              console.log('Show= ',scope.show)


        });


    }
}
于 2015-04-14T08:56:13.987 回答