kendo grid:过滤数组对象

时间:2013-09-26 15:57:26

标签: c# javascript kendo-ui kendo-grid

我想在剑道网格上过滤它的模型有对象列表。

我的过滤器无法处理联系人列表:

这是我的javascript代码:

var typeFilter;
var contactsFilter;
var datesFilter;
var ByFiler;
var zeroFilter = {
    field: "ID",
    operator: "eq",
    value: 0
};

function filterOnMultiSelectContacts(e) {

    var filters;
    for (var i = 0; i < $('#ContactsDropDownFilter').data("kendoMultiSelect").dataItems().length; i++)
    {
        filters =[
                    {
                        field: "Contacts.ContactID",
                        operator: "eq",
                        value: Number($('#ContactsDropDownFilter').data("kendoMultiSelect").dataItems()[i].ContactID)
                    },
                        zeroFilter
        ]
    }

    contactsFilter = {
        logic: "or",
        filters: filters
    };
    applyFilters();
}

function TypeChanged(e) {
    var type = $("#SelectedType").val();
    var value = Number(type);

    if (value === -1) {
        typeFilter = null;
    }
    else {
        typeFilter = {
            logic: "or",
            filters: [
                {
                    field: "Type",
                    operator: "eq",
                    value: value
                },
                zeroFilter
            ]
        };
    }
    applyFilters();
}


function applyFilters() {
    var ds = $("#Grid").data("kendoGrid").dataSource;
    var filters = [];

    if (typeFilter) filters.push(typeFilter);
    if (contactsFilter) filters.push(contactsFilter);
    if (datesFilter) filters.push(datesFilter);
    ds.filter(filters);
}

我日期和我的类型上的过滤器工作正常,因为字段(模型)不是数组。另一方面,我的filterOnMultiSelectContacts()功能无法正常工作。我猜我的错误是这一行field: "Contacts.ContactID"。这条线通过正确的ContactID来正常工作:

Number($('#ContactsDropDownFilter').data("kendoMultiSelect").dataItems()[i].ContactID)

顺便说一句,这是我的网格剃刀数据源:

    .DataSource(dataSource => dataSource
    .Ajax()
    .ServerOperation(false)
    .Events(e => e.Sync("SyncLogEventGrid"))
    .Model(m =>
    {
        m.Id(t => t.LogEventID);
        m.Field(u => u.CreatedByContact).DefaultValue(UserHelper.GetCurrentUserContact(Request.RequestContext));
        m.Field(u => u.CreatedBy).DefaultValue(UserHelper.GetCurrentUserContact(Request.RequestContext).ContactID);
        m.Field(u => u.Contacts).DefaultValue(new List<ContactModel>());
        m.Field(u => u.LogEventAttachments).DefaultValue(new List<LogEventAttachmentModel>());
    })
    )

修改

我在我的模型中添加了ContactIds,其中包含联系人的ID(不是整个对象)。我想出了这个,但它还没有用......

function filterOnMultiSelectContacts(e) {
    contactsFilter = null;
    var ContactIdsList = new Array();
    for (var i = 0; i < $('#LogEventContactsDropDownFilter').val().length; i++)
    {
        ContactIdsList.push(Number($('#LogEventContactsDropDownFilter').val()[i]));
    }

    var filters;
    if ($('#LogEventContactsDropDownFilter').data("kendoMultiSelect").dataItems().length > 0) {
        filters = [
                    {
                        field: "ContactIDs",
                        operator: "eq",
                        value: ContactIdsList
                    }
                    ,
                    zeroFilter
        ];
        contactsFilter = {
            logic: "or",
            filters: filters
        };
    }
    else
    {
        contactsFilter = null;
    }
    applyFilters();
}   

1 个答案:

答案 0 :(得分:6)

我终于成功了,这是解决方案:

MyFunctions = {
    getIntersect: function (arr1, arr2) {
        var intersect = [];

        for (i = 0; i < arr2.length; i++) {
            if ($.inArray(arr2[i], arr1) > -1)
                intersect.push(arr2[i]);
        }

        return intersect;
    }
}

function filterOnMultiSelectContacts(e) {
    filterBtnClicked(e, $(this));
    contactsFilter = null;

    var filters;
    if ($('#LogEventContactsDropDownFilter').data("kendoMultiSelect").dataItems().length > 0)
    {
        var ContactIdsList = new Array();
        for (var i = 0; i < $('#LogEventContactsDropDownFilter').val().length; i++) {
            ContactIdsList.push(Number($('#LogEventContactsDropDownFilter').val()[i]));
        }
        filters = [
                    {
                        field: "ContactIDs",
                        operator: function (items, filterValue) {

                            var intersect = MyFunctions.getIntersect(items, ContactIdsList);
                            if (intersect.length > 0) return true;
                            return false;
                        },
                        value: ContactIdsList
                    }
                    ,
                    zeroFilter
                ];
        contactsFilter = {
            logic: "or",
            filters: filters
        };
    }

    else
    {
        contactsFilter = null;
    }
    applyFilters();
}

function applyFilters() {
    var ds = $("#Grid").data("kendoGrid").dataSource;
    var filters = [];

    if (typeFilter) filters.push(typeFilter);
    if (contactsFilter) filters.push(contactsFilter);
    if (datesFilter) filters.push(datesFilter);
    ds.filter(filters);
}

我需要创建一个与给定数组相交的函数与网格数据源的项目。