0

I currently have a table listing jobs. Each job has a checkbox.

<form name="jobsSampleSelectForm" id="jobs-sample-select-form">
    <div  jobs="data">
      <div ng-form="jobsSampleSelectForm">
        <table ng-table="tableParams" class="table" show-filter="true">
            <tr ng-repeat="job in $data" class="table-highlight" ng-class="{deactivated: job.is_active == 'No'}">
                <td data-title="'Sample'"><input type="checkbox" ng-model="job.selected"></td>
                <td data-title="'QC ID'" sortable="'id'"><a href="#/jobs/{{job.id}}">{{job.id}}</a></td>
                <td data-title="'Date Submitted'" sortable="'dateJob'">{{job.dateJob}}</td>
            </tr>
        </table>
      </div></div>
    <button type="submit" class="btn btn-primary-red" ng-click="sampleJobs()" ng-disabled="(jobs | filter: job.selected != true).length <= 0">Sample Jobs</button>
</form>

This allows the user to select the jobs they want. However, when the user sorts a column the checkbox will uncheck itself. This seems to be a common issue, so from the advice from this SO question I added a selected attribute to each job in my controller.

jobsService.getJobsByStatuses(["pre_sampling", "sampling", "setup"])
    .success(function(data){
        data.forEach(function(job){ job.selected = false; });
        $scope.jobs = data;
        var filteredData = params.filter() ? $filter('filter')(data, params.filter()) : data;
        var orderedData = params.sorting() ? $filter('orderBy')(filteredData, params.orderBy()) : data;

        $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
        params.total(orderedData.length);
})

This did not seem to cause a difference. What am I missing?


I created a Fiddle to show the basic idea. When the user selects checkboxes, then sorts, the checkboxes become unchecked.

4
  • Can you add a JS Fiddle or Plunker link? Commented Aug 21, 2017 at 14:26
  • @NobalMohan I can try! Commented Aug 21, 2017 at 14:27
  • @NobalMohan Added a fiddle! Commented Aug 21, 2017 at 17:41
  • Thanks, Sara. The fiddle helped to solve the problem. Happy coding! Commented Aug 21, 2017 at 18:07

3 Answers 3

1

just change
<td data-title="'Sample'"><input type="checkbox" ng-model="job.selected"></td>
to
<td data-title="'Sample'"><input type="checkbox" ng-model="user.selected"></td>

If you want to persist the row selection then you should bind the checkbox to user.selected instead of binding to job.selected.

If you use user.selected for ng-model each row will have its own user object with selected property binded to checkbox so that you will get the selection even after the sorting

see the updated fiddle

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

4 Comments

Sorry, I kind of merged my code with an existing fiddle, so in my code, as you can see in the snippet, I already have ng-model="job.selected". Let me see if I can update the Fiddle
no change ng-model="job.selected" to ng-model="user.selected"
My actual code is in the code snippet above. I do not user user, I use job
I understand that. But if you look in my code in the question (not in the Fiddle) I never use user. user would be undefined
0
<input type="checkbox" ng-model="user.selected">

Working fiddle http://jsfiddle.net/PQvQ2/83/

You are not capturing the checkbox value anywhere. In the user object, you already have "selected" to capture the selection. Using the same will store the selection. If you are using a different object "job", you will capture the index of selection but it will not be mapped to the correct value. Since, "job" object only store the checkbox selected, nothing more.

1 Comment

Sorry, I kind of merged my code with an existing fiddle, so in my code, as you can see in the snippet, I already have ng-model="job.selected". Let me see if I can update the Fiddle
0

The issue was that I was calling getData every time a filter was applied. This was setting my selected attribute on each job to false. To fix this issue, I moved my code to get the data outside of the getData function.

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.