2

I am working on a project with AngularJS and facing an issue (which seems to be known but I did not understand or succeed to apply the solutions) :

I tried to use fresh table (http://demos.creative-tim.com/fresh-bootstrap-table) with ng-view. When I prepare a template without angular it is all working fine. But when I try to insert this table in angular in an ng-view it's not working anymore.

I have figured out that has to do with the javascript code they use after the balise :

<script type="text/javascript">
        var $table = $('#fresh-table'),
            $alertBtn = $('#alertBtn'),
            full_screen = false;

        $().ready(function(){
            $table.bootstrapTable({
                toolbar: ".toolbar",

                showRefresh: true,
                search: true,
                showToggle: true,
                showColumns: true,
                pagination: true,
                striped: true,
                sortable: true,
                pageSize: 8,
                pageList: [8,10,25,50,100],

                formatShowingRows: function(pageFrom, pageTo, totalRows){
                    //do nothing here, we don't want to show the text "showing x of y from..." 
                },
                formatRecordsPerPage: function(pageNumber){
                    return pageNumber + " rows visible";
                },
                icons: {
                    refresh: 'fa fa-refresh',
                    toggle: 'fa fa-th-list',
                    columns: 'fa fa-columns',
                    detailOpen: 'fa fa-plus-circle',
                    detailClose: 'fa fa-minus-circle'
                }
            });
        });

        $(function () {
            $alertBtn.click(function () {
                alert("You pressed on Alert");
            });
        });


        function operateFormatter(value, row, index) {
            return [
                '<a rel="tooltip" title="Like" class="table-action like" href="javascript:void(0)" title="Like">',
                    '<i class="fa fa-heart"></i>',
                '</a>',
                '<a rel="tooltip" title="Edit" class="table-action edit" href="javascript:void(0)" title="Edit">',
                    '<i class="fa fa-edit"></i>',
                '</a>',
                '<a rel="tooltip" title="Remove" class="table-action remove" href="javascript:void(0)" title="Remove">',
                    '<i class="fa fa-remove"></i>',
                '</a>'
            ].join('');
        }

        window.operateEvents = {
            'click .like': function (e, value, row, index) {
                alert('You click like icon, row: ' + JSON.stringify(row));
                console.log(value, row, index);
            },
            'click .edit': function (e, value, row, index) {
                console.log(value, row, index);    
            },
            'click .remove': function (e, value, row, index) {
                alert('You click remove icon, row: ' + JSON.stringify(row));
                console.log(value, row, index);
            }
        };
</script>

I have read it is difficult to execute custom javascript functions with Angular. I don't know how to do this...

Here is my index.html file with the ng-view where I am trying to insert the table :

<html lang="en" ng-app="MyApp">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>SaaS Manager</title>
    <meta name="Main" content="Dashboard">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
    <link href="../resources/styles/fresh-bootstrap-table.css" rel="stylesheet" />
    <link rel="stylesheet" href="../resources/styles/style.css">
    <link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
    <link href='http://fonts.googleapis.com/css?family=Roboto:400,700,300' rel='stylesheet' type='text/css'>
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.2.0.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.9.1/bootstrap-table.js"></script>
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="../bower_components/angular/angular.js"></script>
    <script src="../bower_components/angular-ui-router/release/angular-ui-router.js"></script>  
    <script src="../bower_components/angular-resource/angular-resource.js"></script>
    <script src="app.js"></script>
    <script src="controllers/dashboardController.js"></script>
    <script src="controllers/configurationDetailsController.js"></script>
    <script src="services/myService.js"></script>
  </head>
  <body>
    <div class="container-fluid">
      <div class="row banner">
        <div class="col-md-4">
          <img src="../resources/images/logo.png">
        </div>
        <div class="col-md-5">
          <h1>My title</h1>  
        </div> 
      </div>
      <div ui-view></div>
    </div>
  </body>
</html>

Here is the content of the view file :

<div class="container-fluid">
    <section class="dashboard">
        <div ng-controller ="dashboardCtrl as dashboard">
            <div class="fresh-table toolbar-color-orange">
            <div class="toolbar">
                <h2>Liste des services disponibles</h2>
            </div>
            <table id="fresh-table" class="table">
              <thead>
                <th data-field="Nom" data-sortable="true">Nom</th>
              </thead>
              <tbody>
                  <tr ng-repeat="configuration in configurations">
                    <td>{{configuration.service.name}} </td>
                  </tr>
              </tbody>
            </table>
        </div>
    </section>
</div>

The question is where should I put the javascript function ?

Thanks for your help guys !

user2164950
  • 101
  • 1
  • 1
  • 5
  • you can't mix jQuery style `$().ready` with angular templates, because the templates aren't loaded when `$().ready` is executed, they are loaded when the template is navigated to. In order to recommend a solution, it would be necessary to see the code for the angular controllers and router. – Claies Feb 07 '16 at 00:37

1 Answers1

0

I can't see where you implement ng-view in your HTML markup, and as far as where do you add in your javascript function, that would be in the controller associated with the view file for which you wrote the function.

Sara Inés Calderón
  • 1,070
  • 12
  • 22