jbox-web / ajax-datatables-rails

A wrapper around DataTable's ajax methods that allow synchronization with server-side pagination in a Rails app
MIT License
584 stars 228 forks source link

Columns with structured data types #408

Open AndrewKvalheim opened 2 years ago

AndrewKvalheim commented 2 years ago

In past versions, datatables serialized into JSON that could represent columns containing arbitrarily structured data. For example, a column of post tags could contain an array—

def data
  records.map do |record|
    {
      title: record.title, # String
      tags:  record.tags,  # Array[String]
    }
  end
end

—and would be serialized as such:

{ "title": "Example", "tags": ["One", "Two"] }

This allowed the view to handle rendering:

columns = [
  { data: "title", render: (title) => escapeHtml(title) },
  { data: "tags", render: (tags) => tags.map((t) => `<span class="tag">${escapeHtml(t)}</span>`).join("") },
];

Since #199, returned records no longer contain data but fully rendered HTML views:

{ "title": "Example", "tags": "[&quot;One&quot;, &quot;Two&quot;]" }

This leaves a couple options for how to handle columns that contain structured data:

Neither is very appealing to me. Is it intended that structured column types just not be used, or is there a better way to address this?