select2根据传入的url参数动态添加option

(function ($) { 
    var FundArchivementsDSHome = function () { };
    //初始化
    FundArchivementsDSHome.load = function () {
        init();
        ResultDSChart();
    };
//根据url动态添加select2的option
    var init = function (callback) {
        let fundName = getUrlParam("fundName", true);
        let fundCode = getUrlParam("fundCode");
        if (fundCode !== "0" && fundCode !== null) {
            var option = new Option(fundName, fundCode, true, true);
            $('#FundListS').append(option);
        } 
    };
    var ResultDSChart = function (callback) {
        var data_url = CPRO.getVirtualPath() + "/FundArchivementsDS/GetResultDataByFund";
        var chart = echarts.init(document.getElementById('_ResultDSChart'));
        chart.showLoading({
            text: "加载中..."
        });
        var fundID = $("#FundListS option:selected").attr("value");
        var fundName = $("#FundListS  option:selected").text().replace(/[\r\n]/g, "").replace(/[ ]/g, "");
        if (fundName.length === 0) {
            fundID = "0";
        }
        $.ajax({
            url: data_url,
            type: 'get',
            data: "fundID=" + fundID,
            cache: false,
            dataType: 'json',
            async: true,
            success: function (result) {
                chart.hideLoading();
                if (result) {
                    var option = {
                        grid: {
                            bottom: 80
                        },
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {
                                type: 'cross',
                                animation: false,
                                label: {
                                    backgroundColor: '#505765'
                                }
                            },
                            extraCssText: 'width:300px;min-height:60px;'
                        },
                        dataZoom: [
                            {
                                show: true,
                                realtime: true,
                                start: 60,
                                end: 100
                            }
                        ],
                        xAxis: [
                            {
                                type: 'category',
                                data: result.xColloums,
                            }
                        ],
                        yAxis: [
                            {
                                name: '数据总量',
                                type: 'value'
                            }
                        ],
                        series: [result.projectmodel]
                    };
                    chart.clear();
                    chart.setOption(option);
                    chart.off('click');
                    
                }
            },
            error: function (errorMsg) {
                //alert("图表请求数据失败!" + errorMsg);
                console.log(errorMsg);
                chart.hideLoading();
            }
        });
    };
    $("#FundListS").change(function (data) {
        var fundID = $("#FundListS option:selected").attr("value");
        var fundName = $("#FundListS  option:selected").text().replace(/[\r\n]/g, "").replace(/[ ]/g, "");
        if (fundName.length === 0) {
            fundID = "0";
        }
        if (window.location.href.toLowerCase().indexOf("fundarchivementsds") !== -1) {
            ResultDSChart();
        }
    });
    var getUrlParam = function (name, decodeURI) {
        //console.log(name);
        //构造一个含有目标参数的正则表达式对象
        var reg = new RegExp("(^|&)" + name.toLowerCase() + "=([^&]*)(&|$)");
        //匹配目标参数
        var url = decodeURI ? decodeURIComponent(window.location.search.toLowerCase()) : window.location.search.toLowerCase();
        var r = url.substr(1).match(reg);
        //console.log(r);
        //返回参数值
        if (r !== null) return unescape(r[2]); return null;
    };
    $('#FundListS').select2({
        placeholder: '请输入',
        allowClear: true,
        ajax: {
            url: function (params) {
                return CPRO.getVirtualPath() + "/FundDataStatistics/GetFundList";
            },
            dataType: 'json',
            delay: 250,
            data: function (params) {
                var queryParameters = {
                    keywords: params.term
                };
                return queryParameters;
            },
            processResults: function (data) {
                return {
                    results: data.message
                };
            },
            cache: true
        }
    });
    window.CPRO.FundArchivementsDS = FundArchivementsDSHome;
})(jQuery);
发布了83 篇原创文章 · 获赞 38 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/mao_mao37/article/details/103777814