2

我想过滤一个剑道网格,它的模型有对象列表。

我的过滤器不适用于联系人列表:

这是我的 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();
}   
4

1 回答 1

8

我终于让它工作了,这是解决方案:

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);
}

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

于 2013-09-27T15:51:32.490 回答