Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you build nested options with JSOptionBuilder #13

Open
nh13 opened this issue Jun 30, 2017 · 3 comments
Open

How do you build nested options with JSOptionBuilder #13

nh13 opened this issue Jun 30, 2017 · 3 comments

Comments

@nh13
Copy link

nh13 commented Jun 30, 2017

For example the columns option in https://datatables.net/examples/api/row_details.html

@js.native
trait DataTableOptions extends js.Object
object DataTableOptions extends DataTableOptionsBuilder(noOpts)
class DataTableOptionsBuilder(val dict: OptMap)
  extends JSOptionBuilder[DataTableOptions, DataTableOptionsBuilder](new DataTableOptionsBuilder(_)) {
  def columns(obj: js.Array[js.Object]) = jsOpt("columns", obj)
}

@js.native
trait DataTableColumnOptions extends js.Object
object DataTableColumnOptions extends DataTableColumnOptionsBuilder(noOpts)
class DataTableColumnOptionsBuilder(val dict: OptMap)
extends JSOptionBuilder[DataTableColumnOptions, DataTableColumnOptionsBuilder](new DataTableColumnOptionsBuilder(_)) {
  def data(v: String) = jsOpt("data", v)
  def className(v: String) = jsOpt("className", v)
  def orderable(v: Boolean) = jsOpt("orderable", v)
  def defaultContent(v: String) = jsOpt("defaultContent", v)
}

but I don't know how to wire them together. Thanks for any help.

@jducoeur
Copy link
Owner

I don't understand -- why don't you just give the type of columns as js.Array[DataTableColumnOptions]?

@nh13
Copy link
Author

nh13 commented Jun 30, 2017

I didn't understand, thank-you.

@nh13 nh13 closed this as completed Jun 30, 2017
@nh13
Copy link
Author

nh13 commented Jul 1, 2017

It looks like I am missing something again, and thank-you in advance for all your help. If I can get this right I can make a facade for JQuery's DataTables, with all of its options! Below is my source, and also a screenshot of where the code crashes in datatables.js.

screen shot 2017-07-01 at 7 09 30 am

@js.native
trait JQueryDataTable extends js.Object {
  def rows(): JQueryDataTableRow = js.native
  def draw(): JQueryDataTableRow = js.native
}

@js.native
trait JQueryDataTableRow extends js.Object {
  def invalidate(): JQueryDataTable = js.native
}

@js.native
trait JQueryDataTableFacade extends js.Object {
  def DataTable(options: DataTableOptions): JQueryDataTable = js.native
}

object JQueryDataTableFacade {
  implicit def jq2DataTable(jq: JQuery): JQueryDataTableFacade = jq.asInstanceOf[JQueryDataTableFacade]
  implicit def element2dataTableElement(e: dom.Element): JQueryDataTableFacade = e.asInstanceOf[JQueryDataTableFacade]
}


@js.native
trait DataTableOptions extends js.Object
object DataTableOptions extends DataTableOptionsBuilder(noOpts)
class DataTableOptionsBuilder(val dict: OptMap)
  extends JSOptionBuilder[DataTableOptions, DataTableOptionsBuilder](new DataTableOptionsBuilder(_)) {
  def columns(options: js.Array[DataTableColumnOptionsBuilder]) = jsOpt("columns", options)
  def destroy(b: Boolean) = jsOpt("destroy", b)
}

@js.native
trait DataTableColumnOptions extends js.Object
object DataTableColumnOptions extends DataTableColumnOptionsBuilder(noOpts)
class DataTableColumnOptionsBuilder(val dict: OptMap)
extends JSOptionBuilder[DataTableColumnOptions, DataTableColumnOptionsBuilder](new DataTableColumnOptionsBuilder(_)) {
  def data(v: String)           = jsOpt("data", v)
  def className(v: String)      = jsOpt("className", v)
  def orderable(v: Boolean)     = jsOpt("orderable", v)
  def defaultContent(v: String) = jsOpt("defaultContent", v)
}

...
jQuery(() =>
      val taskTableIds   = Seq("", "name", "id", "status", "description", "attempts", "last", "script", "log", "depends_on", "dependents", "parent", "children")

      // Construct the DataTable for the tasks
      val taskDataTable = {
        import JQueryDataTableFacade._
        val dataTableColumns = taskTableIds.map { id => DataTableColumnOptions.data(id) }
        val dataTableOptions = DataTableOptions.destroy(true).columns(js.Array(dataTableColumns:_*)) //DataTableOptions.columns(js.Array(dataTableColumns:_*)).destroy(true)
        jQuery("#tasks").DataTable(dataTableOptions)
      }
    )

@nh13 nh13 reopened this Jul 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants