Toolbar dropdown not working. #7116
-
All, I am trying to implement a simple toolbar dropdown button (splitbutton) in a grid in order to run some code based on user selection. The source for the dropdown should be a lookup table, but I can't seem to get the dropdown to work correctly. Using the following code (from a wiki example) I can get the button to show, but it will not expand/drop down, in order for the user to make a selection. Can anyone help me with what is wrong? Possibly also suggest how to set the source of the dropdown to all the records from a lookup table? In the example below I am just trying to get the button to work, but will eventually also need to set the dropdown source to a table instead of hardcoded values. Serenity 6.96. I had to remove (from the wiki example) options?: TOptions from the constructor as it was not valid.
site.css // Classes for PDF button |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 7 replies
-
This will not be what you want to hear. When I tried out your code as-is, attaching it to the UserDialog in a template-generated application using 6.9-something, there were some very odd results; I saw the button appear in both the grid and the form, which you may not have noticed if your dialog is a panel, and also it appeared in multiple places in the dialog, and multiple times in the grid; so I think your condition for where it should go ( I don't know if this accounts for your problem, though. Note: the click event did work, but only for the leftmost button in the grid, and not at all for the dialog buttons that got added. I saw three "Test" alerts, indicating that your binding is working but there is something wrong with your nested elements. Have you inspected the HTML node? On one of the buttons I saw multiple Print and File Another thing I notice, as you'll see in the screenshot, is that you have a PDF button and a dropdown element that don't look like they are the same element. Again, looking at the generated HTML should make this more obvious to you. At any rate it doesn't look like the example in https://github.com/serenity-is/Serenity/wiki/Create-toolbar-dropdown-button. So, I followed that example from scratch, using UserGrid, and I had the same experience as you did, although it looked better. So I think that this example, which is from 2017, just doesn't work in more modern Serenity. The UI SplitButtons dropdowns, which you may have used instead, https://github.com/serenity-is/Serenity/wiki/UI-SplitButton-dropdowns, is from 2021 so maybe it just doesn't work now either. Also I notice that this code, n both places, was designed to work with a grid, not a dialog, which probably accounts for what I see in my screenshot somehow. Maybe somebody else can chime in and say where there is a more modern example available. If I needed to do this, I'd probably just start from scratch. Or, if you know your lookup table is only going to have a few rows, I guess I would iterate through them and create separate buttons for each of the possible actions. Is this possible for you? |
Beta Was this translation helpful? Give feedback.
-
Here are some followup thoughts. I was able to get rid of the duplicates and the button showing up in the wrong places by simply creating a div with a class and name that I knew would be unique, like this:
... however this didn't resolve the problem with the dropdown list items not showing up. I think there was something wrong with the hover code in the CSS but I couldn't see what. So, as I suggested, I went back to first principles and created an example that does work, from scratch, starting from this example: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_dropdown_navbar, which I could test and prove worked, and then adapt slightly for inclusion in Serenity dialog code. The working code below is independent of the example div created above. I ran this code in updateInterface(), not in the dialog constructor, but I didn't have any particular reason to put it there, to be honest. It would probably work in the constructor as well.
It needs to be paired with the following CSS (not exactly the same as what you'll see in the W3Schools example, but very close):
The result looks something like this -- obviously the CSS could use more work -- ... and when you hover over the new button you get this, with the click event for each list item doing what you would expect on each: ... so at this point it is just a question of building up the dropdownPDF-content string to suit your needs, and as long as your dialog has access to the rows in your lookup table I think this should be possible as well. As I mentioned before, if it is just a few items I don't see any reason why you would not create separate buttons for each item, though, rather than fuss with this. Hope this helps, at any rate. |
Beta Was this translation helpful? Give feedback.
-
Oh, in a grid is pretty much just the same although I changed to (a) run the code in createToolbarExtensions() and (b) use spans instead of divs, both changes to get the button where I wanted it on the grid page. I didn't make any changes to the CSS from the earlier example:
From there, as I said earlier, I don't think it's difficult to data drive/dynamically build. Here is a sample, in which I'm using the Roles table for my source. Your table would have the elements, such as icon filenames and actions to take, that you need., and you might want to build a customized lookup that builds the full string for each item, ready for re-use if you did decide to put the button in a form too: Caveats: 1, you should test to make sure that you actually get items back, in case there are none configured, and, 2, I know we aren't supposed to use synchronous call but I couldn't figure out how to get it to work properly with Async, at least not quickly, sigh.
|
Beta Was this translation helpful? Give feedback.
-
Hi again, No worries about the late reply. And I see that you have figured out the first question you originally asked about how to show something different in the dropdown :-). Really, I was just putting anything into the sample code as an example, because it should be easy to change and I don't even really know what you need there. But, unfortunately, the remaining question you have left is something I may not be able to help with. If you remember, I told you that I hunted down an example of dropdown behavior that worked properly, and adapted it, because I didn't understand what was wrong with the CSS "hover" code you were trying to adapt. I wanted to start from something that would work. I'm not an expert in this stuff, and it is the CSS that is making the dropdown appear. So, at this point, there are two choices:
Caveats:
|
Beta Was this translation helpful? Give feedback.
-
Hi again, I think it is a question of context. Again, I am not an expert on this, but for a test I did this and it did work -- if I used window.test or test with no prefix, but it did not work with "this.test":
... So I think that "this" is not what you think it is at the point of the call. I am NOT suggesting that you do exactly what I did in this test, however. There must be a better way to get that script attached to the Serenity page, maybe you could research that further. If not, I think you would be better off defining the function in-line as part of the retval string. Alternatively, just fashion the endpoint that you want to call as a URL in an href as part of retval instead of onclick, maybe? What is the action that you are really trying to do at this point? |
Beta Was this translation helpful? Give feedback.
-
In UserGrid.ts, the getColumns method has this:
... and it seems to me this is how you would fashion your href that calls your endpoint, as I suggested before. You would add the date and the id into the string as you see here with token. This would be the simplest method. I do understand that this will not give you the notify... dialogs that your function has. There are some different choices for this, if that's important; you can use onclick, with my alternative suggestions; either: [ ] put all the code in-line or |
Beta Was this translation helpful? Give feedback.
Oh, in a grid is pretty much just the same although I changed to (a) run the code in createToolbarExtensions() and (b) use spans instead of divs, both changes to get the button where I wanted it on the grid page. I didn't make any changes to the CSS from the earlier example: