2014-04-02 24 views
0

這是我的觀點。我在這裏發佈了我的視圖,控制器和jsonHelper.cs供您參考。請通過這個。並附上我的問題的屏幕截圖。如何在jQuery網格中創建下拉列表。

先生,我已經在上面粘貼了我所有的代碼。我想在下拉列表中選擇'列名'。我希望你可以幫助我。??

//SchemaCreation View 
    <script type="text/javascript"> 
      var TableWidth = null; 
      var TableHeight = null; 
      $(document).ready(function() 
      { 
       //debugger; 
       TableWidth = $("#list").width(); 
       TableHeight = $("#Div1").height(); 
      }); 
      jQuery(document).ready(function() 
      { 
       //debugger; 
       jQuery("#list").jqGrid({ 
        url: '/Schema/GetGridData?DataSource=' + '@ViewData["ServerName"]' + '&InitialCatalog=' + '@ViewData["DataBaseName"]' + '&UserName=' + '@ViewData["UserName"]' + '&PassWord=' + '@ViewData["PassWord"]', 
        datatype: 'json', 
        mtype: 'GET', 
        colNames: ['Table Names', 'Column Names'], 
        colModel: [ 
           { name: 'Table Names', index: 'Table Names', width: 40, align: 'left', Style:'padding-left:15px;'}, 
           { name: 'Column Names', index: 'Column Names', width: 40, align: 'left'},//, width: 80, formatter: 'select', type:'select', edittype: 'select', editoptions: { value: "1:One;2:Two" } } 
           ], 
        pager: jQuery('#pager'), 
        rowNum: 10, 
        rowList: [5, 10, 20, 50], 
        sortname: 'Table Names', 
        sortorder: "desc", 
        multiselect: true, 
        viewrecords: true, 
        width: TableWidth, 
        Height: 500, 
        scroll: true, 
        //caption: 'My first grid' 
       }); 
      }); 
      </script> 



    //SchemaController:- 

    public ActionResult GetGridData(string sidx, string sord, int page, int rows, string DataSource, string InitialCatalog, string UserName, string PassWord) 
      { 
       DataSource = CrossCutting.EncryptDecrypt.Decrypt(DataSource, ConfigurationManager.AppSettings["EncryptDecryptKey"]); 
       InitialCatalog = CrossCutting.EncryptDecrypt.Decrypt(InitialCatalog, ConfigurationManager.AppSettings["EncryptDecryptKey"]); 
       UserName = CrossCutting.EncryptDecrypt.Decrypt(UserName, ConfigurationManager.AppSettings["EncryptDecryptKey"]); 
       PassWord = CrossCutting.EncryptDecrypt.Decrypt(PassWord, ConfigurationManager.AppSettings["EncryptDecryptKey"]); 

       return Content(JsonHelper.JsonForJqgrid(GetDataTable(sidx, sord, page, rows, DataSource, InitialCatalog, UserName, PassWord), rows, GetTotalCount(DataSource, InitialCatalog, UserName, PassWord), page), "application/json"); 
      } 

      public DataTable GetDataTable(string sidx, string sord, int page, int pageSize, string DataSource, string InitialCatalog, string UserName, string PassWord) 
      { 
       int startIndex = (page - 1) * pageSize; 
       int endIndex = page * pageSize; 
       string sql = "select name from sys.tables"; 
       DataTable dt = new DataTable(); 
       string connString = "Data Source=" + DataSource + ";Initial Catalog=" + InitialCatalog + ";User ID=" + UserName + ";pwd=" + PassWord; 
       SqlConnection conn = new SqlConnection(connString); 
       SqlDataAdapter adap = new SqlDataAdapter(sql, conn); 
       var rows = adap.Fill(dt); 
       return dt; 
      } 

      public int GetTotalCount(string DataSource, string InitialCatalog, string UserName, string PassWord) 
      { 
       SqlConnection sqlConnection2 = null; 
       string connString = "Data Source=" + DataSource + ";Initial Catalog=" + InitialCatalog + ";User ID=" + UserName + ";pwd=" + PassWord; 
       try 
       { 
        string cmdText = "select name from sys.tables"; 
        List<string> tablenames = new List<string>(); 
        using (sqlConnection2 = new SqlConnection(connString)) 
        { 
         sqlConnection2.Open(); 
         using (SqlCommand sqlCmd = new SqlCommand(cmdText, sqlConnection2)) 
         { 
          SqlDataReader reader = sqlCmd.ExecuteReader(); 
          while (reader.Read()) 
          { 
           tablenames.Add(reader.GetValue(0).ToString()); 
           //Count = (int)sqlCmd.ExecuteScalar(); 
          } 
         } 
        } 
        return tablenames.Count(); 
       } 
       catch 
       { 
       } 
       finally 
       { 
        try 
        { 
         if (ConnectionState.Closed != sqlConnection2.State) 
         { 
          sqlConnection2.Close(); 
         } 
        } 
        catch 
        { 
        } 
       } 
       return -1; 
      } 


    //JsonHelper.cs:- 

    using System; 
    using System.Collections.Generic; 
    using System.Data; 
    using System.Data.SqlClient; 
    using System.Linq; 
    using System.Text; 
    using System.Web; 

    namespace RuleEngine.Helper 
    { 
     public class JsonHelper 
     { 
      public static string JsonForJqgrid(DataTable dt, int pageSize, int totalRecords, int page) 
      { 
       string connString = "Data Source=" + "po8lt8zdqt.database.windows.net" + ";Initial Catalog=" + "iNubeRSBYClaims2" + ";User ID=" + "inubeadmin" + ";pwd=" + "Ravi*vikram123"; 
       string cmdText = "SELECT table_name,column_name FROM information_schema.columns Group By table_name,column_name ORDER BY table_name,column_name"; 

       List<SchemaCreation> AllTableAndColumnsName = new List<SchemaCreation>(); 
       using (SqlConnection SqlConn = new SqlConnection(connString)) 
       { 
        SqlConn.Open(); 
        using (SqlCommand SqlCmd = new SqlCommand(cmdText, SqlConn)) 
        { 
         SqlDataReader Reader = SqlCmd.ExecuteReader(); 
         while (Reader.Read()) 
         { 
          AllTableAndColumnsName.Add(new SchemaCreation { Table_Names = Reader.GetValue(0).ToString(), Column_Names = Reader.GetValue(1).ToString() }); 
         } 
        } 
       } 

       //List<string> ColumnNames = new List<string>(); 
       int totalPages = (int)Math.Ceiling((float)totalRecords/(float)pageSize); 
       StringBuilder jsonBuilder = new StringBuilder(); 
       jsonBuilder.Append("{"); 
       jsonBuilder.Append("\"total\":" + totalPages + ",\"page\":" + page + ",\"records\":" + (totalRecords) + ",\"rows\""); 
       jsonBuilder.Append(":["); 
       for (int i = 0; i < dt.Rows.Count; i++) 
       { 
        jsonBuilder.Append("{\"i\":" + (i) + ",\"cell\":["); 
        for (int j = 0; j < dt.Columns.Count; j++) 
        { 
         jsonBuilder.Append("\""); 
         jsonBuilder.Append(dt.Rows[i][j].ToString()); 
         jsonBuilder.Append("\","); 

         List<string> ColumnNames = new List<string>(); 
         string EachTableName = dt.Rows[i][j].ToString(); 
         ColumnNames = AllTableAndColumnsName.Where(p => p.Table_Names == EachTableName).Select(sw => sw.Column_Names).ToList(); 

         jsonBuilder.Append("\""); 
         foreach (var EachColName in ColumnNames) 
         { 
          jsonBuilder.Append(EachColName); 
          jsonBuilder.Append(","); 
         } 
         jsonBuilder.Append("\","); 

        } 
        jsonBuilder.Remove(jsonBuilder.Length - 1, 1); 
        jsonBuilder.Append("]},"); 
       } 
       jsonBuilder.Remove(jsonBuilder.Length - 1, 1); 
       jsonBuilder.Append("]"); 
       jsonBuilder.Append("}"); 
       return jsonBuilder.ToString(); 
      } 
     } 

     public class SchemaCreation 
     { 
      public string Table_Names { get; set; } 

      public string Column_Names { get; set; } 

      public List<string> ColumnNames { get; set; } 
     } 

    } 
+0

在colModel中,確保'name'和'index'屬性使用您的對象屬性名稱。我想它應該是像'ColumnName',不知道你是如何定義 –

+0

是的先生。,我想在jqGrid的第二列創建dropdownlist – user2231271

+0

我怎麼能發送我的jqGrid屏幕截圖。??我在這裏找不到選擇。 – user2231271

回答

0

在jqGrid的的colModel,nameindex性質使用您的C#對象屬性的名稱。我想這應該是像ColumnName,不知道你是如何定義的。請相應地改變它像

colModel: [ 
     { name: 'TableName', index: 'TableName', width: 40, align: 'left', 
               Style:'padding-left:15px;'}, 
     { name: 'ColumnName', index: 'ColumnName', width: 40, align: 'left', 
      width: 80, formatter: 'select', type:'select', edittype: 'select', 
      editoptions: { value: "1:One;2:Two" } } 
    ] 
+0

我的問題還在繼續。任何人都可以幫助我嗎? – user2231271

+0

Murali Murugesan先生,可以幫助我。 – user2231271

0
colModel: [ 
       { name: 'Table_Names', index: 'Table_Names', width: 40, align: 'left', Style:'padding-left:15px;'}, 
       { name: 'Column_Names', index: 'Column_Names', width: 40, align: 'left', width: 80, formatter: 'select', type:'select', edittype: 'select', 
       editoptions:{ 
       dataUrl: '/Schema/GetGridData?DataSource=' + '@ViewData["ServerName"]' + '&InitialCatalog=' + '@ViewData["DataBaseName"]' + '&UserName=' + '@ViewData["UserName"]' + '&PassWord=' + '@ViewData["PassWord"]', 
       buildSelect: function (data) { 
          var response, s = '<select>', i; 
          response = jQuery.parseJSON(data); 
          if (response && response.length) { 
           $.each(response, function (i) { 
            s += '<option value="' + this.Column_Names + '">' + this.Column_Names+ '</option>'; 
           }); 
          } 
          return s + '</select>'; 
         } 
    }}] 

我覺得現在你可以得到價值now.If你不要再看到firebug->控制檯,又回到了你。如果有什麼JSON你希望所有的值在