How to call jquery´s $ function in dart

2019-08-10 03:37发布

问题:

I try to make the following jquery functioncall in dart

$('#mytable').handsontable({
      data: data,
      minSpareRows: 1,
      colHeaders: true,
      contextMenu: true
    });

my dartcode

import 'dart:js';

void main() 
{ var data = [["", "Maserati", "Mazda", "Mercedes", "Mini", "Mitsubishi"],
              ["2009", 0, 2941, 4303, 354, 5814]  ];

  context.callMethod('$', ['#mytable'])
  .callMethod('handsontable', [new JsObject.jsify(
  {'data': data,
   'minSpareRows': 1,
   'colHeaders': true,
   'contextMenu': true})]);  
}

The line:

  context.callMethod('$', ['#mytable'])

produces an error in the editor: Expected an identifier ('$' is marked red) Any other string then the '$' semes to be ok, but the jquery function in $()

回答1:

You get the error because $ is the character in Dart String to perform String interpolation. To make your code work you have to escape this character context.callMethod('\$', ['#mytable']) or use a raw String context.callMethod(r'$', ['#mytable']).



回答2:

thx @Intelekshual

context.callMethod('jQuery', ['#mytable'])
  .callMethod('handsontable', [new JsObject.jsify(
  {'data': data,
   'minSpareRows': 1,
   'colHeaders': true,
   'contextMenu': true})]);

works fine.



标签: jquery dart