added related entries and stuff

This commit is contained in:
rulingcom 2025-06-04 22:37:42 +08:00
parent cbd370b31a
commit 21e2c0819b
6 changed files with 123 additions and 25 deletions

View File

@ -155,19 +155,43 @@ class UniversalTablesController < ApplicationController
"order" => ct.order "order" => ct.order
} if text != "" } if text != ""
end end
sorted = rows.sort{ |k,v| k["order"] <=> v["order"] } sorted = rows.sort{ |k,v| k["order"] <=> v["order"] }
sorted << { sorted << {
"title" => t("universal_table.hashtags"),
"text" => entry.tags_for_frontend
}
entry.inc(view_count: 1)
related_entries = []
entry.get_related_entries.each do |e|
rows = []
e.column_entries.each do |ce|
ct = ce.table_column
text = ce.get_frontend_text(ct)
if ct.is_link_to_show
text = "<a href='#{OrbitHelper.url_to_show(e.to_param)}'>#{text}</a>"
end
rows << {
"title" => ct.title,
"text" => text,
"url" => OrbitHelper.url_to_show(e.to_param)
} if text != ""
end
rows << {
"title" => t("universal_table.hashtags"), "title" => t("universal_table.hashtags"),
"text" => entry.tags_for_frontend "text" => e.tags_for_frontend
} }
entry.inc(view_count: 1) related_entries << {
{ "related_entry" => rows
"entry" => sorted, }
"extras" => { end
"view_count_head" => I18n.t("view_count"), {
"view_count" => entry.view_count "entry" => sorted,
} "related_entries" => related_entries,
} "extras" => {
"view_count_head" => I18n.t("view_count"),
"view_count" => entry.view_count
}
}
end end
def download_file def download_file
@ -334,7 +358,7 @@ class UniversalTablesController < ApplicationController
if !ce.nil? if !ce.nil?
text = ce.get_frontend_text(column) text = ce.get_frontend_text(column)
if column.is_link_to_show if column.is_link_to_show
text = "<a href='#{OrbitHelper.url_to_show("-" + te.uid)}'>#{text}</a>" text = "<a href='#{OrbitHelper.url_to_show(te.to_param)}'>#{text}</a>"
end end
cols << {"text" => text} cols << {"text" => text}

View File

@ -3,10 +3,11 @@ class TableEntry
include Mongoid::Timestamps include Mongoid::Timestamps
include Slug include Slug
attr_accessor :sort_value attr_accessor :sort_value
field :have_data, type: Boolean, localize: true field :have_data, type: Boolean, localize: true
field :sort_number, type: Integer field :sort_number, type: Integer
field :view_count, type: Integer, default: 0 field :view_count, type: Integer, default: 0
field :related_entries, type: String, default: ""
has_many :column_entries, :dependent => :destroy has_many :column_entries, :dependent => :destroy
belongs_to :u_table, index: true belongs_to :u_table, index: true
@ -32,6 +33,11 @@ class TableEntry
self.class.where(:id=> self.id).update_all(have_data_translations.map{|l, v| ["have_data.#{l}", v]}.to_h) self.class.where(:id=> self.id).update_all(have_data_translations.map{|l, v| ["have_data.#{l}", v]}.to_h)
end end
def get_related_entries
tids = self.related_entries.split(',')
TableEntry.find(tids)
end
def get_have_data def get_have_data
searchable_field_ids = TableColumn.filter_searchable.where(u_table_id: self.u_table_id).pluck(:id) searchable_field_ids = TableColumn.filter_searchable.where(u_table_id: self.u_table_id).pluck(:id)
searchable_column_entries = self.column_entries.where(:table_column_id.in=> searchable_field_ids).to_a searchable_column_entries = self.column_entries.where(:table_column_id.in=> searchable_field_ids).to_a

View File

@ -15,6 +15,9 @@
#s2id_autogen1{ #s2id_autogen1{
width: 500px !important; width: 500px !important;
} }
#s2id_autogen2 {
width: 500px !important;
}
</style> </style>
<div class="input-area"> <div class="input-area">
@ -24,6 +27,15 @@
<input id="universal_table_tags" name="table_tags" /> <input id="universal_table_tags" name="table_tags" />
</div> </div>
</div> </div>
<%
tbData = @entry.get_related_entries.map{|tb| {id: tb.id.to_s, text: tb.column_entries.first.text}}
%>
<div class="control-group">
<label class="control-label"><%= t("universal_table.related_entries") %></label>
<div class="controls">
<%= f.text_field :related_entries, :class => "select2", data: { value: tbData } %>
</div>
</div>
<% @columns.each_with_index do |column, index| %> <% @columns.each_with_index do |column, index| %>
<% if @entry.new_record? %> <% if @entry.new_record? %>
<% object = f.object.send(:column_entries).build rescue nil %> <% object = f.object.send(:column_entries).build rescue nil %>
@ -56,11 +68,55 @@ $("#universal_table_tags").select2({
if (!data.length) if (!data.length)
return { id: term, text: "#" + term.trim().toLowerCase() }; return { id: term, text: "#" + term.trim().toLowerCase() };
} }
// ajax: {
// url: '/api/v1.1/locations',
// dataType: 'json'
// }
}); });
$("#universal_table_tags").val(<%= raw(@entry.table_tags.collect { |tag| tag.id.to_s }) %>).trigger("change"); $("#universal_table_tags").val(<%= raw(@entry.table_tags.collect { |tag| tag.id.to_s }) %>).trigger("change");
var select2Options = {
maximumSelectionSize: 10,
minimumResultsForSearch: Infinity,
multiple: true,
minimumInputLength: 2,
placeholder: "<%= t("universal_table.search_entries") %>",
ajax: {
type: 'get',
url: "/admin/universal_tables/get_entries?uid=<%= @entry.u_table.uid %>",
allowClear: true,
dataType: 'json',
delay: 250,
data: function (term) {
var query = {
q: term
}
return query;
},
results: function(data, page) {
return {
results: data
};
},
cache: true
},
formatResult: function(i) {
return '<div>' + i.text + '</div>';
}, // Formats results in drop down
formatSelection: function(i) {
return '<div>' + i.text + '</div>';
}, //Formats result that is selected
escapeMarkup: function(m) {
return m;
} // we do not want to escape markup since we are displaying html in results
}
$("#table_entry_related_entries").select2(select2Options);
$('.select2').each(function () {
const data = $(this).data('value');
if (data) {
console.log($(this))
$(this).select2({
data: data,
multiple: true,
width: '100%'
}).val(data.map(i => i.id)).trigger('change');
}
});
</script> </script>

View File

@ -30,4 +30,6 @@ en:
disable_editing: Disable editing disable_editing: Disable editing
enable_editing: Enable editing enable_editing: Enable editing
save_mind_map: Save mind map save_mind_map: Save mind map
hashtags: Hashtags hashtags: Hashtags
related_entries: Related entries
search_entries: Search entries

View File

@ -30,4 +30,6 @@ zh_tw:
disable_editing: Disable editing disable_editing: Disable editing
enable_editing: Enable editing enable_editing: Enable editing
save_mind_map: Save mind map save_mind_map: Save mind map
hashtags: Hashtags hashtags: Hashtags
related_entries: Related entries
search_entries: Search entries

View File

@ -14,7 +14,7 @@
<table class="table table-striped universal-table-show"> <table class="table table-striped universal-table-show">
<tbody data-level="0" data-list="entry"> <tbody data-level="0" data-list="entry">
<tr> <tr>
<td class="table-title">{{title}}</td> <td class="col-md-2 table-title">{{title}}</td>
<td>{{text}}</td> <td>{{text}}</td>
</tr> </tr>
</tbody> </tbody>
@ -22,4 +22,12 @@
<div class="view_count pull-right"> <div class="view_count pull-right">
<i class="fa fa-eye">{{view_count_head}}:</i> <i class="fa fa-eye">{{view_count_head}}:</i>
<span class="view-count">{{view_count}}</span> <span class="view-count">{{view_count}}</span>
</div> </div>
<div data-list="related_entries" data-level="0">
<tbody data-level="1" data-list="related_entry">
<tr>
<td>{{text}}</td>
</tr>
</tbody>
</div>