jQWidgets jqxScheduler cellClick事件

  • Post category:jquery

jQWidgets jqxScheduler是一个功能强大的日程表控件,可以用来展示和管理各种时间段的活动。其中,cellClick事件是当用户单击了日程表中的任意单元格时触发的事件。在本篇文章中,我们将详细讲解如何使用jQWidgets jqxScheduler控件的cellClick事件。

1. 安装jqwidgets

首先,我们需要在我们的项目中安装jqwidgets库,可以通过npm包管理工具进行安装:

npm install jqwidgets-scripts --save

2. 引入相关文件

在使用jQWidgets jqxScheduler控件之前,我们需要先在HTML文件中引入相关的js和css文件。具体如下:

<!-- 引入jqwidgets.css文件 -->
<link rel="stylesheet" href="./jqwidgets/styles/jqx.base.css" type="text/css" />

<!-- 引入jqwidgets js文件 -->
<script type="text/javascript" src="./jqwidgets/jqxcore.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxbuttons.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxscrollbar.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxmenu.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxgrid.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxgrid.filter.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxgrid.sort.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxgrid.pager.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxgrid.selection.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxgrid.edit.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxgrid.columnsresize.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxdata.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxdatetimeinput.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxcalendar.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxtooltip.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxscheduler.js"></script>
<script type="text/javascript" src="./jqwidgets/jqxscheduler.api.js"></script>

3. 创建日程表

接下来,我们创建一个简单的日程表,并绑定cellClick事件。具体如下:

<!-- HTML代码 -->
<div id="scheduler"></div>

<!-- JS代码 -->
// 初始化日程表
var scheduler = $("#scheduler").jqxScheduler({
    width: "100%",
    height: 600,
    date: new Date(),
    view: "weekView",
    appointmentHeight: 40,
    resources:
    {
        colorScheme: "scheme05",
        dataField: "calendar",
        source: new $.jqx.dataAdapter({
            dataFields: [
                { name: "id", type: "string" },
                { name: "name", type: "string" },
                { name: "color", type: "string" }
            ],
            dataType: "array",
            localData: [
                { id: "calendar1", name: "Calendar 1", color: "#f2cb1d" },
                { id: "calendar2", name: "Calendar 2", color: "#03a9f4" },
                { id: "calendar3", name: "Calendar 3", color: "#4caf50" }
            ]
        })
    },
    views:
    [
        "dayView",
        "weekView",
        "monthView"
    ],
    appointments:
    [
        {
            description: "Meeting with John",
            location: "Room 101",
            subject: "Plan new project",
            calendar: "calendar1",
            from: new Date(2021, 11, 1, 10, 0, 0),
            to: new Date(2021, 11, 121, 11, 0, 0),
        },
        {
            description: "Meeting with Susan",
            location: "Room 102",
            subject: "Review current status",
            calendar: "calendar2",
            from: new Date(2021, 11, 7, 13, 0, 0),
            to: new Date(2021, 11, 7, 14, 0, 0),
        },
        {
            description: "Team meeting",
            location: "Room 103",
            subject: "Weekly sync-up",
            calendar: "calendar3",
            from: new Date(2021, 11, 10, 15, 0, 0),
            to: new Date(2021, 11, 10, 16, 0, 0),
        }
    ]
});

// 绑定cellClick事件
scheduler.on("cellClick", function (event) {
    console.log(event.args.date);
});

上述代码中,我们使用jqxScheduler创建一个日程表,并将其绑定到页面上。然后我们通过on方法绑定了cellClick事件,并在事件处理器中打印出了事件的日期。

4. 示例说明

示例一

在这个示例中,我们将展示如何通过cellClick事件获取用户点击的单元格的信息。

<div id="scheduler"></div>

<script type="text/javascript">
    // 初始化日程表
    var scheduler = $("#scheduler").jqxScheduler({
        width: "100%",
        height: 600,
        date: new Date(),
        view: "weekView",
        appointmentHeight: 40,
        resources:
        {
            colorScheme: "scheme05",
            dataField: "calendar",
            source: new $.jqx.dataAdapter({
                dataFields: [
                    { name: "id", type: "string" },
                    { name: "name", type: "string" },
                    { name: "color", type: "string" }
                ],
                dataType: "array",
                localData: [
                    { id: "calendar1", name: "Calendar 1", color: "#f2cb1d" },
                    { id: "calendar2", name: "Calendar 2", color: "#03a9f4" },
                    { id: "calendar3", name: "Calendar 3", color: "#4caf50" }
                ]
            })
        },
        views:
        [
            "dayView",
            "weekView",
            "monthView"
        ],
        appointments:
        [
            {
                description: "Meeting with John",
                location: "Room 101",
                subject: "Plan new project",
                calendar: "calendar1",
                from: new Date(2021, 11, 1, 10, 0, 0),
                to: new Date(2021, 11, 121, 11, 0, 0),
            },
            {
                description: "Meeting with Susan",
                location: "Room 102",
                subject: "Review current status",
                calendar: "calendar2",
                from: new Date(2021, 11, 7, 13, 0, 0),
                to: new Date(2021, 11, 7, 14, 0, 0),
            },
            {
                description: "Team meeting",
                location: "Room 103",
                subject: "Weekly sync-up",
                calendar: "calendar3",
                from: new Date(2021, 11, 10, 15, 0, 0),
                to: new Date(2021, 11, 10, 16, 0, 0),
            }
        ]
    });

    // 绑定cellClick事件
    scheduler.on("cellClick", function (event) {
        console.log(event.args.date + " clicked.");
        console.log(event.args.resource.calendar + " cell clicked.");
    });
</script>

上述代码中,我们在cellClick事件的处理器中,分别使用console.log打印了事件引发的日期和点击的日程表单元格所属的资源(calendar1、calendar2或calendar3)。

示例二

在这个示例中,我们将展示如何通过cellClick事件选中事件,并将刚选择的事件设为当前事件。

<div id="scheduler"></div>

<script type="text/javascript">
    // 初始化日程表
    var scheduler = $("#scheduler").jqxScheduler({
        width: "100%",
        height: 600,
        date: new Date(),
        view: "weekView",
        appointmentHeight: 40,
        resources:
        {
            colorScheme: "scheme05",
            dataField: "calendar",
            source: new $.jqx.dataAdapter({
                dataFields: [
                    { name: "id", type: "string" },
                    { name: "name", type: "string" },
                    { name: "color", type: "string" }
                ],
                dataType: "array",
                localData: [
                    { id: "calendar1", name: "Calendar 1", color: "#f2cb1d" },
                    { id: "calendar2", name: "Calendar 2", color: "#03a9f4" },
                    { id: "calendar3", name: "Calendar 3", color: "#4caf50" }
                ]
            })
        },
        views:
        [
            "dayView",
            "weekView",
            "monthView"
        ],
        appointments:
        [
            {
                description: "Meeting with John",
                location: "Room 101",
                subject: "Plan new project",
                calendar: "calendar1",
                from: new Date(2021, 11, 1, 10, 0, 0),
                to: new Date(2021, 11, 121, 11, 0, 0),
            },
            {
                description: "Meeting with Susan",
                location: "Room 102",
                subject: "Review current status",
                calendar: "calendar2",
                from: new Date(2021, 11, 7, 13, 0, 0),
                to: new Date(2021, 11, 7, 14, 0, 0),
            },
            {
                description: "Team meeting",
                location: "Room 103",
                subject: "Weekly sync-up",
                calendar: "calendar3",
                from: new Date(2021, 11, 10, 15, 0, 0),
                to: new Date(2021, 11, 10, 16, 0, 0),
            }
        ]
    });

    // 绑定cellClick事件
    scheduler.on("cellClick", function (event) {
        var date = event.args.date;
        var resource = event.args.resource.calendar;
        scheduler.clearSelection();
        scheduler.selectCell(date, resource);
        var appointment = scheduler.getSelection()[0];
        scheduler.scrollTo(appointment.from);
        scheduler.ensureVisible(appointment);
    });
</script>

上述代码中,我们在cellClick事件的处理器中,将选中的事件(即鼠标单击的事件)设为当前事件,并将其滚动到可见区域。