【问题标题】:Semantic UI - Search options are blank语义 UI - 搜索选项为空白
【发布时间】:2020-04-01 17:48:04
【问题描述】:

所以我试图弄清楚 Semantic UI 的搜索模块是如何工作的。我已经在一些测试数据上对其进行了测试:

const testdata = [
    {
    "id": 7,
    "created_at": "2020-03-24 08:40:42",
    "updated_at": "2020-03-25 10:10:59",
    "name": "NameX",
    "email": "namex@email.no",
    "phone": "94860509"
    },
    {
    "id": 8,
    "created_at": "2020-03-25 10:11:10",
    "updated_at": "2020-03-25 10:11:10",
    "name": "NameY",
    "email": "namey@gmail.com",
    "phone": "94860509"
    },
    {
    "id": 9,
    "created_at": "2020-03-31 15:55:06",
    "updated_at": "2020-03-31 15:55:19",
    "name": "Lucifer",
    "email": "lucifer@gmail.com",
    "phone": "58893177"
    },
    {
    "id": 10,
    "created_at": "2020-03-31 15:55:41",
    "updated_at": "2020-03-31 15:55:41",
    "name": "Morningstar",
    "email": "morningstar@gmail.com",
    "phone": "12345678"
    },
    {
    "id": 11,
    "created_at": "2020-04-01 16:13:39",
    "updated_at": "2020-04-01 16:13:39",
    "name": "Superwhale",
    "email": "superwhale@gmail.com",
    "phone": "18181818"
    }
];

目前,它似乎正在搜索。其中有两个带有“名称”一词的名称 - NameXNameY,它给了我两个结果,问题是这些结果只是空白。 [https://i.imgur.com/O6CcAdc.png][1]

这是搜索元素的配置方式:

$('.ui.search').search({
    source: testdata,
    showNoResults: true,
    ignoreDiacritics: true,
    searchFullText: false,
    searchFields: [
        'name'
    ]
});

我一直在阅读文档,它似乎应该可以工作,但它没有。我错过了什么明显的东西吗?

【问题讨论】:

    标签: javascript json search semantic-ui


    【解决方案1】:

    尝试按功能过滤结果

    const filterNameObj = (array) => array.filter(item => {
      if((item.name === 'NameX') || (item.name === 'NameY')) return item
    })
    
    filterNameObj(testdata )
    

    const testdata = [
      {
      "id": 7,
      "created_at": "2020-03-24 08:40:42",
      "updated_at": "2020-03-25 10:10:59",
      "name": "NameX",
      "email": "namex@email.no",
      "phone": "94860509"
      },
      {
      "id": 8,
      "created_at": "2020-03-25 10:11:10",
      "updated_at": "2020-03-25 10:11:10",
      "name": "NameY",
      "email": "namey@gmail.com",
      "phone": "94860509"
      },
      {
      "id": 9,
      "created_at": "2020-03-31 15:55:06",
      "updated_at": "2020-03-31 15:55:19",
      "name": "Lucifer",
      "email": "lucifer@gmail.com",
      "phone": "58893177"
      },
      {
      "id": 10,
      "created_at": "2020-03-31 15:55:41",
      "updated_at": "2020-03-31 15:55:41",
      "name": "Morningstar",
      "email": "morningstar@gmail.com",
      "phone": "12345678"
      },
      {
      "id": 11,
      "created_at": "2020-04-01 16:13:39",
      "updated_at": "2020-04-01 16:13:39",
      "name": "Superwhale",
      "email": "superwhale@gmail.com",
      "phone": "18181818"
      }
    ];
    
    const filterNameObj = (array) => array.filter(item => {
      if((item.name === 'NameX') || (item.name === 'NameY')) return item
    })
    
    console.log(filterNameObj(testdata))

    【讨论】:

    • 这对我正在尝试做的事情有什么帮助?
    • 所要做的就是从搜索中删除其他对象。
    【解决方案2】:

    我发现了问题所在。 Semantic UI 的搜索模块需要某些键,所以我的等效“名称”必须是“标题”。我通过查看 JSON 并替换了每次出现的

    来修复它

    “名称”:

    “标题”:

    我尝试过使用

    fields: {
      title   : 'name'
    }
    

    但由于某种原因这不起作用。

    【讨论】:

      猜你喜欢
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-25
      • 2017-03-14
      • 2016-11-12
      相关资源
      最近更新 更多