MVC Select2.js使用小结

问题参考:https://www.jb51.net/article/95561.htm

select2.js版本号:3.3.2,不是最新的版本,使用方法和官网上介绍的最新版本有点不太一样。

使用需要引入:select2.min.css和select2.min.js

js:

$(document).ready(function () {
    
    //select2.min.js
    function formatRepo(repo) {
        var markup = "<table class='movie-result'><tr>";
        
        markup += "<td valign='top'><img width='80px' src='data:" + repo.imgsrc+ "'/></td>";
        
        markup += "<td valign='top'><h5>" + repo.text + "</h5>";
        
        markup += "<div class='movie-synopsis'>" + repo.ISSN + "</div>";
        
        markup += "</td></tr></table>"
        return markup;
    }

    function formatRepoSelection(repo) {
        return repo.text;
    }

    $("#select_CNKI").select2({
        placeholder: "请输入期刊名进行知网数据库查询",
        minimumInputLength: 1,
        ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
            url: "/home/myGetCNKI",
            delay: 250,            
            dataType: 'json',
            data: function (term) {
                return {
                    search: term, // search term
                };
            },
            results: function (data) { // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to alter remote JSON data                
                return {
                    results: data
                };
            },
            cache:true
        },
       
        formatResult: formatRepo, // omitted for brevity, see the source of this page
        formatSelection: formatRepoSelection, // omitted for brevity, see the source of this page
        dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
        escapeMarkup: function (m) {
            return m;
        } // we do not want to escape markup since we are displaying html in results
    })
})

浏览器发出的Ajax访问需求格式为:/home/mygetCNKI/?search=<parameters>,可以添加任意多个,改data:选项就是。

HTML:

老版本的只能用在<input>标签里面,新版本的要用在<select>里面

<input id="select_CNKI" class="span12 select2"> 

根据css,网页会显示为:

Controller:

 '获取数据库中知网的期刊数据
    Class CNKIBookInfo
        Public Property id As Integer
        Public Property text As String
        Public Property ISSN As String
        Public Property imgsrc As String
    End Class
    Function myGetCNKI(ByVal search As String) As JsonResult
        Dim CNKIsearchStr As String = search.Replace(" ", "")
        Dim mybookList As List(Of CNKIBookInfo) = New List(Of CNKIBookInfo)
        If CNKIsearchStr = String.Empty Then
            Return Json(mybookList, JsonRequestBehavior.AllowGet)
            Exit Function
        End If

        Dim CNKIlist = AllinoneDb.Allinone_ISSNInfo.Where(Function(s) s.ISSN_Name.Contains(CNKIsearchStr)).Take(20).ToList
        If IsNothing(CNKIlist) Then
            Return Json(mybookList, JsonRequestBehavior.AllowGet)
            Exit Function
        End If

        For Each item In CNKIlist
            Dim newBook As CNKIBookInfo = New CNKIBookInfo
            newBook.id = item.ISSN_ID
            newBook.text = item.ISSN_Name
            newBook.ISSN = item.ISSN_Code
            newBook.imgsrc = item.ISSN_Cover_Img
            mybookList.Add(newBook)
        Next
        Return Json(mybookList, JsonRequestBehavior.AllowGet)
    End Function

要特别注意:VB语言参数是忽略大小写的,Javascript读data的时候是大小写敏感的。稍微不注意,就会不能正确显示

最终的结果会是这样:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在ASP.NET MVC使用Vue.js,您需要按照以下步骤进行配置: 1. 在您的ASP.NET MVC项目中安装Vue.js。您可以使用npm或CDN来安装Vue.js。 2. 创建一个Vue.js组件,并将其添加到您的ASP.NET MVC视图中。 3. 在您的ASP.NET MVC视图中添加Vue.js依赖项,并将其链接到您的Vue.js组件。 4. 使用Vue.js指令和模板语法编写HTML标记。 以下是一个简单的例子,演示如何在ASP.NET MVC使用Vue.js: 1. 安装Vue.js 您可以通过npm安装Vue.js,打开命令提示符,进入您的ASP.NET MVC项目文件夹,并运行以下命令: ``` npm install vue ``` 或者您可以使用CDN,将以下代码添加到您的ASP.NET MVC视图中: ``` <script src="https://cdn.jsdelivr.net/npm/vue"></script> ``` 2. 创建Vue.js组件 创建一个Vue.js组件,并将其保存到您的ASP.NET MVC项目中。例如,创建一个名为"MyComponent.vue"的组件。 ``` <template> <div> <h1>{{ message }}</h1> </div> </template> <script> export default { data() { return { message: 'Hello Vue!' } } } </script> ``` 3. 添加Vue.js依赖项 在您的ASP.NET MVC视图中添加Vue.js依赖项,并将其链接到您的Vue.js组件。 ``` <head> <script src="https://cdn.jsdelivr.net/npm/vue"></script> <script src="~/Scripts/MyComponent.vue"></script> </head> ``` 4. 使用Vue.js指令和模板语法编写HTML标记 在您的ASP.NET MVC视图中使用Vue.js指令和模板语法编写HTML标记。 ``` <div id="app"> <my-component></my-component> </div> <script> new Vue({ el: '#app', components: { 'my-component': MyComponent } }) </script> ``` 这样,您就可以在ASP.NET MVC使用Vue.js了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值