Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remember jQuery Tablesorter drop-down selection using jQuery Cookie?

I'm using two jQuery Tablesorter drop-downs to filter results of a dynamically-generated table. When a name in either drop-down is selected, only results containing that name will show.

What I want is for jQuery Cookie to remember the last selected filter option for each drop-down before the page was reloaded. If the name "Alaina " (seen in the attached picture) was the selected option for the "Assigned to" drop-down before the user left the page, I want "Alaina" to be the default option when the user goes back to the page.

Currently when the page loads, the default "Filter" selection is loaded, and thus no results are filtered.

The output of the drop-down for the "Assigned to" column (the drop-down for the "Expeditor" column has the same structure, only its id is msdrpdd20 instead of msdrpdd21):

<td>
  <div id="msdrpdd21_msddHolder" class="ddOutOfVision">
    <select id="msdrpdd21" class="tablesorter-filter" data-column="3" tabindex="-1">
      <option value=""> … </option>
      <option value="(none)"> … </option>
      <option value="Alaina"> … </option>
      <option value="Alyssa"> … </option>
      <option value="Felicita"> … </option>
      <option value="Luciana"> … </option>
      <option value="Rachel"> … </option>
    </select>
  </div>
  <div id="msdrpdd21_msdd" class="dd ddcommon borderRadius" tabindex="0">
    <div class="ddTitle borderRadiusTp">
      <span id="msdrpdd21_title" class="ddTitleText ">
        <span class="ddlabel">
          (none)
        </span>
        <span class="description" style="display: none;"></span>
      </span>
    </div>
    <input id="msdrpdd21_titleText" class="text shadow borderRadius" type="text" autocomplete="off" style="display: none;"></input>
    <div id="msdrpdd21_child" class="ddChild ddchild_ border shadow" style="display: none;">
      <ul>
        <li class="enabled _msddli_"> … </li>
        <li class="enabled _msddli_ selected"> … </li>
        <li class="enabled _msddli_"> … </li>
        <li class="enabled _msddli_"> … </li>
        <li class="enabled _msddli_"> … </li>
        <li class="enabled _msddli_"> … </li>
        <li class="enabled _msddli_"> … </li>
      </ul>
    </div>
  </div>
</td>

enter image description here

EDIT (07/29/13): Screenshot of Nev's cookie:

enter image description here

EDIT (08/01/13): Screenshot of Nev's revision:

enter image description here

Filtering of results (via Tablesorter Filter):

$( '#todo-list' ).tablesorter( {
    widgets: ["filter"],
    widgetOptions : {filter_reset : '.reset'}
} );
like image 993
eclipsis Avatar asked Apr 22 '26 04:04

eclipsis


1 Answers

Using JQuery Cookie:

$(document).ready(function (e) {
    data = $("#payments").msDropdown().data('dd');
    if ($.cookie("table-sorter") !== null) {
        var toselect = $.cookie("table-sorter");
        data.set('selectedIndex', toselect);
    }

    $('#payments').change(function () {
        var selected = this.selectedIndex;
        // save value to cookie
        $.cookie("table-sorter", selected, { path: '/' });
    });


});

Here is the Fiddle (works in Firefox but not IE). The code however works if you upload it to your own server (tested in IE).

like image 176
Nev Avatar answered Apr 24 '26 21:04

Nev