简体   繁体   English

如何获取select2的选定文本并在sql语句中使用它

[英]how to get selected text of select2 and use it in a sql statement

I am trying to get the selected text of the select2 element and use it in a custom sql statement which will return json and i will render the json to morris bar chart...can anyone assist me with what i need to do? 我正在尝试获取select2元素的选定文本,并将其用于自定义的sql语句中,该语句将返回json,然后将json渲染为morris条形图...任何人都可以协助我完成我需要做的事情吗? below you will find the code i currently have. 在下面,您将找到我目前拥有的代码。 thanks in advance 提前致谢

DAL: DAL:

public DataTable GetBookingTrend()
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("select WEEKNO, YEARNO, SUM(NOOFWEEKS) from BOOKINGTREND where ( SCHOOLCOUNTRY in (select2.text)) group by weekno, yearno");
            DataTable dt;

            using (SqlDataAdapter da = new SqlDataAdapter(strBuilder.ToString(), connStr))
        {
            dt = new DataTable();
            da.Fill(dt);
        }
            return dt;
        }

SERVICE: 服务:

public List<BookingTrend> GetBookingTrendList()
    {
        List<BookingTrend> bookingtrends = new List<BookingTrend>();
        sqlDal dal = new sqlDal();

        foreach (DataRow item in dal.GetBookingTrend().Rows)
        {
            BookingTrend bookingtrend = new BookingTrend();
            bookingtrend.weekno = (int)item["WEEKNO"];
            bookingtrend.yearno = (int)item["YEARNO"];
            bookingtrends.Add(bookingtrend);
        }
        return bookingtrends;
    }

CONTROLLER: 控制器:

public ActionResult BookingTrendRead()
    {
        Services.sqlService ss = new Services.sqlService();

        return Json(ss.GetBookingTrendList(), JsonRequestBehavior.AllowGet);
    }

HTML: HTML:

<div class="panel panel-info">
    <div class="panel-heading">
        Booking Trend
    </div>
<div class="panel-body">
    <div id="booking-trend-bar" style="height: 250px"></div>
</div>

HTML: HTML:

<div class="form-group col-lg-3 col-sm-6">
     <select class ="form-control" id="SchoolDestinations" style="width:385px;" multiple>
          <option></option>
     </select>
</div> 

DOCUMENT READY: 准备文件:

$(document).ready(function () {

 function createChart(data) {
        var mychart = new Morris.Bar({
            element: 'booking-trend-bar',
            data: data,
            xkey: 'Country',
            ykeys: ['weekno','yearno'],
            labels: ['Week', 'Year']
        });
    }

    $.ajax({
        type: "GET",
        url: '@Url.Action("BookingTrendRead", "Main")',
        dataType: 'json',
        success: function (data) {
            var data = JSON.parse(data);

            createChart(data);
        },
        error: function () {
            alert("Error loading data! Please try again.");
        }
    });

$("#SchoolDestinations").select2({
        minimumInputLength: 0,
        placeholder: "School Destinations"
});

$.ajax({
    type: 'POST',
    url: '@Url.Action("SchoolRead", "Main")',
    dataType: 'json',
        success: function (data) {
            $.each(data, function (i, item) {
                $('<option value="' + item.Id + '">' + item.Text + '</option>').appendTo('#SchoolDestinations');
            });
        },
    error: function () {
        console.log('err')
    }
});
});

您需要从sql字符串中拆分选择的名称:

strBuilder.Append("select WEEKNO, YEARNO, SUM(NOOFWEEKS) from BOOKINGTREND where ( SCHOOLCOUNTRY in (" + select2.selectedText + ")) group by weekno, yearno");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM