Im using angularjs to show a table from a json records with checkbox, and i have two problems:
First: I need all checkboxs true by default. Tested with ng-init but not working. Or just a button to toggle true-false all checkboxs
Second When uncheck a file, the object show quotes["", "", ""], but i need to show anything if not checked
view
<div ng-controller="TestController">
<table class="table table-striped">
<tr>
<th>Seleccionar</th>
<th>Servicio</th>
<th>Detalle</th>
<th>Precio</th>
</tr>
<tr ng-repeat="(key, value) in listado">
<td><input type="checkbox" ng-model="ids[$index]" ng-true-value="{{value}}" ng-false-value="{{ undefined }}" ng-selected="checkAll"></td>
<td>{{ value.modelo }}</td>
<td>{{ value.detalle }}</td>
<td>{{ value.precio_in | currency:'$':0 }}</td>
</tr>
</table>
{{ ids | json }}
controller
var myApp = angular.module('myApp', [])
.controller('TestController', ['$scope', function ($scope) {
$scope.listado = [];
$scope.ids = [];
$scope.listado = [ { "id_stock": "4", "oc_id": "4", "detalle": "Revisión de suspención", "cat_id": "16", "codigo": "m20.1", "marca": "", "parte": "", "precio_in": "5000", "id_prov": "1", "cantidad": "1", "id_bodega": "1", "nom_prov": "Proveedor de prueba 1", "modelo": "MECANIZADOS Y OTROS", "nombre": "Bodega Conchalí", "status": "EN BODEGA", "id_cat": "16" }, { "id_stock": "5", "oc_id": "4", "detalle": "Revisión de frenos", "cat_id": "16", "codigo": "m2.2", "marca": "", "parte": "", "precio_in": "4500", "id_prov": "1", "cantidad": "1", "id_bodega": "1", "nom_prov": "Proveedor de prueba 1", "modelo": "MECANIZADOS Y OTROS", "nombre": "Bodega Conchalí", "status": "EN BODEGA", "id_cat": "16" }, { "id_stock": "6", "oc_id": "4", "detalle": "Revisión de ruedas", "cat_id": "16", "codigo": "m20.3", "marca": "", "parte": "", "precio_in": "4500", "id_prov": "1", "cantidad": "1", "id_bodega": "1", "nom_prov": "Proveedor de prueba 1", "modelo": "MECANIZADOS Y OTROS", "nombre": "Bodega Conchalí", "status": "EN BODEGA", "id_cat": "16" }]; }]);
Check code /
Sorry for the english is not my native language. Thanks !!
Im using angularjs to show a table from a json records with checkbox, and i have two problems:
First: I need all checkboxs true by default. Tested with ng-init but not working. Or just a button to toggle true-false all checkboxs
Second When uncheck a file, the object show quotes["", "", ""], but i need to show anything if not checked
view
<div ng-controller="TestController">
<table class="table table-striped">
<tr>
<th>Seleccionar</th>
<th>Servicio</th>
<th>Detalle</th>
<th>Precio</th>
</tr>
<tr ng-repeat="(key, value) in listado">
<td><input type="checkbox" ng-model="ids[$index]" ng-true-value="{{value}}" ng-false-value="{{ undefined }}" ng-selected="checkAll"></td>
<td>{{ value.modelo }}</td>
<td>{{ value.detalle }}</td>
<td>{{ value.precio_in | currency:'$':0 }}</td>
</tr>
</table>
{{ ids | json }}
controller
var myApp = angular.module('myApp', [])
.controller('TestController', ['$scope', function ($scope) {
$scope.listado = [];
$scope.ids = [];
$scope.listado = [ { "id_stock": "4", "oc_id": "4", "detalle": "Revisión de suspención", "cat_id": "16", "codigo": "m20.1", "marca": "", "parte": "", "precio_in": "5000", "id_prov": "1", "cantidad": "1", "id_bodega": "1", "nom_prov": "Proveedor de prueba 1", "modelo": "MECANIZADOS Y OTROS", "nombre": "Bodega Conchalí", "status": "EN BODEGA", "id_cat": "16" }, { "id_stock": "5", "oc_id": "4", "detalle": "Revisión de frenos", "cat_id": "16", "codigo": "m2.2", "marca": "", "parte": "", "precio_in": "4500", "id_prov": "1", "cantidad": "1", "id_bodega": "1", "nom_prov": "Proveedor de prueba 1", "modelo": "MECANIZADOS Y OTROS", "nombre": "Bodega Conchalí", "status": "EN BODEGA", "id_cat": "16" }, { "id_stock": "6", "oc_id": "4", "detalle": "Revisión de ruedas", "cat_id": "16", "codigo": "m20.3", "marca": "", "parte": "", "precio_in": "4500", "id_prov": "1", "cantidad": "1", "id_bodega": "1", "nom_prov": "Proveedor de prueba 1", "modelo": "MECANIZADOS Y OTROS", "nombre": "Bodega Conchalí", "status": "EN BODEGA", "id_cat": "16" }]; }]);
Check code http://jsfiddle/eCJ47/44/
Sorry for the english is not my native language. Thanks !!
Share Improve this question asked Apr 18, 2018 at 0:57 Andres CCAndres CC 531 silver badge6 bronze badges 2-
You are assigning entire object to
ng-model
. Is that what you want? Wouldn't it be clean if you assignng-true-value="{{value.id_stock}}"
? – Gowthaman Commented Apr 18, 2018 at 2:28 - Yes , i need all object, not just the id – Andres CC Commented Apr 18, 2018 at 4:55
1 Answer
Reset to default 5Modified your code a bit, instead of assigning object to a new array, added a new property isSelected
to the existing listado
array and bind to the checkbox and initializing it as true by default.
<tr ng-repeat="(key, value) in listado" ng-init="value.isSelected = true;">
<td><input type="checkbox" ng-model="value.isSelected"></td>
<td>{{ value.modelo }}</td>
<td>{{ value.detalle }}</td>
<td>{{ value.precio_in | currency:'$':0 }}</td>
</tr>
Print listado
to verify checkbox changes
Plunker http://jsfiddle/Vinthi/4atbzo6L/3/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745330823a4622868.html
评论列表(0条)