Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using localStorage with filter controls and cookies. #7261

Open
merinosky opened this issue Apr 3, 2024 · 0 comments · May be fixed by #7351
Open

Error when using localStorage with filter controls and cookies. #7261

merinosky opened this issue Apr 3, 2024 · 0 comments · May be fixed by #7351
Labels
Bug Issues which are marked as Bug confirmed Issues that have been confirmed with a reduced test case and identify a bug. cookie Issues for the cookie extension. filter-control Issues for the filter-control extension. has PR Issues that has been fixed with a PR.

Comments

@merinosky
Copy link

merinosky commented Apr 3, 2024

Bootstraptable version(s) affected

1.22.4

Description

My table uses data-filter-control="true" and data-show-search-clear-button="true". I also use localStorage for saving my own cookies, nothing to do with bootstrap table. I have found that using the clear search button while localStorage contains cookies the following error is shown:

2024-04-03_12-58-28

As the error says, the problem is found in the following piece of code from Cookie extension:

deleteCookie: function(t, e) {
    return t._storage.removeItem("".concat(t.options.cookieIdTable, ".").concat(e))
}

because t._storage is undefined. Adding data-cookie="true" to the table makes the error go away because t._storage is not undefined, but this is not the point, since cookies should not be tried to be deleted if the table works without cookies.

I made some debugging to know why cookies are being deleted if my table does not use cookies, and found out that collectBootstrapTableFilterCookies function, from Filter Control extension, is returning the cookies stored in localStorage even when they have nothing to do with bootstrap table. Specifically the code that is causing so is:

if (foundLocalStorage) {
   for (var i = 0; i < foundLocalStorage.length; i++) {
      var cookie = foundLocalStorage.key(i);
      if (/./g.test(cookie)) {
         cookie = cookie.split('.').pop();
      }
      if (!cookies.includes(cookie)) {
         cookies.push(cookie);
      }
   }
}

The problem is that any cookie's name stored in localStorage is returned by this method and afterwards those cookies will be deleted.

Example(s)

I tried to reproduce it in the online editor but couldn't.

Possible Solutions

I think that changing the regular expression so it only matches with bootstrap-table cookies would solve the problem:

if (foundLocalStorage) {
   for (var i = 0; i < foundLocalStorage.length; i++) {
      var cookie = foundLocalStorage.key(i);
      if (/.bs\.table\../.test(cookie)) {
         cookie = cookie.split('.').pop();
      }
      if (!cookies.includes(cookie)) {
         cookies.push(cookie);
      }
   }
}

Additional Context

No response

@merinosky merinosky added the Bug Issues which are marked as Bug label Apr 3, 2024
@wenzhixin wenzhixin added cookie Issues for the cookie extension. filter-control Issues for the filter-control extension. labels Apr 24, 2024
@UtechtDustin UtechtDustin linked a pull request May 27, 2024 that will close this issue
10 tasks
@UtechtDustin UtechtDustin added has PR Issues that has been fixed with a PR. confirmed Issues that have been confirmed with a reduced test case and identify a bug. labels May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Issues which are marked as Bug confirmed Issues that have been confirmed with a reduced test case and identify a bug. cookie Issues for the cookie extension. filter-control Issues for the filter-control extension. has PR Issues that has been fixed with a PR.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants