多列上的Knockoutjs ObservableArray ArrayFilter

时间:2015-10-02 06:27:57

标签: knockout-3.0

我正在尝试通过多列过滤ObservableArray。

我的问题与此question类似,但我的模型不同,因为可观察数组中的每个条目都是可观察对象。

var Employee = function(name, job, state, phone)
{
    this.name = ko.observable(name);
    this.job = ko.observable(job);
    this.state = ko.observable(state);
    this.phone = ko.observable(phone);
};

self.employees = ko.observableArray([
    new Employee("Sarah","Manager","","123456"),
    new Employee("Michelle","Receptionist","California",""),
    new Employee("James","","Florida","1213123"),
    new Employee("Steve","","Team Lead",""),
    new Employee("Britney","","Developer","")
]);

我的数组以表格形式显示,并且具有供用户过滤每列空值的功能。

我正在处理要过滤的动态列表列表(也来自observablearray)

self.rowheader = ko.observableArray(["Name","Job","State","Phone" ]);

我似乎无法弄清楚如何映射这些值并使用ko.utils.arrayFilter

将这些值评估为条件

这里是JSFiddle我现在所处的位置。 感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我已经设法找到了解决方案,但我仍然希望有人可以为此提出更优雅的解决方案。

我必须根据selectedColumns内容动态构造arrayFilter函数的返回条件。 filteredArray observable现在看起来像这样:

public static void areaTriangle(int base, int height)
{
    System.out.println(0.5 * base * height);
}

public static void areaCircle(int radius, double area)
{
    area = Math.PI * (radius * radius);
    System.out.println(area);
}

public static void areaRectangle(int length, int width)
{
    System.out.println(length * width);
}

public static void calcArea()
{
    System.out.println("What type of area do you want to calculate?");
    inputStr = JOptionPane.showInputDialog("Type 1 for triangle, 2 for circle, 3 for rectangle, and 0 for none, then press ENTER.");
    int type = Integer.parseInt(inputStr);

    if (type == 0)
    {
        return;

    }
    else if (type == 1)
    {
        inputStr = JOptionPane.showInputDialog("Enter the measurement of the base, then press ENTER.");
        int base = Integer.parseInt(inputStr);
        inputStr = JOptionPane.showInputDialog("Enter the measurement of the height, then press ENTER.");
        int height = Integer.parseInt(inputStr);
        int areaT = areaTriangle (base, height);
        JOptionPane.showMessageDialog(null, "The area of the triangle is: " + areaT);

    }
    else if (type == 2)
    {
        inputStr = JOptionPane.showInputDialog("Enter the measurement of the radius, then press ENTER.");
        int radius = Integer.parseInt(inputStr);
        double area = Math.PI * (radius * radius);
        int areaC = areaCircle (radius, area);
        JOptionPane.showMessageDialog(null, "The area of the cicle is: " + areaC);

    }
    else if (type == 3)
    {
        inputStr = JOptionPane.showInputDialog("Enter the measurement of the length, then press ENTER.");
        int length = Integer.parseInt(inputStr);
        inputStr = JOptionPane.showInputDialog("Enter the measurement of the width, then press ENTER.");
        int width = Integer.parseInt(inputStr);
        int areaR = areaRectangle(length, width);
        JOptionPane.showMessageDialog(null, "The area of the rectangle is: " + areaR);
    }

}

public static void main(String[] args)
{
    calcArea();
}

我别无选择,只能在那里做一个评估。