0

I have tried a code about auto generating id. I want to try it on JTextField, but I don't know where i should put it.

Here is my code:

--PembelianController.groovy--

String generateID() {
        String date = DateTime.now().toString("yyyyMMdd")
        List list = findAllPembelian([orderBy: 'noNota', orderDirection: 'desc'])
        Integer num = list.size()==0? 0: list[0].kode[12..-1].toInteger() + 1
        return String.format("NT00%s%04d", date, num)
}

--PembelianView.groovy--

label('No Nota:')
textField(id: 'noNota', columns: 20, text: bind('noNota', target: model, mutual: true), errorPath: 'noNota')
errorLabel(path: 'noNota', constraints: 'wrap')
hendrakmg
  • 37
  • 7

1 Answers1

1

Well, it depends on when you want to do it. You can assign textField.text = generateID() at any time. You may assign the value during the binding

textField(id: 'noNota', columns: 20, errorPath: 'noNota',
      text: bind('noNota', target: model, mutual: true, value: generateID()))
Andres Almiray
  • 3,236
  • 18
  • 28
  • I see, it's working perfectly. But it only appear for one time. When I do then next input it show me nothing. How do I put it, so I can add it there every time and can't edit the textfield? – hendrakmg Apr 07 '13 at 01:49
  • By the way, if you're using simple-jpa plugin, by default, it will only inject finders to controller. Assuming your code is located in view, you should change `findAllPembelian()` to `controller.findAllPembelian()`. – jocki Apr 07 '13 at 06:00
  • Ah, i put the function on controller (above). I just edit it. – hendrakmg Apr 08 '13 at 10:53