Check record

Check record

lauromnetolauromneto Posts: 129Questions: 0Answers: 0

Good afternoon.
I have a datatable that loads name and code and a button.
If in the name and code field there is a DB value, if it does not exist, I set a code 0 and name "Available"
I want to disable the button when there is a record and leave the button enabled when it does not exist, that is, code = 0 and name = 'Available'. But there is a question about it ... I have my name and my session code that I'm logged in and I need to do this check, that is:
If there is a line in the table with my code and my name, I leave the button enabled and disable all others (the other existing codes and those that do not yet have codes).
How to do this in datatable?

Replies

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    You could use columns.render to display the HTMl for the row and add the disabled attribute. Or you can use createdRow or rowCallback (if the data can change). If you still need help please build a simple test case showing what you have and how you are creating the buttons. Then we can provide more specific help.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • lauromnetolauromneto Posts: 129Questions: 0Answers: 0

    So Kevin.
    It is difficult for me to create a new one, because I need to have the connection with BD, to do the process correctly ...
    The fact is ... I need to know if there is a record before closing the datatable, I see no other way. If so, when I load the page I do the query and return if there is or not stop there when loading the datatable I do these actions.

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    Sorry, I don't understand what you are doing.

    I want to disable the button when there is a record and leave the button enabled when it does not exist, that is, code = 0 and name = 'Available'.

    Is the button, the code and the name all in one row?

    If you want to disable a button, that is in the row, based on other data in the row then use one of the options I provided above.

    Kevin

  • lauromnetolauromneto Posts: 129Questions: 0Answers: 0

    Come on.
    I am connected with my user, where I receive the user name and code per session, for example:
    My code is 1 and my name is Lau.
    After that, I have a datatable with code, name, button. In this datatable I carry 10 lines.
    In the query of the BD that returns the data to the datatable, I return my code 1 and my name Lau (if I am registered in this table), I bring code 2 and name Kevin, for example and the other 8 lines, I load it in white .
    What I want is ... If my code = session code exists in the datatable, it enables the button and disables all other loaded buttons.
    If in the datatable there is no code = my session code, it will bring all enabled buttons.

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    edited October 2020

    Where are the buttons? In the rows, are you using the buttons extension or your own buttons?

    If my code = session code exists in the datatable,

    You can use the filter() API for this. Maybe in initComplete.

    Seems like you could use Javascript variables to create a test environment with the session name/code and some example rows and the buttons. Then use data instead of ajax to load the data.

    Kevin

  • lauromnetolauromneto Posts: 129Questions: 0Answers: 0

    I'll try to create a perfect example here and send it.
    Thanks Kevin

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    edited October 2020

    Not sure it has to be a perfect test case :smile:

    Based on my understanding (which may be way off) of what you want I started this test case. Maybe it will help you provide a more complete test case we can help with or maybe it will be enough for you to build your solution.
    http://live.datatables.net/jajifugo/1/edit

    Kevin

  • lauromnetolauromneto Posts: 129Questions: 0Answers: 0

    Good afternoon Kevin.
    I made an exact example of the case. See the link.
    The code and name of the session I associated the javascript variable as suggested, so here we go.
    The example is:
    The logged in user (code session and name session) can be listed in the datatable.
    If the datatable loads the code and name of the code session and name session, it leaves only the button related to its line enabled and the other buttons disabled.
    If there is no code session and name session in the datatable and all are "AVAILABLE", leave all buttons enabled. If there is no code session and name session in the datatable, but there are other code sessions and other name sessions, leave only the buttons where the lines are "AVAILABLE" enabled.

    https://lmneto.com.br/example/

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    Sounds like you might want to use the example code I provide in rowCallback to first get the count that matches your condition. Based on the match you can enable to disable the button as desired. If you still need help then I suggest using something like http://live.datatables.net/ so we can work on the code with you.

    Kevin

  • lauromnetolauromneto Posts: 129Questions: 0Answers: 0

    Good afternoon Kevin.
    I had already done your example in much the same way.
    Kevin, to be direct, look at this:
    If the code for my session code exists in the datatable, it only enables the button on this line and disables all others.
    If in the datatable there is no code for my session code, it enables all others, as long as they have the name AVAILABLE.
    Understood !? That's it!

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    I had already done your example in much the same way.

    Does that mean your code is working?

    Kevin

  • lauromnetolauromneto Posts: 129Questions: 0Answers: 0

    It doesn't work Kevin .... I don't think you understand.
    His example, he disables or enables everything! That's not what I want!
    If the datatable has the same code as the session code, it disables all buttons and leaves only the button for the existing code enabled.
    If there is no code in the datatable equal to the session code, it enables all buttons.
    If the datatable does not have the same session code and there are other codes and names other than AVAILABLE, it disables all buttons that are different from AVAILABLE and enables only those that contain the name field "AVAILABLE".

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    Can you provide a test case sowing this please? Its difficult to help troubleshoot a problem when there isn't code to look at.

    Kevin

  • lauromnetolauromneto Posts: 129Questions: 0Answers: 0

    Follow the link:
    http://live.datatables.net/gosehoto/1/edit

    Note .... Code Session 33 is Lau.
    It exists in the datatable too. Thus, only the button related to this line should be enabled.
    If a code session 33 does not exist in the datatable, enable the buttons only if they have the name "AVAILABLE", Kevin must be disabled.

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    edited October 2020

    Thanks!

    Code Session 33 is Lau.
    It exists in the datatable too. Thus, only the button related to this line should be enabled.

    I provided an example of one way to do this using filter(), as I show above, in the rowCallback. Here is the example:
    http://live.datatables.net/jajifugo/1/edit

    If a code session 33 does not exist in the datatable, enable the buttons only if they have the name "AVAILABLE", Kevin must be disabled.

    An else if needs to be added for this condition. I'll let you try coding this part.

    Kevin

  • lauromnetolauromneto Posts: 129Questions: 0Answers: 0

    You are saying rowCallback but you are using initComplete

    I did not understand ??????

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    Sorry, that was my original example. Check this out:
    http://live.datatables.net/coguxuve/1/edit

    Kevin

  • lauromnetolauromneto Posts: 129Questions: 0Answers: 0

    Kevin, eu acredito que seja isso que falta:

    if (filteredDataCount == 0 && data[2] !== name_session && data[2] == 'AVAILABLE') {
    $(row).find('button').attr('disabled', false);
    }
    
  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    Not sure if that means its working or not. If not please update the test case so we can help.

    Kevin

  • lauromnetolauromneto Posts: 129Questions: 0Answers: 0
  • lauromnetolauromneto Posts: 129Questions: 0Answers: 0

    Thank you very much Kevin for your help and willingness.
    God bless !

This discussion has been closed.