Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying assign data attribute to th element on click so that I could sort the table. But it's not working


Dom.

HTML
<thead>
    <tr>
        <th class="sort-odrdtl-tbl" data-column="0" data-sort-dir="asc">OrderId</th>
        <th class="sort-odrdtl-tbl" data-column="1" data-sort-dir="asc">Descriptions</th>
        <th class="sort-odrdtl-tbl" data-column="2" data-sort-dir="asc">Quatity</th>
        <th class="sort-odrdtl-tbl" data-column="4" data-sort-dir="asc">Requested Date</th>
        <th class="sort-odrdtl-tbl" data-column="5" data-sort-dir="asc">Reason</th>
    </tr>
</thead>


What I have tried:

$(document).ready(function () {


       // initialization

       $('#loadingWait').hide();

       var token = $('[name=__RequestVerificationToken]').val();



       $('.sort-odrdtl-tbl').on('click', function () {

          $(this).data('sort-dir', 'desc');
           //$(this).first().data('sort-dir', 'desc');


           debugger;

       })

   });
Posted
Updated 20-Mar-18 6:39am
v3

1 solution

When you access a data attribute through code, the "kebab-case" name is converted to "camelCase". So you need to use:
JavaScript
$(this).data('sortDir', 'desc');

Using data attributes - Learn web development | MDN[^]
 
Share this answer
 
Comments
istudent 20-Mar-18 14:23pm    
var dirVal = $(this).data('sort');



if (dirVal === 'asc') {

$(this).data('sort', 'desc');
}
else {
$(this).data('sort', 'asc');
}

console.log($(this).data('sort'));

I can see in value desc in console log, but it did not update th element.
for simplicity i changed data-sort-dir to data-sort
Richard Deeming 20-Mar-18 15:24pm    
The data method only updates the element's dataset; it doesn't change the attribute on the element.

If you want to change the attribute as well, you'll need to use the attr method:
var $me = $(this);
if ($me.data("sort") === "asc"){
    $me.data("sort", "desc").attr("data-sort", "desc");
}
else {
    $me.data("sort", "asc").attr("data-sort", "asc");
}
istudent 21-Mar-18 10:48am    
it did not updated
<th class="sort-odrdtl-tbl" data-column="0" data-sort="asc">OrderId</th>

to

<th class="sort-odrdtl-tbl" data-column="0" data-sort="desc">OrderId</th>


I hope you know what I am trying to achieve here. I want add sort functionality on table head. and preserve its value for subsequent request.
Richard Deeming 21-Mar-18 15:05pm    
If $me.data("sort", "desc").attr("data-sort", "desc"); doesn't change the attribute in the browser's developer tools, then you're not updating the correct element.

NB: Make sure you're looking at the element in the developer tools, and not the "view source"; the tools reflect the current state of the DOM, whereas "view source" shows you the source as returned by the server.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900