【问题标题】:Sweetalert 2 - sorting Options for Select inputSweetalert 2 - 选择输入的排序选项
【发布时间】:2020-06-12 12:46:57
【问题描述】:

我有以下代码:



this.getdata((params.....).then((data) => {

    var selectOptions = [];
    for (let i = 0; i < data.length; i++) {
        selectOptions[data[i].id_calc] = data[i].surname + " " + data[i].name;
    }
    console.log(selectOptions )
    Swal.fire({
        position: 'top',
        icon: 'warning',
        title: 'Attenzione',
        text: 'Select something',
        input: 'select',
        inputPlaceholder: 'Seleziona',
        inputOptions: selectOptions,
        showCancelButton: true,
        confirmButtonText: 'insert',
        showLoaderOnConfirm: true,
        cancelButtonColor: '#d33',
        cancelButtonText: 'cancel',
        inputValidator: (value) => {
            return new Promise((resolve) => {
                if (value > 0 && value != "" && value != undefined && value != null) {
                    resolve()
                } else {
                    resolve('Select something')
                }
            })
        },
        preConfirm: (value) => {
            callAPI(value) 
        },
        allowOutsideClick: () => !Swal.isLoading()
    }).then((result) => {
        if (result.value) {
            console.log(result.value)
            Swal.fire({
                icon: 'success',
                title: 'OK',
                text: 'ok!'
            })
        }
    })
})

所以现在我有这样的东西:

selectOptions[1535] : bob,
selectOptions[1536]: lucas,
selectOptions[3445]: laura,
selectOptions[6443]: andrew,
ecc

并且选择输入按selectOptions id排序, 但我需要按属性“surename”对选择选项进行排序。

{
  6443: andrew,
  1535 : bob,
  3445: laura
  1536: lucas,
},


我怎样才能做到这一点? 我需要属性“id_calc”在 preConfirm 部分中使用所选值调用函数(如果我选择 lucas,我​​需要使用 1536 值调用函数)

我的代码有效,但选项没有按surename排序,有什么帮助吗?

附言对不起我的英语!!

编辑:收到的数据已经排序,但是当我创建选择选项时,变成按 id 排序

selectOptions 就像

{
  225: "NOOK TOM ",
  467: "BITE ABEL ",
  479: "DOG MAX ", 
  1234: "ALAN DENIS",
...
}

我需要

{
  1234: "ALAN DENIS",
  467: "BITE ABEL ",
  479: "DOG MAX ",
  225: "NOOK TOM ",
...
}

【问题讨论】:

    标签: javascript reactjs sorting select sweetalert2


    【解决方案1】:

    在构建selectOptions 数组之前,对收到的数据进行排序

    this.getdata((params.....).then((data) => {
    
        // sort incoming collection
        var sortedData = data.sort((a, b) => a.surname > b.surname);
    
        // build selectOptions with the sorted data
        var selectOptions = [];
        for (let i = 0; i < sortedData.length; i++) {
            selectOptions[sortedData[i].id_calc] = sortedData[i].surname + " " + sortedData[i].name;
        }
    
        // rest of the code unchanged
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-08
      • 2018-06-17
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 2021-08-22
      相关资源
      最近更新 更多