javascript - Pass value of checkbox and dropdown into same array -


i working on creating form allow user add new capability collection based on capabilities select. form structured table consists of checkboxs , dropdowns. checkbox allows users select capability wish add , drop down allows them select level of expertise capability.

here html form:

<form class="listofcaps">     <table>                 <tbody>             <tr ng-repeat-start="cap in capsarr"></tr>             <tr ng-repeat-start="capname in filteredcaps track capname.cid"></tr>                 <td>                     <input type="checkbox" name="selectedcaps[]" value="{{capname}}" ng-model="capname.selected" /> <span>{{capname.capname}}</span>                 </td>                 <td>                     <span class="custom-dropdown custom-dropdown--white">                         <select                              class="custom-dropdown__select custom-dropdown__select--white"                               name = "selectedexp[]"                              ng-options = "exp.id exp.level exp in expertise"                              ng-model = "exp">                             <option value="">select one</option>                              </select>                         </span>                 </td>             <tr ng-repeat-end="capname in cap.capabilities"></tr>             <tr ng-repeat-end="cap in capsarr"></tr>         </tbody>     </table> </form> 

what want have value of checkbox , drop down populate array or update value in array if changed. have tried doing , have been able checkbox values populate in array. used $watch whenever checked added , when unchecked removed. cannot figure out how apply drop down value in same $watch these values added same object in array.

here controller code have:

$scope.expertise = [     {id: '1', level: 'certified'},     {id: '2', level: 'knowledgable'},     {id: '3', level: 'interested'} ];  $scope.selection = [];  $scope.$watch('filteredcaps | filter:{selected:true}', function (newvalues) {     $scope.selection = newvalues.map(function (capname) {         return {"capname" : capname.capname, "cid": capname.cid};     }); }, true); 

note: filteredcaps array stored in $scope.filteredcaps previous function. array contains objects consist of capabilities name , id

if doesn't need array, can make selection object:

$scope.selection = {}; 

and on view:

<input type="checkbox" ... ng-model="selection[capname]" ... /> <select ... ng-model = "selection[capname + '_exp']" ...> 

so selection object should have property capname value capname if checkbox selected, , property capname_exp value dropdown.

when want save database, can iterate on properties of object:

for (var prop in obj) {   // logic here } 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -