How to compare values between datastore and table row from the database

Hi,

I have setup a datastore ds_assign_students that is updated with student IDs when clicked on a table row. When a unique student ID is updated/insterted in the datastore, I want to tick the checkbox for that row in the table and set table-success class dynamically to highlight the selected rows.

I have tried contains formatter to compare student ID values between the row that's clicked and ID saved in the datastore. But this doesn't work. There's no inArray option available in the dynamic picker.

Any help in finding a solution to this issue is greatly appreciated. The code is as below:

<tbody is="dmx-repeat" dmx-generator="bs5table" dmx-bind:repeat="sc_list_students.data.q_list_students.data" id="tableRepeat2" key="id">
  <tr dmx-class:table-success="ds_assign_students.data.contains(id)" dmx-on:click="run({run:{outputType:'text',action:`ds_assign_students.upsert({ds_student_id: id},{ds_student_id: id, ds_student_name: full_name})`}})">
    <td dmx-text="full_name"></td>
    <td dmx-text="year_level"></td>
    <td dmx-text="class_name"></td>
    <td>
      <div class="form-check">
        <input class="form-check-input" type="checkbox" value="1" id="studentIdChecked" dmx-bind:id="'studentIdChecked'+$index" name="studentIdChecked" dmx-bind:checked="ds_assign_students.data.contains(id)">
        <label class="form-check-label" dmx-bind:for="'studentIdChecked'+$index"></label>
      </div>
    </td>
  </tr>
</tbody>

The contains formatter might not be working as expected because ds_assign_students.data is an array of objects, and contains(id) is likely checking for an exact match rather than searching within the objects.

A possible solution is to use the where formatter to filter the datastore and check if the student ID exists. Try modifying your checkbox binding and row class like this:

dmx-bind:checked="ds_assign_students.data.where('ds_student_id', id).count() > 0"
dmx-class:table-success="ds_assign_students.data.where('ds_student_id', id).count() > 0"

This will filter the datastore for entries where ds_student_id matches the row's id, and if the count is greater than zero, it means the student is selected.

2 Likes

Hi Ben,

Thank you for the solution - it worked perfectly! Your suggestion to use the where formatter was exactly what I needed to solve this issue.

Thanks again :slight_smile:

1 Like