1

Having some trouble passing an object through ng-click on a data-formatter button, data-formatter is part of bootstrap-table functionality.

This is what I have on the html

<table  class="container" 
        data-pagination="true"
        data-page-list="[5, 10, 20, 50, 100, 200]"
        data-search="true"
        data-toggle="table"
        data-show-refresh="true"
        data-show-columns="true"
        data-toolbar="#customToolbar"
        id="currentTable">
   <thead>
     <tr>
        <th data-field="bundle" data-sortable="true">Name</th>
        <th data-field="status" data-sortable="true" data-cell-style="cellStyle">Status</th>
        <th data-field="createdTime" data-sortable="true" data-formatter="dateFormatter">Date Created</th>
        <th data-formatter="editButton"></th>
     </tr>
   </thead>
 </table>

<script type="text/javascript">
  function dateFormatter(value){
    var date = new Date(value);
    var formattedDate = date.getFullYear() + '-' + (date.getMonth() + 1 )+ '-' + date.getDate();
    return formattedDate;
  }

 function editButton(value, row){
   var currentRow = row;
   console.log(currentRow);
   var append = 'angular.element(this).scope().editName('+currentRow+')';
   return '<button class="editButton" onclick="'+append+'"><i class="fa fa-pencil fa-2x" aria-hidden="true"></i></button>';

 }
</script>

controller-js - it's very simple

$scope.editName = function(row){
        console.log(row);
    }

so user will click on the edit button, once it's clicked it should pass the row object through ng-click, the editBundle function should capture in the controller, but I'm a getting an error -> Uncaught SyntaxError: Unexpected identifier

1 (function(event){angular.element(this).scope().editName([object Object])
2 })

but if I add double quotes before and after row, editName("'+currentRow+'"), then i get a different error -> Uncaught SyntaxError: Unexpected token }

 1 (function(event){angular.element(this).scope().editName(
 2 })

Trying different things here but no result, has anyone encountered this type of problem? I know it's some syntax issue, but can't get it to work.

The reason i'm using data formatter from bootstrap table is because this table seems to load the names pretty fast from an api call than other table formats. I tried to use smart-table.js but it's very slow when loading or when I try to sort, the data won't show up. I'm not very familiar w/ smart-table.js.

medev21
  • 2,469
  • 8
  • 31
  • 43

2 Answers2

0

I believe the reason you are getting this error is because you are commenting out the quotation mark 'angular.element(this).scope().editName('+currentRow+')' and you added 'angular.element(this).scope().editName("'+currentRow+'")' making all the values in edit name in quotation and not braking out of the brackets try escaping the values or putting the double quotes inside the single quotes. Look at Escape quotes in JavaScript .

Aaron Rabinowitz
  • 357
  • 2
  • 18
0

You can use the code below:

'angular.element(this).scope().editName(' + JSON.stringify(row).replace(/"/g, '&quot;') + ')';

You will get the object in $scope.editName function.

Nick
  • 4,820
  • 18
  • 31
  • 47