gpt4 book ai didi

angularjs - 我们可以在 typescript 中编辑绝对键入的文件以更改某些参数类型吗?

转载 作者:行者123 更新时间:2023-12-02 11:06:53 24 4
gpt4 key购买 nike

  • 我们可以在Typescript中编辑绝对类型的文件吗?
  • 将ui-grid与 typescript 一起使用时,我遇到了一种情况,
    ui-grid.d.ts文件使日期过滤器起作用。我需要根据ui-grid中显示的结果而非日期来过滤日期
    原始数据(我从Web API获取的数据)。

  • 这是 代码段:

    ui-grid.d.ts文件代码:(在 interface IFilterOptions下)
    condition?:  number;

    对应的 typescript 代码:
    column.filter = {
    condition: (searchTerm, cellValue, row, column) => {
    var date = this.$filter("date")(row.entity.Date, this.masks.date.angular)
    return (date + '').toLowerCase().indexOf(searchTerm.toLowerCase().replace(/\\/g, "")) !== -1;
    }
    }

    我遇到的错误如下:
    Type '(searchTerm:any, cellValue:any, row:any, coloumn:any) => boolean' is not assignable to type 'number'

    enter image description here

    之后,我对过滤器对象进行了更改,以返回一个数字,如下所示:
    column.filter = {
    condition: (searchTerm, cellValue, row, column) => {
    var date = this.$filter("date")(row.entity.Date, this.masks.date.angular)
    var res: number = (date + '').toLowerCase().indexOf(searchTerm.toLowerCase().replace(/\\/g, "")) !== -1 ? 1 : 0;
    return res;
    }
    }

    但是在此之后,我出现了错误:
    Type '(searchTerm:any, cellValue:any, row:any, coloumn:any) => number' is not assignable to type 'number'

    之后,我在绝对类型的文件(即ui-grid.d.ts文件)中进行了如下更改:

    interface IFilterOptions

    我变了
    condition?:  number; 


    condition?: (searchTerm: any, cellValue: any, row: any, column: any) => boolean | number;

    因此对我有用的相应过滤器功能如下:
    column.filter = {
    condition: (searchTerm, cellValue, row, column) => {
    var date = this.$filter("date")(row.entity.Date, this.masks.date.angular)
    return (date + '').toLowerCase().indexOf(searchTerm.toLowerCase().replace(/\\/g, "")) !== -1;
    }
    }

    这是解决这个问题的正确方法吗?
    如果上述解决方案不好,或者编辑绝对类型的文件不是一个好习惯,谁能指导我找到正确的解决方案?

    而且还用
    column.filterCellFiltered = true;

    而不是 filter函数,但是没有用。这也是一个令人担忧的原因,为什么它不起作用,因为我需要根据ui网格中的显示结果而不是原始数据来过滤日期。谁能解释为什么它不起作用?

    请帮助我了解正确的方法。让我知道是否有人需要其他详细信息。提前致谢。

    Angular JS版本: AngularJS v1.4.7
    Angular JS绝对键入版本: Angular JS 1.4+
    ui-grid版本:* ui-grid-v3.0.5
    ui-grid绝对输入版本: ui-grid v3.0.3
    typescript 版本: 1.0.3.0

    最佳答案

    我也有同样的问题,
    在函数开头添加“ ”应该可以解决问题。

    像这样 :

              condition: <any>((searchTerm, cellValue, row, column) => 
    {
    return cellValue.indexOf(searchTerm) > -1
    })


    并且不要忘了括号()。

    关于angularjs - 我们可以在 typescript 中编辑绝对键入的文件以更改某些参数类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34857902/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com