使用angular.js在每行中为数据表jquery添加一个复选框

时间:2019-01-15 09:59:04

标签: javascript jquery angularjs checkbox datatables

我想要的是,我想通过使用angular js以datatable格式显示一些数据。

所以我做了下面的事情。

角度

<script src="~/Scripts/jquery.js"></script>
<script src="~/Scripts/jquery.dataTables.min.js"></script>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/angular-datatables.js"></script>

var app = angular.module('myapp', ['datatables']);
app.controller('homectrl', ['$scope', '$http', 'dtoptionsbuilder', 'dtcolumnbuilder',
    function ($scope, $http, dtoptionsbuilder, dtcolumnbuilder) {
        $scope.dtcolumns = [
            //dtcolumnbuilder.newcolumn("action", "action"),
            dtcolumnbuilder.newcolumn("objectid", "id"),
            dtcolumnbuilder.newcolumn("service_code", "service code"),
            dtcolumnbuilder.newcolumn("cond1", "condition 1"),
            dtcolumnbuilder.newcolumn("cond2", "condition 2"),
            dtcolumnbuilder.newcolumn("cond3", "condition 3"),
            dtcolumnbuilder.newcolumn("service_type", "service type"),
            dtcolumnbuilder.newcolumn("remark", "remark"),
            dtcolumnbuilder.newcolumn("description", "description")
        ]
        $scope.dtoptions = dtoptionsbuilder.newoptions().withoption('ajax', {
            url: "/home/getdata",
            type: "post"
        })
        .withpaginationtype('full_numbers')
        .withdisplaylength(10);
    }])
<div ng-app="MyApp" class="container" ng-controller="homeCtrl">

    <div ng-controller="homeCtrl">
        <table id="entry-grid" datatable="" dt-options="dtOptions" dt-columns="dtColumns" class="table table-hover">           
        </table>
<br />
    </div>

控制器

public ActionResult getdata()
    {
        DataTable dt = new DataTable();
        using (OracleConnection conn = new OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnAPP_NEIQC"].ToString()))
        {   
            GetData objGetData = new GetData();
            dt =  objGetData.GetDataForGrid();

            var circleList = (from DataRow dr in dt.Rows
                           select new 
                           {         
                               //Action = "",
                               OBJECTID = Convert.ToString(dr["OBJECTID"]),
                               SERVICE_CODE = Convert.ToString(dr["SERVICE_CODE"]),
                               COND1 = Convert.ToString(dr["COND1"]),
                               COND2 = Convert.ToString(dr["COND2"]),
                               COND3 = Convert.ToString(dr["COND3"]),
                               SERVICE_TYPE = Convert.ToString(dr["SERVICE_TYPE"]),
                               REMARK = Convert.ToString(dr["REMARK"]),
                               DESCRIPTION = Convert.ToString(dr["DESCRIPTION"]),
                           }).ToList();

            return Json(circleList, JsonRequestBehavior.AllowGet);
        }
    }

现在我想要的是,我想在每行中添加一个CHECKBOX,以便我可以编辑和更新其记录。有没有人可以帮助我解决这个问题。

2 个答案:

答案 0 :(得分:1)

是这个吗?select插件。 withSelect

答案 1 :(得分:1)

如yujinpan所述,您可以使用select扩展名。或者只是自己渲染一个复选框:

$scope.dtcolumns = [
  dtcolumnbuilder.newcolumn(null, '').renderWith(function(data, type, full)
     return '<input type="checkbox" class="check" data-object-id="'+full.objectid+'">'
  }),
  dtcolumnbuilder.newcolumn("objectid", "id"),
  ...
]

现在,您可以通过.check类将委派的事件处理程序与复选框相关联。


按照here或在文档here中的说明使用dtInstance(请参阅页面底部)。现在您可以做类似的事情

$('#entry-grid').on('click', '.check', function() {
   var data = $scope.dtInstance.DataTable.row($(this).closest('tr')).data()
})