AngularJS,自定义filter实现文字和拼音的双过滤

前言

这个功能在ng-repeat中过滤内容的时候会用到
这个filter简单来说就是同时过滤两个字段或者多个字段达到我们想要的实际效果:文字和拼音双过滤;

代码

keeApp.filter('filterPinyin',function(){
    return function(inputArray,value){
        var array = [];    //定义返回的新数组;
        if(value==undefined||value==null){
            array=inputArray;    //当过滤条件为空的时候返回全部的内容;
        }
        else{
            for(var i=0;i<inputArray.length;i++){
                if(inputArray[i].aliasName.indexOf(value)!= -1){
                    array.push(inputArray[i]);//过滤第一个字段,如果不符合条件则判断第二个字段
                }
                else{
                    if(inputArray[i].spelling.indexOf(value)!= -1){
                        array.push(inputArray[i]);
                    }
                }
            }
        }
        return array;
    }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先需要创建一个注解: ``` @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface FilterRequired { boolean value() default true; } ``` 这个注解只有一个 boolean 类型的属性 value,用来标识是否需要过滤。 接下来在实现 WebMvcConfigurer 接口的类中重写 addInterceptors 方法,通过该方法为 Controller 中使用了 FilterRequired 注解的方法添加拦截器: ``` @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Autowired private MyInterceptor myInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myInterceptor).addPathPatterns("/**") .excludePathPatterns("/login").excludePathPatterns("/register") .excludePathPatterns("/error").excludePathPatterns("/static/**") .excludePathPatterns("/favicon.ico") .excludePathPatterns(getExcludePathPatterns()); registry.addInterceptor(filterInterceptor()).addPathPatterns("/**") .excludePathPatterns("/login").excludePathPatterns("/register") .excludePathPatterns("/error").excludePathPatterns("/static/**") .excludePathPatterns("/favicon.ico") .excludePathPatterns(getExcludePathPatterns()) .excludePathPatterns(getExcludeFilterPathPatterns()); } private FilterInterceptor filterInterceptor() { return new FilterInterceptor(); } private List<String> getExcludeFilterPathPatterns() { List<String> excludePathPatterns = new ArrayList<>(); for (Method method : getClass().getDeclaredMethods()) { if (method.isAnnotationPresent(FilterRequired.class)) { FilterRequired filterRequired = method.getAnnotation(FilterRequired.class); if (!filterRequired.value()) { String pathPattern = getPathPattern(method); excludePathPatterns.add(pathPattern); } } } return excludePathPatterns; } private String getPathPattern(Method method) { RequestMapping requestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class); if (requestMapping != null && requestMapping.value().length > 0) { return requestMapping.value()[0]; } return null; } } ``` 其中,filterInterceptor() 方法返回 FilterInterceptor 类的实例,用于实现具体的拦截逻辑。getExcludeFilterPathPatterns() 方法用于获取所有使用了 FilterRequired 注解且 value 值为 false 的方法所对应的请求路径,从而将这些请求路径排除在拦截器的拦截范围之外。getPathPattern() 方法用于获取方法上使用的 RequestMapping 注解中的请求路径。 最后,在 Controller 中需要过滤的方法上添加 FilterRequired 注解,并设置其 value 值为 true 或 false,如下所示: ``` @RestController public class UserController { @Autowired private UserService userService; @PostMapping("/login") @FilterRequired(false) public Result login(@RequestBody User user) { User userResult = userService.login(user.getUsername(), user.getPassword()); if (userResult != null) { return Result.success(userResult); } else { return Result.error("用户名或密码错误"); } } } ``` 这样就可以通过自定义注解来实现是否过滤的功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值