0

I want to insert new row into table through dialog pop window. This window has form and Add Task button to insert the new row into table. I tries every thing but not able to connect ng-model of this dialog to the table.

Please see the demo:

index.html

<html>

<head>
<title>ToDo API Client Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>

</head>

<body ng-app="myApp" ng-controller="tasksCtrl">
  <div class="navbar">
        <div class="navbar-inner">
            <a class="brand" href="#">ToDo API Client Demo</a>
        </div>
  </div>
  <div>

         <table class="table table-striped">
             <tr><td style="width: 1px;"></td><td><b>Task</b></td><td><b>Options</b></td></tr>
            <tr ng-repeat="task in tasks">

            <td>{{task.title}}</td>
            <td>{{task.description}}</td>
            <td>  <a class="btn" data-toggle="modal" data-target="#modal" ng-click="editTask(task)">Edit</a></td>
                <td>  <a class="btn" data-toggle="modal" data-ng-click="removeRow(task)">Delete</a></td>


            </tr>
         </table>

            <a class="btn" data-toggle="modal" data-target="#add" ng-click="editTask(task)">Add Task</a>
  </div>
  <div id="modal" role="dialog" class="modal hide fade">
        <div>
            <div class="modal-header">
                Task Dialog
            </div>
            <div class="modal-body">
                <label for="txtName"></label> 
                <input type="text"  ng-model="selectedTask.title" />
                <input type="text"  ng-model="selectedTask.description" />
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" ng-click="saveTask()" data-dismiss="modal">OK</button>
            </div>
        </div>
   </div>

   <div id="add" class="modal hide fade" tabindex="=1" role="dialog" aria-labelledby="addDialogLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="addDialogLabel">Add Task</h3>
        </div>
        <div class="modal-body">
            <form class="form-horizontal">
                <div class="control-group">
                    <label class="control-label" for="inputTask">Task</label>
                    <div class="controls">
                        <input type="text" id="inputTask" ng-model="task1" placeholder="Task title" style="width: 150px;"><br />
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="inputDescription">Description</label>
                    <div class="controls">
                        <input type="text" id="inputDescription" ng-model="description1" placeholder="Description" style="width: 300px;">
                    </div>
                </div>
            </form>
        </div>
        <div class="modal-footer"> 
            <a class="btn btn-primary" data-toggle="modal" ng-click="addNewTask()">Add Task</a>
            <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
        </div>
    </div>


   <script>
    var app = angular.module('myApp', []);
    app.controller('tasksCtrl', function($scope, $http) {
        $http.get("data.json")
        //$http.get("/todo/api/v1.0/tasks")
        .success(function(response) {
          console.log(response.tasks)
          $scope.tasks = response.tasks;
        });

        $scope.editTask=function(task) {
            $scope.selectedTask = task;
        };
        $scope.removeRow = function (task) {
            $scope.tasks.splice(task, 1);
        };
        $scope.addNewTask=function(){
            //$scope.tasks.push({$scope.task1, $scope.description1});
        }
    });


  </script>
</body>

</html>
0

2 Answers 2

1

On your AddNewTask function you pass a wrong object :

 $scope.addNewTask=function(){
      $scope.tasks.push({$scope.task1, $scope.description1});
 }

Should be

$scope.addNewTask=function(){
     $scope.tasks.push({title : $scope.task1, description : $scope.description1});
 }

You missed Object keys.

Sign up to request clarification or add additional context in comments.

Comments

0

You were not adding a valid map to the array of tasks.

Since you are accessing task.title and task.description those need to be valid fields in the map/JSON. Initially this is coming in as formatted JSON where:

{
    key: value,
    key: value,
    ...
}

So for your data you were trying to do:

       $scope.tasks.push({$scope.task1, $scope.description1});

But since there is no key for the task or description the data can understand so what you need is this:

 $scope.addNewTask=function(){
       $scope.tasks.push({"title":$scope.task1, "description":$scope.description1});
    }

Im not sure if the other person already answered this. I was delayed in posting my response.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.