Two dimensional array in table with unique ng model

1,604 views
Skip to first unread message

Don jose

unread,
Jun 14, 2016, 6:40:22 AM6/14/16
to AngularJS
  
i am having ng repeat in <tr> and <td> i am trying to achieve a time table like structure thus require unique ng model for each cell in table
here is my code
   <tr ng-repeat="day in days track by $index">
        <td>
         <input ng-disabled="true" class="form-control" ng-model="weekdays[$index]"></td>
<td ng-repeat="period in periods track by $index"><input type="text"
        ng-model="timetable.subject[[$parent.$index][$index]]"
        name="name"
        uib-typeahead="onesectionsubject as onesectionsubject.subjectMaster.subjectName+'/'+onesectionsubject.subjectTeacher.firstName for onesectionsubject in onesectionsubjects | filter:$viewValue | limitTo:8"
        class="form-control" pattern="[A-Za-z0-9 ]*"
        uib-tooltip="Enter valid subject name"
        tooltip-enable="form.name.$invalid" autocomplete="off">
        </td>
    </tr>

Sander Elias

unread,
Jun 15, 2016, 2:23:10 AM6/15/16
to AngularJS
Hi Don,

It would be easier to help you when you demonstrator your issue in a plunk.
Probably you need to use ngInit in this case.

Regards
Sander

Don jose

unread,
Jun 15, 2016, 6:11:41 AM6/15/16
to AngularJS
there is option for both adding periods and days
 

Sander Elias

unread,
Jun 15, 2016, 8:00:08 AM6/15/16
to AngularJS
Hi Don,

I forked your plunk, and made it to work,
The $scope.timetable now holds an 2 dimensional array of the results that are put in the table.
Added an json output, so you can see what is in the array.

Is this what you where looking for?

Regards
Sander

Don jose

unread,
Jun 16, 2016, 1:13:32 AM6/16/16
to AngularJS
Thank you so much sir! This is what i am looking for!

Don jose

unread,
Jun 16, 2016, 1:21:40 AM6/16/16
to AngularJS
Sir,
Can you explain why ng init is used?
$parent.$index will not work?? 

Sander Elias

unread,
Jun 16, 2016, 1:26:53 AM6/16/16
to AngularJS
Hi Don,

Well, it will work, but its a bit to fragile in my opinion. If you insert another repeat, or do something else, it might mess up the $parent. Using ngInit, makes it explicit, and using a properly named variable makes its usage more clear, and as a result your template is easier to maintain.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages