javascript - 我如何在jquery中获取预约时间(从时间,到时间)

标签 javascript jquery

我正在通过拖动时隙单元格来选择时隙。选择时间段后,我在文本框中输入患者姓名,然后单击选择按钮,然后患者姓名将转到所选时间段。用户可以为多个患者姓名选择多个时间段,然后单击分配按钮我必须将患者姓名和时间段(从时间到时间)插入数据库。

我在获取分配的时间段时遇到问题,即 jquery 中的起始时间和终止时间。

$("#btnAllot").click(function () {
    //how i get alloted time here.
    $('tr').each(function () {
        $(this).find('td').each(function () {
            if ($(this).hasClass('yell')) {
                alert($(this).closest('tr').find('td:eq(0)').text());

            };
        });
    });
}

see jsbin on dragging on time slot cell

最佳答案

好的,这是一种方法:

您将迭代第三个单元格具有 rowspan 属性的每一行。这表明新约会的开始。您可以通过检查同级(某种程度上)来获取开始时间,并通过获取距离 rowspan - 1 元素的行来获取结束时间。

可能有更好的方法,但这可能会给您一个开始。

例如:

var find_closest_hour = function($row) {
    var $cell = $row.children('td:first-child'),
        hour = "";
    // probably add something here
    while($cell.length && !(hour = $.trim($cell.text()))) {
        $cell = $cell.parent().prev().children('td:first-child');
    }
    return hour;
};

var $all_tds = $('#tableAppointment tr td:nth-child(3)'),
    $tds = $all_tds.filter('[rowspan]'); 

// will contain a list of objects [{patient: name, start: time, end: time},...]
var appointments = $tds.map(function() {
    var $this = $(this),
        $row = $this.parent(),
        $cells =  $row.children('td'),
        patient = $.trim($this.text()),
        start = find_closest_hour($row).split(':', 1) + ":" + $.trim($cells.eq(1).text()),
        $end_row, end;

    if(this.rowspan == 1) {
        end = start;
    }
    else {
        $end_row = $all_tds.eq($all_tds.index(this) + this.rowSpan - 1).parent();
        end = find_closest_hour($end_row).split(':', 1) + ":" + $.trim($end_row.children('td').eq(1).text());
    }

    return {patient: patient, start: start, end: end};
}).get();

DEMO

我会让你弄清楚如何正确格式化时间;)

注意:这在很大程度上取决于您当前的表结构,如果您更改它可能会损坏。为了使事情更加灵活,您可以将类添加到包含重要信息的单元格,例如将 hour 添加到包含一天中的小时的每个第一个单元格,并将 appointment_start 添加到约会的单元格。然后您可以通过这些搜索/过滤。

关于javascript - 我如何在jquery中获取预约时间(从时间,到时间),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11168382/

相关文章:

javascript - 在页面加载之前等待 $rootScope 值在 Angular 中解析

javascript - 带有背景图像的 jQuery 淡入

jquery - Backbone,当全部加载完毕时触发事件

jquery - 如何使用 Bootstrap 和 scrollToFixed jQuery 插件让选项卡 Pane 内容滚动?

javascript - 从标签和输入创建对象 - javascript、jquery

javascript - 客户端和服务器端编程有什么区别?

javascript - node.js 回调获取变量的意外值

javascript - 如果文本框中的值大于 1,则更改为复数

javascript - 如果我在 jquery mobile 中使用 "onclick",按钮不会改变颜色

javascript - 如何在 Vuex/Nuxt 中设置值