【问题标题】:How to sort data in JQueryJQuery中如何对数据进行排序
【发布时间】:2020-03-03 14:17:01
【问题描述】:

我正在制作一个显示来自后端的数据的滑块。使用“推送”功能,它会根据日期在滑块中显示幻灯片。但我需要它根据日期和状态显示这些幻灯片。

先显示未完成状态(btn-danger),后显示待处理状态(btn-success),然后显示完成状态(btn-warning)。

代码截图https://ibb.co/5KJpdrh

完整代码:

paste.ofcode.org/hmKSwTvaWrjTj3A6rWh464

代码:

  function handleSubmit(command) {
    if(command === 'meeting') {

        let meetingFormValue = getMeetingFormValue('.create-meeting-form');
        httpService.post('http://localhost:3000/meeting/create', meetingFormValue)
            .then(response => {
                meetings.push(response);
                setMeetingCarausel();
            }).catch(ex => {
                console.log('Error');
                console.log(ex);
            }) 
        // close the form
        $('.create-meeting-form').stop().slideToggle();
    }else if(command === 'task') {
        //attendees
        const taskFormValue = getTaskFormValue('#createTaskForm');
        httpService.post('http://localhost:3000/meeting/taskList/create', taskFormValue)
            .then(response => {
                tasks.push(response);
                setTasksCarausel();
            }).catch(ex => {
                console.log(ex);
            });

        // close the form
        $('.create-task-form').stop().slideToggle();
    }
}

【问题讨论】:

  • 数据在哪里?
  • @ksav 数据来自数据库并显示在滑块 ibb.co/5KJpdrh 的幻灯片中,它包含主题、日期、状态
  • 将数据添加到您的问题中。
  • 我添加了视图截图和完整代码
  • 没有数据。您不想对数据进行排序吗?

标签: javascript jquery ejs


【解决方案1】:

你想使用Array.prototype.sort()

您应该传递一个比较函数,让sort() 使用您的标准对您的条目进行正确排序。

【讨论】:

  • 既然我已经为您指明了正确的方向,请尝试先阅读文档并学习如何自己做。
  • 我已经通过看到它编写了这个函数但是它不起作用,,,,,,,,,,,,,,,,,,,tasks.push(response); tasks.sort(function(a, b) { if(a.status !== b.status) { return a.status > b.status ? 1 :-1; } return a.date > b.date ? 1 : -1; });
  • tasks.push(response); tasks.sort(function(a, b) { if(a.status !== b.status) { return a.status > b.status ? 1 :-1; } return a.date > b.date ? 1 : -1; });
  • 嗨,这个函数正在工作 tasks.sort(function(a, b) { if(a.status !== b.status) { return a.status > b.status ? 1 :-1 ; } return a.date > b.date ? 1 : -1; });
  • 它好多了,,,,, 结果是ibb.co/JF2yh3t 但我需要btn-danger 1st, 然后btn-success, 然后btn-warning,,, 只是为了替换btn-的位置警告和 btn 危险
猜你喜欢
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 1970-01-01
相关资源
最近更新 更多