Jump to content

Jquery Datatables add class or ID with data parameters.

venomtail

Delete this before posting. Please make sure to include the language that you're using in the title, and use the <> button for any code.

Creating a jQuery table. Want to add a class to the rows that are created but I want to use the data: portion of the request. In "columnDefs": [ adding {"data": "ID"} to "className": doesn't work as jQuery just places and object in there where instead I was hoping it will automaticall place in the correct data in sequence. I can easily place in other hardcoded ID's and classes but nothing dynamically that I assumed jQuery would do on it's on.

 

How can I add ID's or classes to the same rows that jQuery already places automatically via the data section?

Desktop: Ryzen 7 5800X3D - Kraken X62 Rev 2 - STRIX X470-I - 3600MHz 32GB Kingston Fury - 250GB 970 Evo boot - 2x 500GB 860 Evo - 1TB P3 - 4TB HDD - RX6800 - RMx 750 W 80+ Gold - Manta - Silent Wings Pro 4's enjoyer

SetupZowie XL2740 27.0" 240hz - Roccat Burt Pro Corsair K70 LUX browns - PC38X - Mackie CR5X's

Current build on PCPartPicker

 

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, venomtail said:

How can I add ID's or classes to the same rows that jQuery already places automatically via the data section?

Use the addClass method?

https://api.jquery.com/addClass/

 

Need to give code example with expected output to fully understand your question

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, C2dan88 said:

Use the addClass method?

https://api.jquery.com/addClass/

 

Need to give code example with expected output to fully understand your question

I'm not using jQuery to add a class to a field. I'm using jQuery's datatables extension to add a class when my interactable datatable is created. Column definition documentation as well as Class name documentation. Problem for me is that they all document hardcoded classes inside quotes of "Class" yet when defining colums, I can dynamically add in the json data received for each fields which is nice, however I can't seem to be able to replicate the same behaviour for column definition by which I want to also do for ID's. A datatable would look like this.

 

I either can't find documentation or non hard coded classes for datatables don't exist.

Desktop: Ryzen 7 5800X3D - Kraken X62 Rev 2 - STRIX X470-I - 3600MHz 32GB Kingston Fury - 250GB 970 Evo boot - 2x 500GB 860 Evo - 1TB P3 - 4TB HDD - RX6800 - RMx 750 W 80+ Gold - Manta - Silent Wings Pro 4's enjoyer

SetupZowie XL2740 27.0" 240hz - Roccat Burt Pro Corsair K70 LUX browns - PC38X - Mackie CR5X's

Current build on PCPartPicker

 

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

I could probably help you but your question is to vague, i dont understand what you are trying to do. Post example code to clarify it.

I have no signature

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Helly said:

I could probably help you but your question is to vague, i dont understand what you are trying to do. Post example code to clarify it.

I'm trying to input data from column data object such as the ID's and have the ID's that correspond to each row also appear as a class (or whatever value was in the data object, just using ID as example) using className. There are mentions of how to control a specific class to which fields but none mention of classes in direct relation to the data being displayed.

 

Typing this reply I found createdCell which I'll have to play around with. Might be even more flexible as to how I can manipulate a cell that just being restricted to a cells classes if it works the way I assume it does.

Desktop: Ryzen 7 5800X3D - Kraken X62 Rev 2 - STRIX X470-I - 3600MHz 32GB Kingston Fury - 250GB 970 Evo boot - 2x 500GB 860 Evo - 1TB P3 - 4TB HDD - RX6800 - RMx 750 W 80+ Gold - Manta - Silent Wings Pro 4's enjoyer

SetupZowie XL2740 27.0" 240hz - Roccat Burt Pro Corsair K70 LUX browns - PC38X - Mackie CR5X's

Current build on PCPartPicker

 

Link to comment
Share on other sites

Link to post
Share on other sites

creating a unique class for each row seems kind of pointless. More so because that data is probably constantly changing. My guess is that whatever the reason is for adding those classes can be done in a far better and efficient way then you are doing. So if you want to know a better way, explain what you are doing with the classes you are adding and we can probably help you do this in a more efficient way.

Also, adding actual code is like adding an image, it speaks a thousand words...

I have no signature

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Helly said:

creating a unique class for each row seems kind of pointless. More so because that data is probably constantly changing. My guess is that whatever the reason is for adding those classes can be done in a far better and efficient way then you are doing. So if you want to know a better way, explain what you are doing with the classes you are adding and we can probably help you do this in a more efficient way.

Also, adding actual code is like adding an image, it speaks a thousand words...

Settings unique classes for each separate row isn't my endgoal. Merely the most basic step from which I can understand how to manipulate data from object and set it to the cell itself along side the original data inserted into the cell/column and hen just go from there. If I know how to insert data as a class then setting up a custom cell parameter is the next logical step.

 

Two fold uses. More data per cell if I can add it as extra variables to the cell that doesn't have to be the object data itself that the user can see.

Other is just field manipulation. Lets take column_render for example. Knowing the data I can create cases or if statements and have the cell be rendered differently from one another, for example if cell has a "score" field, in it a value of 24, I can make that cell render lets say red cause score is bad compared to a different cell that would be green cause score is above 90. At times I just feel like this gets progressively more unwieldy the more variations there are. I feel like if I was simply able to add classes directly from the data I could rely on CSS more to colour the cells how I want them and keep the code bulk down.

Desktop: Ryzen 7 5800X3D - Kraken X62 Rev 2 - STRIX X470-I - 3600MHz 32GB Kingston Fury - 250GB 970 Evo boot - 2x 500GB 860 Evo - 1TB P3 - 4TB HDD - RX6800 - RMx 750 W 80+ Gold - Manta - Silent Wings Pro 4's enjoyer

SetupZowie XL2740 27.0" 240hz - Roccat Burt Pro Corsair K70 LUX browns - PC38X - Mackie CR5X's

Current build on PCPartPicker

 

Link to comment
Share on other sites

Link to post
Share on other sites

So you want to use conditional formatting and store formatting CSS classes in the dataset somehow?  That doesn't sound like such a good approach - you're not separating concerns adequately at that point.  The data model should know nothing about the view, or the view model for that matter.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×