zoukankan      html  css  js  c++  java
  • MVC4 jQuery UI自动完成

    view中嵌入如下代码引入jquery-ui

    <script src="~/Scripts/jquery-1.6.2.min.js"
    ></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"
    ></script>
    <script src~/Scripts/jquery-ui.min.js"
    ></script>

    使用jquery-ui实现特效

    $(function () {
    $("#album-list img").mouseover(function () {
    $(this).effect("bounce");
    });
    });
    $(this).effect("bounce", { time: 3, distance: 40 });

    jquery实现

    $(function () {
    $("#album-list img").mouseover(function () {
    $(this).animate({ height: '+=25',  '+=25' })
    .animate({ height: '-=25',  '-=25' });
    });
    });

    Autocomplete with jQuery UI

    view中引入如下代码

    复制代码
    <link href="~/Content/Site.css) rel="stylesheet"
    type="text/css" />
    <link href="~/Content/themes/base/jquery-ui.css"
    rel="stylesheet"
    type="text/css" />
    <script src="~/Scripts/jquery-1.4.4.min.js"
    ></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"
    ></script>
    <script src~/Scripts/jquery-ui.min.js"
    ></script>
    复制代码

    Adding the Behavior input中之间获取结果的action

    <input type="text" name="q"
    data-autocomplete-source="@Url.Action("QuickSearch", "Home")" />

    JavaScript中实现自动完成

    $("input[data-autocomplete-source]").each(function () {
    var target = $(this);
    target.autocomplete({ source: target.attr("data-autocomplete-source") });
    });

    Building the Data Source构建数据源

    复制代码
    public ActionResult QuickSearch(string term)
    {
    var artists = GetArtists(term).Select(a => new {value = a.Name});
    return Json(artists, JsonRequestBehavior.AllowGet);
    }
    private List<Artist> GetArtists(string searchString)
    {
    return storeDB.Artists
    .Where(a => a.Name.Contains(searchString))
    .ToList();
    }
    复制代码

    JSON and Client-Side Templates

    <span class="detail">
    Rating: {{AverageReview}}
    Total Reviews: {{TotalReviews}}
    </span>

    Adding Templates引入JS

    通过nuget安装mustache.js

    复制代码
    <script src="~/Scripts/jquery-1.6.2.min.js"
    ></script>
    
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"
    ></script>
    <script src="~/Scripts/jquery-ui.min.js"
    ></script>
    <script src="~/Scripts/mustache.js"
    ></script>
    复制代码

    Modifying the Search Form

    复制代码
    @using (Ajax.BeginForm("ArtistSearch", "Home",
    new AjaxOptions {
    InsertionMode=InsertionMode.Replace,
    HttpMethod="GET",
    OnFailure="searchFailed",
    LoadingElementId="ajax-loader",
    UpdateTargetId="searchresults",
    }))
    {
    <input type="text" name="q"
    
    data-autocomplete-source="@Url.Action("QuickSearch", "Home")" />
    <input type="submit" value="search" />
    <img id="ajax-loader"
    src="~/Content/Images/ajax-loader.gif"
    style="display:none"/>
    }
    复制代码

    Index.cshtml

    复制代码
    <form id="artistSearch" method="get" action="@Url.Action("ArtistSearch", "Home")">
    <input type="text" name="q"
    data-autocomplete-source="@Url.Action("QuickSearch", "Home")" />
    <input type="submit" value="search" />
    <img id="ajax-loader" src="~/Content/Images/ajax-loader.gif"
    style="display:none"/>
    </form>
    复制代码

    js中实现提交

    复制代码
    $("#artistSearch").submit(function(event) {
    event.preventDefault();
    var form = $(this);
    $.getJSON(form.attr("action"), form.serialize(), function(data) {
    var html = Mustache.to_html($("#artistTemplate").html(), { artists: data });
    $("#searchresults").empty().append(html);
    });
    });
    复制代码
    Getting JSON获取json数据
    public ActionResult ArtistSearch(string q)
    {
    var artists = GetArtists(q);
    return Json(artists, JsonRequestBehavior.AllowGet);
    }
    Mustache模版定义
    复制代码
    <script id="artistTemplate" type="text/html">
    <ul>
    {{#artists}}
    <li>{{Name}}</li>
    {{/artists}}
    </ul>
    </script>
    <div id="searchresults">
    </div>
    复制代码

    jQuery.ajax for Maximum Flexibility灵活性

    复制代码
    $("#artistSearch").submit(function (event) {
    event.preventDefault();
    var form = $(this);
    $.ajax({
    url: form.attr("action"),
    data: form.serialize(),
    beforeSend: function () {
    $("#ajax-loader").show();
    },
    complete: function () {
    $("#ajax-loader").hide();
    },
    error: searchFailed,
    success: function (data) {
    var html = Mustache.to_html($("#artistTemplate").html(),
    { artists: data });
    $("#searchresults").empty().append(html);
    }
    });
    });
    复制代码

    Bundling and Minifi cation压缩js、css

    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
    "~/Scripts/jquery-1.*"));
    bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
    bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
    "~/Scripts/jquery-ui*"));

    视图中使用

    @Scripts.Render("~/bundles/jquery")
    @Styles.Render("~/Content/css")
  • 相关阅读:
    《一起学Windows phone7开发》系列视频课程
    Windows Phone 7 开发 31 日谈——第19日:推送通知
    周金根 2010年blog汇总:企业架构、敏捷个人、模型驱动
    Windows Phone 7 开发 31 日谈——第4日:设备方向
    Windows Phone 7 开发 31 日谈——第3日:返回键
    JavaScript初学者应注意的七个细节
    [项目过程中所遇到的各种问题记录]图表篇——有关MSChart的一些小技巧
    Windows Phone 7 开发 31 日谈——第2日:页面间导航
    Windows Phone 7 开发 31 日谈——第22日:应用?还是 游戏?
    Windows Phone 7 开发 31 日谈——第11日:加速感应器
  • 原文地址:https://www.cnblogs.com/bobo41/p/3023528.html
Copyright © 2011-2022 走看看