简体   繁体   English

使用angularjs,jquery,json,ajax基于下拉选择绑定html表数据

[英]bind html table data based on dropdown seletion using angularjs,jquery,json,ajax

please rectify my corrections it not working i made a mistakes please rectify. 请纠正我的更正,它无法正常工作,我犯了一个错误,请纠正。 i need html table based on selection. 我需要基于选择的html表。 i tried but i cannot find any solution. 我尝试过,但找不到任何解决方案。 i created drop down if i select any value in drop down & click button it display the selected table 我创建了下拉菜单,如果我在下拉菜单中选择任何值并单击按钮,则会显示所选表格

var app = angular.module("myApp", []);
        app.controller("myCntrl", function ($scope, $http) {
$("#Button1").click=function(RoleID){
var httpreq={
method:'POST',
 url: 'WebForm1.aspx/getdata',
   headers: {
                    'Content-Type': 'application/json; charset=utf-8',
                    'dataType': 'json'
                },
                data: {}
            }
            $http(httpreq).success(function (response) {
 $scope.RolesList = response.d;

}) }; })};

 <input id="Button1" type="button" class="button" value="button" ng-click="click()"  />

public List<User> getdata(int RoleName)
       {
           string strConnection = "Data Source=192.168.1.42,1433;Initial Catalog=Harneedi;User ID=chaitanya_t;Password=makrotech";
           List<User> userobj1 = new List<User>();
           DataTable dt = new DataTable();
           SqlConnection con = new SqlConnection(strConnection);
           con.Open();
           SqlCommand cmd = new SqlCommand("select userName,[RoleName],[status] from HN_Users_Details as t1 inner join HN_Master_User_Role as t2 on  t1.RoleID=t2.RoleID where RoleName='"+RoleName+"'", con);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           da.Fill(dt);
           if (dt.Rows.Count > 0)
           {
               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   User userinfo = new User();
                   userinfo.UserName = dt.Rows[i]["UserName"].ToString();
                   userinfo.RoleName = dt.Rows[i]["RoleName"].ToString();
                   userinfo.status = dt.Rows[i]["status"].ToString();
                   userobj1.Add(userinfo);
               }
           }
           return userobj1;
       }

public class User{public string UserName { get; set; }public string RoleName { get; set; }public string status { get; set; }}

please rectify my corrections 请纠正我的更正

Okay! 好的! Let's get to it. 让我们开始吧。

You have this button in your current scope of work: 您当前的工作范围中包含以下按钮:

<input id="Button1" type="button" class="button" value="button" ng-click="click()"  />  

As i can see you are triggering the click() method defined in your controller's scope. 如我所见,您正在触发在控制器范围内定义的click()方法。 so you have defined it badly i would say this don't mind . 所以您对它的定义很不好,我会说这不介意 In my opinion you have to declare the method this way: 我认为您必须以这种方式声明该方法:

var app = angular.module("myApp", []);
app.controller("myCntrl", function ($scope, $http) {
   $scope.RolesList = {}; // declare the role list here
   $scope.click=function(RoleID){ // not sure how you pass this role id
      var httpreq={
         method:'POST',
         url: 'WebForm1.aspx/getdata',
         headers: {
            'Content-Type': 'application/json; charset=utf-8' // this is useless it is default in angularjs
         },
         responseType: 'json', // use responseType instead of dataType
         data: {}
      };
      $http(httpreq).then(function (response) { // ".success" is deprecated standard is set to use ".then()"
             $scope.RolesList = response.d;
      });
});

It would be nice if you can create a sample demo here @plnkr . 如果您可以在@plnkr处创建示例演示,那将是很好的。

Though it is not clear exactly what your problem is, the basic understanding is that the code is not running properly, what I understand is, you have set your content type as json, but we are returning list from the getdata method. 尽管不清楚您的问题到底是什么,但基本的理解是代码无法正常运行,据我了解,您已将内容类型设置为json,但是我们正在从getdata方法返回list。

So we need to serialize the data and return as json data. 因此,我们需要序列化数据并作为json数据返回。

da.Fill(dt);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
{
     row = new Dictionary<string, object>();
     foreach (DataColumn col in dt.Columns)
     {
        row.Add(col.ColumnName, dr[col]);
     }
     rows.Add(row);
}
return serializer.Serialize(rows);

And change the return type of the method as string, I suppose that would solve your problem. 并将方法的返回类型更改为字符串,我想这将解决您的问题。

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

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