Show fetched data in a table using angularjs - angularjs

I want to show data in a table using angularjs in codeigniter.
Controller:
public function source_list_for_test(){
$data['get_source'] = $this-> admin_model-> get_all_source_list_for_test();
echo json_encode($data['get_source']);
}
Model:
function get_all_source_list_for_test() {
$this->db->select('*');
$this->db->from('source');
$query = $this->db->get();
return $query->result_array();
}
View:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js">
</script>
<div class="table-responsive" ng-app="myapp" ng-controller="tabletest">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Sl No.</th>
<th>Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="data in datas">
<td>{{index + 1}}</td>
<td>{{data.name}}</td>
<td><a class="btn btn-info btn-sm btn-fill" href="">
<i class="fa fa-pencil-square-o"></i> Edit</a>
<a class="btn btn-danger btn-sm btn-edit btn-fill delete" id="" ><i class="fa fa-trash"></i> Delete</a>
</td>
</tr>
</tbody>
</table>
</div>
<script src="<?php echo base_url()?>app/js_code.js></script>
Js file is
<script type="text/javascript">
var app = angular.module('myapp',[]);
app.controller('tabletest', function($scope, $http) {
$http.get(baseUrl + "admin/source_list_for_test")
.then(function(response) {
$scope.datas = response.data;
});
});
</script>
I tried this but data not fetched and not bind in my view page.this shows below screen shoot.
enter image description here

did you check for app.js file, may be app.js not downloaded.

Related

How does the html table tag does not working inside the ng-template(Angularjs)?

I have tried the following using ui.bootstrap.modal).
Here is the code that I am using for the bootstrap.modal:
In the HTML file:
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3 class="modal-title" id="modal-title">Program Account Checklist!</h3>
</div>
<div class="modal-body" id="modal-body">
<table>
<colgroup span="2"></colgroup>
<thead>
<tr>
<th colspan="2" scope="colgroup">Is your organization:</th>
<th>CRA Program Account</th>
</tr>
</thead>
<tbody>
<tr>
<td> checkbot </td>
<td> info </td>
<td> account </td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" ng-click="">Cancel</button>
<button class="btn btn-primary" type="button" ng-click="">Clear</button>
<button class="btn btn-primary" type="button" ng-click="">Submit</button>
</div>
</script>
In my controller.js:
$scope.open = function() {
var modalInstance = $uibModal.open({
animation: true,
ariaLabelledBy: 'modal-title',
ariaDescribedBy: 'modal-body',
templateUrl: 'myModalContent.html',
resolve: {
items: function () {
return $scope.checklist;
}
},
size: 'lg'
})
Now the problem is that when I tried to pop up a window with a table in it, the data are showing on the right position, but there isn't any ui for the table. Everything seems like in the plain text. I guess it has something to do with the type="text/ng-template". But I couldn't figure out a way to work around it. How can I resolve this problem?
If I understood your question correctly, You are missing bootstrap styling on the table?, In that case add some class names like so:
<table class="table table-borderless">
</table>

Get angularjs post value in controller

I want to get post value which i have send in $http.post() in controller when i click on edit button.
view page
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Sl No.</th>
<th>Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="data in datas">
<td>{{$index + 1}}</td>
<td>{{data.name}}</td>
<td>
<a class="btn btn-info btn-sm btn-fill" ng-click="editsource(data.id)" data-toggle="modal" data-target="#myModal"><i class="fa fa-pencil-square-o"></i> Edit</a>
<a class="btn btn-danger btn-sm btn-edit btn-fill delete"><i class="fa fa-trash"></i> Delete</a>
</td>
</tr>
</tbody>
My script
<script type="text/javascript">
var app = angular.module('myapp', []);
app.controller('tabletest', function ($scope, $http) {
$http.get("<?php echo base_url() ?>admin/source_list_for_test")
.then(function (response) {
$scope.datas = response.data;
});
$scope.editsource = function (id) {//alert('hello');
$http.post("<?php echo base_url() ?>admin/edit_source_for_test?id="+id).success(function (data) {
alert(data);
$scope.id = data['id'];
$scope.name = data['name'];
});
};
});
controller
public function edit_source_for_test(){
$id= $_GET['id'];
$data['source_data'] = $this->admin_model->get_edit_source_for_test($id);
echo json_encode($data['source_data']);
}
Above is my code but id not get in controller,How i get selected record id in controller.

How to show and edit my detail view in a seperate pages in angularjs

I have a table grid view in which i can get all my data ..At present When i click on show button the detail views is displaying in the same page..But i want to display that detailview in another page ....
From this page i can get my data and show detailview in the same page:
<button>Add Products </button>
<label>Search</label>
<input class="search" type="text" placeholder="Search" ng-model="searchBox"> <i class="glyphicon glyphicon-search"></i>
<table class="table table-bordered" ng-init="getProducts()">
<thead>
<tr>
<th>ID
</th>
<th>Category
</th>
<th>Product
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="productInfo in products" ng-show="(products | filter:criteria).length" ng-style = "{'background-color': $index == selectedIndex ? 'lightgray': ''}">
<td>{{ $index + 1 }}</td>
<td>{{productInfo.categoryname}}</td>
<td>{{productInfo.productname}}</td>
<td>
<button href="#/showproducts" ng-click="selectProduct(productInfo, $index)" class="mdl-button mdl-js-button mdl-button--raised mdl-button--colored"> Show </button>
Edit
<a ng-click="delete(productInfo);" class="btn btn-primary " ><i class="glyphicon glyphicon-trash"></i>Delete</a>
</td>
</tr>
</tbody>
</table>
<div class="col-md-4">
<ul>
{{selectedProduct.categoryname}} //I want to these getdetails into another html file
{{selectedProduct.productname}}
{{selectedProduct.product}}
{{selectedProduct.noofservices}}
</ul>
</div>
this is my controller:
var userid1 = sessionService.get('userid');
$scope.getProducts = function(){
$http.get('**/getproducts.php/? userid='+userid1).then(function(response){
$scope.products = response.data;
$scope.selectedIndex = null;
$scope.selectedProduct = null;
$scope.selectProduct = function(productInfo, index){
$scope.selectedIndex = index;
$scope.selectedProduct = productInfo;
console.log(productInfo);
};
Take a look at UI-router https://github.com/angular-ui/ui-router
It lets you define different states of your application as well as their corresponding URLs.
Essentially,
.config(($stateProvider, $urlRouterProvider) => {
$stateProvider
.state('table', {
url: '/table',
templateUrl: 'table.view.html'
})
.state('tableDetail',{
url: '/tabledetail',
template: 'table.detail.html'
})
})
And then in your views:
main.html
<div ui-view></div>
table.view.html
...
<button><a ui-sref="tableDetail">Table Detail</a></button>
...
table.detail.html
...
<!-- Table Detail Stuff -->

Decent way to toggle the text of bootstrap popover?

I'm using the angular + bootstrap to create a table and for each row, it will have a popover button. What I want to do is to change 'Show Password' to 'Hide Password' when the popover is shown, and change back when the popover is closed.
<tr ng-repeat="entry in data">
<td>{{ entry.site }}</td>
<td>{{ entry.username }}</td>
<td>
<button popover-placement="right" uib-popover="{{calculatePassword(entry)}}" class="btn btn-primary btn-sm">Show Password</button>
</td>
</tr>
I tried to use lines such as 'displayed? "Show Password":"Hide Password"' but I can't find a proper spot to toggle 'displayed'. I can't find a built-in feature of uib-popover to do that neither. Please help. Thanks!
You could use ng-click to toggle a variable each time the button is clicked and change the text accordingly.
<button ng-click="entry.passwordDisplayed = !entry.passwordDisplayed">
{{entry.passwordDisplayed ? 'Hide' : 'Show'}} Password
</button>
var app = angular.module("app", ["ui.bootstrap"]);
app.controller("controller", function($scope, $sce) {
$scope.data = [
{
site: "Facebook",
username: "jsmith",
password: "abc123"
}
];
var trusted = {};
$scope.htmlPopover = function(entry) {
var html = '<b>Password:</b> ' + entry.password;
return trusted[html] || (trusted[html] = $sce.trustAsHtml(html));
};
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.2.0/ui-bootstrap-tpls.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.2.0/ui-bootstrap-tpls.min.js"></script>
<div ng-app="app" ng-controller="controller">
<div class="wrapper">
<table class="table">
<thead>
<tr>
<th>Site</th>
<th>Password</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="entry in data">
<td>{{ entry.site }}</td>
<td>{{ entry.username }}</td>
<td>
<button ng-click="entry.passwordDisplayed = !entry.passwordDisplayed" class="btn btn-primary btn-sm" uib-popover-html="htmlPopover(entry)" class="btn btn-default">{{entry.passwordDisplayed ? 'Hide' : 'Show'}} Password</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>

how to pass thymeleaf object to angularjs

I am using Spring-boot, thymeleaf and Angularjs. In my my view.html I need to recover the list "lstUsers" from my class "controller.java" using Angular.
For informations: "view.html" and "controller.java" works good. The problem is how I can fill my variable "$scope.users" with "lstUsers"
this is app.js
var app = angular.module('angularTable', ['angularUtils.directives.dirPagination']);
app.controller('listdata',function($scope, $http){
$scope.users = [];
$scope.users = $lstUsers; // (lstUsers) is an object from controller.java: this not work !!!!!!!
$scope.sort = function(keyname){
$scope.sortKey = keyname; //set the sortKey to the param passed
$scope.reverse = !$scope.reverse; //if true make it false and vice versa
}
});
this is my view.html
<body>
<div role="main" class="container theme-showcase">
<div class="" style="margin-top:90px;">
<div class="col-lg-8">
<div class="bs-component" ng-controller="listdata">
<form class="form-inline">
<div class="form-group">
<input type="text" ng-model="search" class="form-control input-style search-input" placeholder="Search" />
</div>
</form>
<table class="table table-responsive table-hover table-striped table-bordered tablesorter">
<thead>
<tr>
<th ng-click="sort('id')">Id
<span class="glyphicon sort-icon" ng-show="sortKey=='id'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
</th>
<th ng-click="sort('first_name')">First Name
<span class="glyphicon sort-icon" ng-show="sortKey=='first_name'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
</th>
<th ng-click="sort('last_name')">Last Name
<span class="glyphicon sort-icon" ng-show="sortKey=='last_name'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
</th>
<th ng-click="sort('hobby')">Hobby
<span class="glyphicon sort-icon" ng-show="sortKey=='hobby'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
</th>
</tr>
</thead>
<tbody>
<tr dir-paginate="user in users|orderBy:sortKey:reverse|filter:search|itemsPerPage:5">
<td>{{user.id}}</td>
<td>{{user.first_name}}</td>
<td>{{user.last_name}}</td>
<td>{{user.hobby}}</td>
</tr>
</tbody>
</table>
<dir-pagination-controls
max-size="5"
direction-links="true"
boundary-links="true" >
</dir-pagination-controls>
</div>
</div>
</div>
</div>
<script th:src="#{/js/angular/angular.js}"></script>
<script th:src="#{/js/dirPagination.js}"></script>
<script th:src="#{/js/app.js}"></script>
</body>
this is a snippet of my controller.java
...
#RequestMapping(value = "/getSubTasks")
public ModelAndView getSubTasks(#RequestParam String issueKey) {
ModelAndView model = new ModelAndView();
.....
model.setViewName("view");
model.addObject("lstUsers", getUsers());
}
return model;
}
...
Thanks for your answer
Usually this kind of data is retrieved from the server via ajax. For example you can provide a Spring MVC Controller that returns your sub tasks as a JSON array and your angular app would need to retrieve that data from your Spring MVC Controller via ajax.
Have a look at this tutorial: https://spring.io/guides/gs/consuming-rest-angularjs/

Resources