纯JS完成 treegrid 模糊搜索功能:
$("#dataTab").treegrid({ url: '/Organization/GetOrganizationList', //view: DefaultView, fit: true, pageSize: 20, method: 'post', queryParams: { }, rownumbers: true, pagination: false, singleSelect: true, fitColumns: true, idField: 'Id', treeField: 'Name', parentField: 'ParentId', loadFilter: function (data) { if (bFound) { allData = data; bFound = false; } return data; }, columns: [[ { field: 'Name', title: '@Z("Home.orgName")@*机构名称*@', 300, align: 'left' } ,{ field: 'OrgTypeName', title: '@Z("Home.orgType")@*机构类型*@', 160, align: 'center' } ,{ field: 'OrgCode', title: '@Z("Platform.OrgCode")@*机构编号*@', 100, align: 'center' } , { field: 'ContactName', title: '@Z("Home.linkman")@*联系人*@', 160, align: 'center' } , { field: 'ContactPhone', title: '@Z("Home.contactNumber")@*联系电话*@', 160, align: 'center' } , { field: 'Address', title: '@Z("Home.contactAddress")@*联系地址*@', 200, align: 'center' } , { field: 'Remarks', title: '@Z("Home.describe")@*描述*@', 200, align: 'center' } ]], onLoadSuccess: function (data) { $('#dataTab').treegrid('resize', { function () { return $(this).parent().width() } }); }, toolbar: "#toolbar", onClickRow: function (row) { var currentUserType = '@ViewBag.UserType'; if (currentUserType != '1' && row.ParentId == 0) { $('#org_edit').hide(); $('#org_delete').hide(); } else { $('#org_edit').show(); $('#org_delete').show(); } } });
function doFilter() { var filter = $("#filter").val(); if (filter == "") { $('#dataTab').treegrid('loadData', allData); } else { var newData = new Array(); for (var i = 0; i < allData.length; i++) { var item = allData[i]; if (item.Name.indexOf(filter) != -1) { // 定义一个数组 newData.push(item); } else if (item.children != null && item.children.length > 0) { doChildFilter(item, newData, filter); } } $('#dataTab').treegrid('loadData', newData); } } function doChildFilter(parentItem, newData, filter) { var list = parentItem.children; for (var i = 0; i < list.length; i++) { var item = list[i]; if (item.Name.indexOf(filter) != -1) { // 定义一个数组 newData.push(item); return; } else if (item.children != null && item.children.length > 0) { doChildFilter(item, newData, filter); } } }