使用Asp.net Mvc3 Razor视图方式扩展JQuery UI Widgets方法介绍

使用Asp.net Mvc3 Razor视图方式扩展JQuery UI Widgets方法介绍

简介

在Asp.net Mvc3应用中,使用JQuery UI Widgets可以非常方便地增强页面功能,而我们可以借助Razor视图方式对JQuery UI Widgets进行扩展,以满足更多需求。

方法介绍

步骤1:创建MVC项目

首先,我们需要创建一个MVC3项目,选择Razor视图语法。

步骤2:添加JQuery UI

在项目中添加JQuery UI文件(.css和.js),可以直接引用CDN地址。

步骤3:创建扩展函数

在项目中新建一个JavaScript文件,并定义一个扩展函数,例如我们要扩展一个对话框的关闭按钮:

$.widget("ui.dialog", $.ui.dialog, {
    options: {
        closeText: "关闭"
    },
    _create: function () {
        this._super();
        var buttons = this.uiDialogButtonPane.find("button");
        buttons.each(function () {
            var closeButton = $("<a>")
                .prop("href", "#")
                .addClass("ui-dialog-titlebar-close ui-corner-all")
                .click(function (event) {
                    event.preventDefault();
                    $(this).closest(".ui-dialog-content").dialog("close");
                });
            $("<span>")
                .addClass("ui-icon ui-icon-closethick")
                .appendTo(closeButton);
            $(this).button("option", "icons", { primary: "ui-icon-minusthick" })
                .prepend(closeButton);
        });
    }
});

上述代码中,我们扩展了对话框的关闭按钮,并添加了关闭图标。

步骤4:调用扩展函数

在页面加载时,调用扩展函数:

<script>
    $(function() {
        $("#dialog").dialog();
    });
</script>
<div id="dialog" title="Dialog Title">Test Dialog</div>

创建一个对话框,调用扩展函数。

示例说明

示例1:JQuery UI日历控件

我们可以通过扩展JQuery UI日历控件,在弹出框中显示日历。首先,我们需要在JavaScript文件中创建一个扩展函数,以增加日历显示功能:

$.widget("ui.datepicker", $.ui.datepicker, {
    options: {
        showOn: 'button',
        buttonText: '日历选择',
        buttonImageOnly: false,
        buttonImage: ''
    },
    _create: function() {
        this._super();
        var self = this;
        this.dpDiv.after($('<button>eXtend DatePicker</button>').click(function() {
            $(self._input)
                .datepicker('hide')
                .blur();
            $('<div id="win"></div>')
                .html(self.dpDiv)
                .dialog({
                    title: '日历',
                    modal: true
                });
        }));
    }
});

上述代码中,我们扩展了日历控件,增加了一个“eXtend DatePicker”的按钮,点击该按钮,可以引用JQuery的对话框组件,并将日历控件显示在弹出框中。在页面中,调用日历控件:

@{
    ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-3.3.1.js"></script>
<script src="~/Scripts/jquery-ui-1.12.1.js"></script>
<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="~/Scripts/extendUI.js"></script>
<h2>Extending jQuery UI datepicker</h2>
<input type="text" id="datepicker" />
<script>
    $(function () {
        $("#datepicker").datepicker();
    });
</script>

示例2:表格排序

我们也可以扩展JQuery UI的表格排序功能。首先,我们需要在JavaScript文件中创建一个函数:

$.widget("ui.sortable", $.ui.sortable, {
    _mouseStart: function(event, overrideHandle, noActivation) {
        var i, body,
            o = this.options;
        this.currentContainer = this;

        //Create and append the visible helper
        this.helper = this._createHelper(event);

        //Cache the helper size
        this._cacheHelperProportions();

        //If ddmanager is used for droppables droppable hover class (ui-state-hover) shouldn't be added
        //    since this helper is never moved out of the current container
        if($.ui.ddmanager) {
            $.ui.ddmanager.current = this;
        }

        //Prepare variables for position generation
        this._cacheMargins();

        //The element's absolute position on the page minus margins
        this.cssPosition = this.helper.css("position");
        this.scrollParent = this.helper.scrollParent(true);
        this.offset = this.currentItem.offset();
        this.offset = {
            top: this.offset.top - this.margins.top,
            left: this.offset.left - this.margins.left
        };

        //Store helper option to later restore after canceling mousecapture
        this.originalPosition = this._generatePosition(event);
        this.originalPageX = event.pageX;
        this.originalPageY = event.pageY;

        //Adjust the mouse offset relative to the helper if 'cursorAt' is supplied
        if(o.cursorAt) {
            this._adjustOffsetFromHelper(o.cursorAt);
        }

        //Cache the former DOM position
        this.domPosition = { prev: this.currentItem.prev()[0], parent: this.currentItem.parent()[0] };

        //If the helper is not the original, hide the original so it's not playing any role during the drag, won't cause anything bad this way
        if(this.helper[0] !== this.currentItem[0]) {
            this.currentItem.hide();
        }

        //Create the placeholder
        this._createPlaceholder();

        //Only after we got the placeholder, force creation of the helper in case we need to
        // adjust the placeholder offset to it
        if(o.helper === "original" && !overrideHandle) {
            this._setPositionRelativeToSegment(this._generatePosition(event, true));
        }

        if(!noActivation) {
            for(i = this.containers.length - 1; i >= 0; i--){
                this.containers[i]._trigger("activate", event, this);
            }
        }

        //Prepare possible droppables
        if($.ui.ddmanager) {
            $.ui.ddmanager.dragStart(this, event);
        }

        this.helper.addClass("ui-sortable-helper");
        this._mouseDrag(event); //Execute the drag once - this causes the helper not to be visible before getting its correct position
        return true;
    },
});

上述代码中,我们扩展了表格排序控件,以满足更多的需求。

为了使用该扩展函数,在页面中需要调用表格排序控件:

@{
    ViewBag.Title = "Sortable table";
}
<script src="~/Scripts/jquery-3.3.1.js"></script>
<script src="~/Scripts/jquery-ui-1.12.1.js"></script>
<script src="~/Scripts/extendUI.js"></script>
<h2>Sortable table</h2>
<table id="table">
    <thead>
        <tr>
            <th>Name</th>
            <th>Type</th>
            <th>Date</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Test 1</td>
            <td>Test 1 type</td>
            <td>2018-01-01</td>
            <td>$100.00</td>
        </tr>
        <tr>
            <td>Test 2</td>
            <td>Test 2 type</td>
            <td>2018-02-02</td>
            <td>$200.00</td>
        </tr>
        <tr>
            <td>Test 3</td>
            <td>Test 3 type</td>
            <td>2018-03-03</td>
            <td>$300.00</td>
        </tr>
        <tr>
            <td>Test 4</td>
            <td>Test 4 type</td>
            <td>2018-04-04</td>
            <td>$400.00</td>
        </tr>
    </tbody>
</table>
<script>
    $(function () {
        $("#table tbody").sortable({
            helper: function (event, ui) {
                ui.children().each(function () {
                    $(this).width($(this).width());
                });
                return ui;
            },
            stop: function (event, ui) {
                ui.item.children("td").each(function () {
                    $(this).width($(this).width());
                });
            }
        });
    });
</script>

上述代码中,我们使用了自定义的helper函数和stop函数,以满足我们的需求。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:使用Asp.net Mvc3 Razor视图方式扩展JQuery UI Widgets方法介绍 - Python技术站

(0)
上一篇 2023年5月27日
下一篇 2023年5月27日

相关文章

  • django实现将后台model对象转换成json对象并传递给前端jquery

    将后台model对象转换成json对象并传递给前端jquery是一个常见的需求,实现的过程可以按照以下步骤进行: 1.在后台编写视图函数 在Django中,我们可以编写视图函数来处理请求,将后台数据转换为json对象并传递给前端。具体实现方式如下: from django.http import JsonResponse from app.models im…

    jquery 2023年5月27日
    00
  • jQuery Misc each()方法

    jQuery Misc each() 方法是 jQuery 提供的一个遍历方法,主要用于遍历 jQuery 对象中的每个元素并应用回调函数。本文将详细讲解该方法的用法及注意事项。 语法 jQuery Misc each() 语法: $(selector).each(function(index, element)) 参数说明: selector : 必选参数…

    jquery 2023年5月12日
    00
  • jQWidgets jqxButtonGroup enableHover属性

    jQWidgets 的 jqxButtonGroup 组件提供了 enableHover 属性,用于启用或禁用按钮组中的鼠标悬停效果。本文将详细介绍 enableHover 属性的使用方法,包括概述、示例以及注意项。 enableHover 属性概述 enableHover 属性用于启用或禁用按钮组中的鼠标悬停效果。当该属性设置为 true 时,鼠标悬停在按…

    jquery 2023年5月11日
    00
  • jQuery UI Datepicker firstDay选项

    以下是关于 jQuery UI 的 Datepicker firstDay 选项的完整攻略: jQuery UI 的 Datepicker firstDay 选项 在 jQuery UI 中,可以使用 datepicker 方法创建一个日期选择器。firstDay 选项可以指定一周的第一天是哪一天。 语法 $(selector).datepicker({ f…

    jquery 2023年5月11日
    00
  • 统计jQuery中各字符串出现次数的工具

    下面是关于统计 jQuery 中各字符串出现次数的完整攻略。 1. 确定需求 在开始编写工具之前,我们需要先明确我们的需求和目标。本次攻略的目标是开发一个工具,可以统计 jQuery 代码中各个字符串出现的次数。 2. 获取jQuery代码 首先我们需要得到 jQuery 的代码。可以通过 jQuery 的官网下载 jQuery,或者通过 CDN 引入 jQ…

    jquery 2023年5月28日
    00
  • jquery获取并修改触发事件的DOM元素示例【基于target 属性】

    当使用jQuery绑定事件时,可以在事件函数中使用event参数,这个参数提供了信息关于事件发生的上下文,包括指定事件元素的信息。其中,event.target属性提供了被触发事件的元素的DOM对象。利用这个属性,我们可以获取和修改触发事件的DOM元素。 改变DOM元素的背景颜色的示例: $("#myButton").click(func…

    jquery 2023年5月28日
    00
  • 如何使用jQuery创建一个HTML元素

    当使用jQuery创建HTML元素时,我们可以使用以下步骤: 获取要添加元素的父元素,例如body元素。 使用jQuery函数创建新的HTML元素,例如$(“<div></div>”)。 使用.attr()函数设置元素的属性,例如div.attr(“id”, “myDiv”)。 使用.css()函数设置元素的样式,例如div.css(…

    jquery 2023年5月9日
    00
  • jQWidgets jqxDataTable的渲染属性

    以下是关于“jQWidgets jqxDataTable的渲染属性”的完整攻略,包含两个示例说明: 简介 jqxDataTable 控件的渲染属性用于自定义控件的外观和行为。通过设置不同的渲染属性,可以实现不同的效果。 整攻 以下是 jqxDataTable 控件渲染属性的完整攻略: 定义渲染属性 在 jqxDataTable 控件中,可以使用不同的渲染属性…

    jquery 2023年5月11日
    00
合作推广
合作推广
分享本页
返回顶部