ajax 调用及前台拼循环拼写html

6 篇文章 0 订阅

ajax 调用及前台拼循环拼写html

 

asp代码

 

$(function () {
   $("#btnShearch").click(function () {

 

       $.ajax({
           type: "Post",
           url: "SearchSentence.aspx/getSearchList",
           data: "{'Sentence': '" + Sentence + "'," + "'chk_SynonymsUsed': '" + chk_SynonymsUsed + "','chk_UnnecessaryWordsUsed':'" + chk_UnnecessaryWordsUsed + "','MaximumRows':'" + MaximumRows + "'}",
           contentType: "application/json;charset=utf-8",
           dataType: "json",
           success: function (data) {
               var rsStr = data.d;

 

               // チェックエラーの場合
               if (rsStr.substr(0, 4) == ("エラー:")) {
                   var errArr = rsStr.split(":");
                   showMessageDialog(errArr[1], errArr[0]);
                   document.getElementById("content_contentFooter_footer_message").innerText = "XXXX..."
                   return;
               }

 

               data = jQuery.parseJSON(rsStr);
               var itemRow = "<table style=\"display: block; overflow-x: auto; overflow-y: auto; overflow: auto \" >";

 

               itemRow += "<tbody>";

 

               $.each(data, function (i, item) {
                   itemRow += "<tr>";
                   for (var k in item) {
                       itemRow += "<td class=\"ell2\"  οnclick=cellClick(this)>" + (null == item[k] ? "" : item[k]) + "</td>";
                   }
                   itemRow += "</tr>";
               })

 

               itemRow += "</tbody>";

 

               itemRow += "</table>";
               $("#divItems").html(itemRow);
               SetTbBgcolor();

 

               document.getElementById("content_contentFooter_footer_message").innerText = "検索完了しました."
               //
               document.getElementById("divItemsF").style.display = 'block'
               document.getElementById("btnShearch").disabled = false
               document.getElementById("btnShearch").focus();
           },
           error: function (err) {
               alert("バックエンド処理が失敗しました。エラーは" + err.responseText + "です。");
           }
       });
   });
});
...
<div id="divItemsF" style="overflow-x:auto; overflow-y:auto">
    <table id="divItems" style="table-layout:fixed; min-width: 100%; height: 100%" ></table>
</div>
###############################################################
cs代码
[WebMethod]
 public static string getSearchList(string Sentence, bool chk_SynonymsUsed, bool chk_UnnecessaryWordsUsed, string MaximumRows)
{
   
    DataTable searchSentenceDataTable = null;

 

    try
    {
        // 数据取得
        searchSentenceDataTable = dao.getSearchSentenceList(Sentence);

 

    return DataTableToJSON(searchSentenceDataTable);
}

 

// DataTable转换成JSON格式
 [WebMethod]
public static string DataTableToJSON(DataTable table)
{
    var list = new List<Dictionary<string, object>>();

 

    foreach (DataRow row in table.Rows)
    {
        var dict = new Dictionary<string, object>();

 

        foreach (DataColumn col in table.Columns)
        {
            dict[col.ColumnName] = row[col];
        }
        list.Add(dict);
    }

 

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string str = serializer.Serialize(list);
    return str;
}


/// Array数组转换成DataTable
public static DataTable ArrayToDataTable(string[,] arr)
{
    DataTable dataSouce = new DataTable();
    for (int i = 0; i < arr.GetLength(1); i++)
    {
        DataColumn newColumn = new DataColumn(i.ToString(), arr[0, 0].GetType());
        dataSouce.Columns.Add(newColumn);
    }
    for (int i = 0; i < arr.GetLength(0); i++)
    {
        DataRow newRow = dataSouce.NewRow();
        for (int j = 0; j < arr.GetLength(1); j++)
        {
            newRow[j.ToString()] = arr[i, j];
        }
        dataSouce.Rows.Add(newRow);
    }
    return dataSouce;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值