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

本文关键字:语句 SQL select2 何获得 文本 | 更新日期: 2023-09-27 18:09:39

我正试图获得select2元素的选定文本,并在自定义sql语句中使用它,该语句将返回json,我会将json呈现给莫里斯条形图…有人能帮我做我需要做的事吗?下面你会发现我目前的代码。提前感谢

木豆:

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;
        }
服务:

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;
    }

控制器:

public ActionResult BookingTrendRead()
    {
        Services.sqlService ss = new Services.sqlService();
        return Json(ss.GetBookingTrendList(), JsonRequestBehavior.AllowGet);
    }
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:

<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(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')
    }
});
});

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

您需要从SQL字符串中分离出select的名称:

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