How to save id field in string?

I am writing a website with jquery and a lot of ajax query to get data for a table and request data changes using PHP / MySql on the server side.

I am currently using the id attribute to store the table field identifier (which is an autoincrement int value).
And it works great.

BUT I recently found out that the identifier must be unique (and start with a letter ...).
And I have different tables that can have the same id value (for different sql tables) Then I am not html (nor xhtml) compatible ...

How can I fix my code?

  • Using the .data() jQuery function?
  • Hidden html element with id as value ( <span class="id">3</span> )?
  • Another solution?

Additional Information:
I wrote a widget to manage my tables.
To add a new row, follow these steps:

  row = $('<div class="row" id="'+item.id+'"/>'); [...] // I add fields to my row row.appendTo(tableData);// tableData is the html element where rows are 

When a field element changes, I fire an event in a table that will request a modification of the server with the correct id:

 $(e.target).closest(".row").attr("id") 
+4
source share
5 answers

If you can use jQuery 1.4.3 or more, use the html 5 data-* attributes. jQuery 1.4.3 will automatically use these data attributes and put them in the .data() collection for the element.

Example:

 <table> <tr data-rowId="1"> </tr> </table> 

$("tr:first").data("rowId") will print 1

This method will also allow you to store json objects.

 <table> <tr data-row='{"Id" : 1, "Name": "Smith"}'> </tr> </table> 

And than in your data() var row = $("tr:first").data("row") you can reference row.Id and row.Name

+7
source

You can attach your identifier to the table name:

 <div id="mytable_1234"></div> 

It is easy to extract the table name and identifier from the field, and this is HTML compliant.

 var values = $(element).attr('id').split('_'); // values[0] is the table name and values[1] is the id. 

You can use any other delimiter if you are already using underscores in table names.

+4
source

you can also use jQuery metadata .....

Its an amazing way to store data in html

+1
source

Instead of using id use data-id and use .data('id') (for this element) to retrieve it using jQuery.

+1
source

I think ... I understand your question - several data tables, all with auto-increment identifiers?

My solution would be to pre-add an identifier with a letter (as you said) to distinguish it.

An example is a dataset for "cars" that I would do:

 <table> <tr id="cars_1"> ... </tr> <tr id="cars_2"> ... </tr> <tr id="cars_3"> ... </tr> </table> 

Later, if you have another table, bikes, you would do:

 <table> <tr id="bike_1"> ... </tr> <tr id="bike_2"> ... </tr> <tr id="bike_3"> ... </tr> </table> 

The end result will be a unique identifier, given the value of the db id, so you would do a simple check (if cars, then do it, etc.), then split the prefix (cars from id 1, you will use something like PHP expolode() fn).

Hope this clarifies, and that I understand your question.

0
source

Source: https://habr.com/ru/post/1343925/


All Articles