can not add text box input in table

Hamed Minaee

I have the following code :

http://plnkr.co/edit/RqLurBaCsgjQjOYMtl8r?p=preview

here there is a textbox and when user add something to the textbox and push add button then the entered text should be added to the table Here is my javaScript code:

var app = angular.module('app', []);
app.factory('Service', function() {
    var typesHash = [ {
        id : '1',
        name : 'lemon',
        price : 100,
        unit : 2.5
    }, {
        id : '2',
        name : 'meat',
        price : 200,
        unit : 3.3
    } ];
    var service = {
        addTable : addTable,
        getData : getData,


    };
    return service;
    function addTable(data) {

        typesHash.push(data);
    }
    function getData() {
        return typesHash;
    }
});
app.controller('table', function(Service) {
    //get the return data from getData funtion in factory
    this.typesHash = Service.getData();
    this.testData = {
        id : '1',
        name : "test",
        price : 100,
        unit : 2.5
    };
    //get the addtable function from factory 
    this.addTable = Service.addTable;
});

here as far as testData is static as follow it works:

this.testData = {
        id : '1',
        name : "test",
        price : 100,
        unit : 2.5
    };

but here the text in the textbox is not added so I changed the above code as follow:

this.testData = {
        id : '1',
        name : $("#txt").val(),
        price : 100,
        unit : 2.5
    };

the name gets nothing and row is added but name spot is empty?

Just a quick note that this is a simpler version of my real code and I have a reason to use factory. Can ahyone help me to find out why this table does not connect to textbox correctly?

shaunhusain

Modified version of the plnkr (ooo nice design changes SO).

Updated pasted a bad plnkr link before. http://plnkr.co/edit/4g7LGRLBNEH2LeuEm1qN?p=preview

code from the post, let me know if this doesn't cover some scenario you were imagining. I tried getting rid of all the style cruft, that should be done in CSS or using things like text-right or text-center provided by bootstrap.

<html>

  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <link data-require="bootstrap@*" data-semver="3.3.1" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
    <title>Insert title here</title>
    <script>
    var app = angular.module('app', []);
    app.factory('Service', function() {
        var typesHash = [ {
            id :1,
            name : 'lemon',
            price : 100,
            unit : 2.5
        }, {
            id : 2,
            name : 'meat',
            price : 200,
            unit : 3.3
        } ];

        var localId = 3;

        var service = {
            addTable : addTable,
            getData : getData,


        };
        return service;
        function addTable(name) {
            typesHash.push({id:localId++, name:name, price:100,unit:1});
        }
        function getData() {
            return typesHash;
        }
    });
    app.controller('table', function(Service) {
        //get the return data from getData funtion in factory
        this.typesHash = Service.getData();
        //get the addtable function from factory 
        this.addTable = Service.addTable;
    });
    </script>
  </head>

  <body ng-app="app" ng-controller="table as tableTools">
    <form>
      <div class="row commonRow">
        <div class="col-xs-1 text-right">
          item:
        </div>
        <div class="col-xs-5">
          <input id="txt" type="text" style="width: 100%;" ng-model="tableTools.inputData" />
        </div>
        <div class="col-xs-2">
          <button class="btn btn-primary" ng-click="tableTools.addTable(tableTools.inputData);tableTools.inputData=''">
                click me
              </button>
        </div>
      </div>  
    </form>

    <div class="row commonRow">
      <div class="col-xs-1"></div>
      <div class="col-xs-10">
        <table class="table table-hover">
          <thead>
            <tr>
              <th>item</th>
            </tr>
          </thead>
          <tbody ng-controller="table as iterateTb">
            <tr ng-repeat="x in iterateTb.typesHash track by x.id">
              <td>
                <div>{{x.name}}</div>
              </td>
              <td>
                <input type="text" ng-model="x.name"/>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </body>

</html>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I add a comment in word in a text box?

분류에서Dev

regex for text box to validate input

분류에서Dev

how can i add dynamic labels and text input field in xcode

분류에서Dev

sending input from text box to controller

분류에서Dev

How can I input data to excel from inputting data in text box where the text box are from two or more Userform Using Excel VB?

분류에서Dev

jQuery - Add a option box to a table row

분류에서Dev

How to make form input box larger in HTML (along with text)?

분류에서Dev

Data Table input type as text instead of search

분류에서Dev

Add new text box on click of a button in angular js

분류에서Dev

I Want to add check box for every table row in angular js

분류에서Dev

Can't paste into text input in FF

분류에서Dev

add value to current value of text input in flash AS3?

분류에서Dev

Keep table from expanding when text is input with PHP

분류에서Dev

read input text value when button click in table

분류에서Dev

leaflet geocoding with input box

분류에서Dev

Can't get PHP script to write user input to text file

분류에서Dev

Animated text within box

분류에서Dev

choose a name from drop down list or if not exit, can write in text box

분류에서Dev

How to add a sitewide text area to a Wordpress Site that can be changed in the dashboard

분류에서Dev

How can I add a text view for every item in my list?

분류에서Dev

how can i add and list a item to cart when checked a check box?

분류에서Dev

How can i achieve truncated text in a responsive width table column?

분류에서Dev

How can I add a paragraph directly after Word table?

분류에서Dev

Can't add record with ID from another table

분류에서Dev

Autocomplete search box input field

분류에서Dev

How to validate a second text box if there is a value in first text box?

분류에서Dev

how to concatenate two or more text box values in one text box

분류에서Dev

WPF Suggestion text box with MVVM

분류에서Dev

Android white text in search box

Related 관련 기사

  1. 1

    How can I add a comment in word in a text box?

  2. 2

    regex for text box to validate input

  3. 3

    how can i add dynamic labels and text input field in xcode

  4. 4

    sending input from text box to controller

  5. 5

    How can I input data to excel from inputting data in text box where the text box are from two or more Userform Using Excel VB?

  6. 6

    jQuery - Add a option box to a table row

  7. 7

    How to make form input box larger in HTML (along with text)?

  8. 8

    Data Table input type as text instead of search

  9. 9

    Add new text box on click of a button in angular js

  10. 10

    I Want to add check box for every table row in angular js

  11. 11

    Can't paste into text input in FF

  12. 12

    add value to current value of text input in flash AS3?

  13. 13

    Keep table from expanding when text is input with PHP

  14. 14

    read input text value when button click in table

  15. 15

    leaflet geocoding with input box

  16. 16

    Can't get PHP script to write user input to text file

  17. 17

    Animated text within box

  18. 18

    choose a name from drop down list or if not exit, can write in text box

  19. 19

    How to add a sitewide text area to a Wordpress Site that can be changed in the dashboard

  20. 20

    How can I add a text view for every item in my list?

  21. 21

    how can i add and list a item to cart when checked a check box?

  22. 22

    How can i achieve truncated text in a responsive width table column?

  23. 23

    How can I add a paragraph directly after Word table?

  24. 24

    Can't add record with ID from another table

  25. 25

    Autocomplete search box input field

  26. 26

    How to validate a second text box if there is a value in first text box?

  27. 27

    how to concatenate two or more text box values in one text box

  28. 28

    WPF Suggestion text box with MVVM

  29. 29

    Android white text in search box

뜨겁다태그

보관