Skip to content
Merged

Sync #27

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/apps/tables/templates/Tables/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<div id="app">
<tabs v-model:value="tab_current" type="card">
<tab-pane v-for="item in tabs" :key="item" :label="item" :name="item">
<apijson-table :table_name="item" :config="apijson_tables[item]"></apijson-table>
<apijson-table :model_name="item" :config="apijson_tables[item]"></apijson-table>
</tab-pane>
</tabs>
</div>
Expand Down
4 changes: 2 additions & 2 deletions uliweb_apijson/apijson/settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ user = {
"secret_fields" : ["password"],
"GET" : { "roles" : ["ADMIN","OWNER"] },
"HEAD" : { "roles" : ["ADMIN","OWNER"] },
"POST" : { "roles" : ["ADMIN","OWNER"] },
"POST" : { "roles" : ["ADMIN"] },
"PUT" : { "roles" : ["ADMIN","OWNER"] },
"DELETE" : { "roles" : ["ADMIN","OWNER"] },
"DELETE" : { "roles" : ["ADMIN"] },
}

[FUNCTIONS]
Expand Down
61 changes: 36 additions & 25 deletions uliweb_apijson/apijson/templates/vue/inc_apijson_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
Vue.component('apijson-table', {
delimiters: ['{', '}'],
props: [
"table_name",
"model_name", //apijson model name
"request_tag", //apijson request tag, default will be same with model name
"config",
"custom_tcolumns_render_generator",
"hook_init",
Expand Down Expand Up @@ -44,12 +45,14 @@
</div>
</modal>
<modal v-model="modal_delete" title="Confirm to delete" @on-ok="real_remove">
<p>Confirm to delete #{delete_params.row&&delete_params.row.id} in table '{table_name}'?</p>
<p>Confirm to delete #{delete_params.row&&delete_params.row.id} in table '{model_name}'?</p>
</modal>
</div>`,
data: function(){
var thisp = this
return {
l_request_tag: null,

loading: false,
modal_view: false,
viewedit_items: [],
Expand Down Expand Up @@ -172,7 +175,7 @@
"@page":thisp.current_page-1,
"@query":2
}
arr_params[this.table_name] = {
arr_params[this.model_name] = {
"@order":thisp.sort_key+thisp.sort_order,
"@role":"{{=role}}"
}
Expand Down Expand Up @@ -225,7 +228,7 @@
}
else {
thisp.$Notice.error({
title: 'error when get table '+thisp.table_name,
title: 'error when get table '+thisp.model_name,
desc: data.msg
})
}
Expand Down Expand Up @@ -276,18 +279,19 @@
save: function(){
var thisp = this
var params = {
"@tag": thisp.table_name
"@tag": thisp.l_request_tag
}
var record_params = {}
var row = thisp.edit_params.row

//only save modified fields
for (var k in thisp.viewedit_items) {
var d = thisp.viewedit_items[k]
if (d.key=="id"|| d.value!=row[d.key]) {
record_params[d.key] = d.value
}
}
params[thisp.table_name] = record_params
params[thisp.l_request_tag] = record_params
params = thisp.ajax_hook("apijson_put","update",params)
$.ajax({
type: "POST",
Expand All @@ -297,15 +301,15 @@
success: function (data) {
if (data.code==200){
thisp.$Notice.success({
title: 'success update #'+row.id+' in table '+thisp.table_name,
title: 'success update #'+row.id+' in table '+thisp.model_name,
desc: data.msg
})
thisp.modal_view = false
thisp.update_list()
}
else {
thisp.$Notice.error({
title: 'error when update #'+row.id+' in table '+thisp.table_name,
title: 'error when update #'+row.id+' in table '+thisp.model_name,
desc: data.msg
})
}
Expand All @@ -323,13 +327,13 @@
real_remove_set_deleted: function(){
var thisp = this
var params = {
"@tag": thisp.table_name
"@tag": thisp.l_request_tag
}
var params_table = {
"id": thisp.delete_params.row.id,
}
params_table[this.config_deleted_field_name] = true
params[thisp.table_name] = params_table
params[thisp.l_request_tag] = params_table

params = thisp.ajax_hook("apijson_put","delete",params)

Expand All @@ -341,21 +345,21 @@
success: function (data) {
if (data.code!=200){
thisp.$Notice.error({
title: 'error when remove #'+thisp.delete_params.row.id+' in table '+thisp.table_name,
title: 'error when remove #'+thisp.delete_params.row.id+' in table '+thisp.model_name,
desc: data.msg
})
return
}
var result = data[thisp.table_name]
var result = data[thisp.l_request_tag]
if (result.code!=200){
thisp.$Notice.error({
title: 'error when remove #'+thisp.delete_params.row.id+' in table '+thisp.table_name,
title: 'error when remove #'+thisp.delete_params.row.id+' in table '+thisp.model_name,
desc: result.msg
})
return
}
thisp.$Notice.success({
title: 'success remove #'+thisp.delete_params.row.id+' in table '+thisp.table_name,
title: 'success remove #'+thisp.delete_params.row.id+' in table '+thisp.model_name,
desc: result.msg
})
thisp.update_list()
Expand All @@ -365,9 +369,9 @@
real_remove_delete: function(){
var thisp = this
var params = {
"@tag": thisp.table_name
"@tag": thisp.l_request_tag
}
params[thisp.table_name] = {
params[thisp.l_request_tag] = {
"id": thisp.delete_params.row.id
}
params = thisp.ajax_hook("apijson_delete","delete",params)
Expand All @@ -379,21 +383,21 @@
success: function (data) {
if (data.code!=200){
thisp.$Notice.error({
title: 'error when remove #'+thisp.delete_params.row.id+' in table '+thisp.table_name,
title: 'error when remove #'+thisp.delete_params.row.id+' in table '+thisp.model_name,
desc: data.msg
})
return
}
var result = data[thisp.table_name]
var result = data[thisp.l_request_tag]
if (result.code!=200){
thisp.$Notice.error({
title: 'error when remove #'+thisp.delete_params.row.id+' in table '+thisp.table_name,
title: 'error when remove #'+thisp.delete_params.row.id+' in table '+thisp.model_name,
desc: result.msg
})
return
}
thisp.$Notice.success({
title: 'success remove #'+thisp.delete_params.row.id+' in table '+thisp.table_name,
title: 'success remove #'+thisp.delete_params.row.id+' in table '+thisp.model_name,
desc: result.msg
})
thisp.update_list()
Expand All @@ -411,14 +415,14 @@
real_add: function(){
thisp = this
var params = {
"@tag": thisp.table_name
"@tag": thisp.l_request_tag
}
var post_params = {}
for (var k in thisp.add_items) {
var d = thisp.add_items[k]
post_params[d.key] = d.value
}
params[this.table_name] = post_params
params[this.l_request_tag] = post_params
params = thisp.ajax_hook("apijson_post","add",params)
$.ajax({
type: "POST",
Expand All @@ -428,14 +432,14 @@
success: function (data) {
if (data.code!=200){
thisp.$Notice.error({
title: 'error when add new record in table '+thisp.table_name,
title: 'error when add new record in table '+thisp.model_name,
desc: data.msg
})
return
}
var result = data[thisp.table_name]
var result = data[thisp.l_request_tag]
thisp.$Notice.success({
title: 'success add #'+result.id+' in table '+thisp.table_name,
title: 'success add #'+result.id+' in table '+thisp.model_name,
desc: result.msg
})
thisp.update_list()
Expand All @@ -456,6 +460,13 @@
}
},
mounted: function(){
if (this.request_tag==null) {
this.l_request_tag = this.model_name
}
else {
this.l_request_tag = this.request_tag
}

if (this.config!=null){
this.config_editable = this.config.editable || false
this.config_table_fields = this.config.table_fields || null
Expand Down
149 changes: 149 additions & 0 deletions uliweb_apijson/apijson/templates/vue/inc_apijson_viewedit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<script>
Vue.component('apijson-viewedit', {
delimiters: ['{', '}'],
props: [
"model_name",
"request_tag",
"id",
"config",
"custom_tcolumns_render_generator",
"hook_init",
"hook_ajax_params"
],
template: `
<i-form @submit.native.prevent :label-width="80">
<form-item v-for="item in viewedit_items" :key="item.key" :label="item.title">
<i-input v-if="item.component=='input'" v-model="item.value" :readonly="!editable(item)"></i-input>
<checkbox v-if="item.component=='checkbox'" v-model="item.value" :disabled="!editable(item)"></checkbox>
<i-input v-if="item.component=='textarea'" v-model="item.value" type="textarea" :autosize="{minRows: 2,maxRows: 5}"></i-input>
</form-item>
<form-item v-if="config_editable" label="action">
<i-button type="info" icon="ios-download" size="large" @click="save">Save</i-button>
</form-item>
</i-form>
`,
data: function(){
return {
l_request_tag: null,
role: "{{=role}}",
row: {},
row_saved: {},
viewedit_items: [],

config_editable: false,
config_viewedit_fields: null,
}
},
methods: {
init_viewedit: function(){
var params = {}
var model_params = {
"id":this.id,
"@role":this.role
}
params[this.model_name] = model_params
var thisp = this
$.ajax({
type: "POST",
url: "{{=url_for('uliweb_apijson.apijson.views.ApiJson.get')}}",
contentType: 'application/json',
data: JSON.stringify(params),
success: function (data) {
if (data.code==200) {
thisp.row = data[thisp.model_name]
thisp.row_saved = thisp.row
thisp.viewedit_items = []
if (thisp.config_viewedit_fields!=null) {
for (var i in thisp.config_viewedit_fields) {
var d = thisp.config_viewedit_fields[i]
d.value = thisp.row[d.key]
d.component = d.component || "input"
thisp.viewedit_items.push(d)
}
}
}
else {
thisp.$Notice.error({
title: 'error when get table '+thisp.table_name,
desc: data.msg
})
}
}
})
},
editable: function(item){
var editable = true
if (item.editable!=null) {editable=item.editable}
return this.config_editable && editable && (item.key!="id")
},
save: function(){
var params = {
"@tag": this.l_request_tag
}
var record_params = {}
//only save modified fields
for (var k in this.viewedit_items) {
var d = this.viewedit_items[k]
if (d.key=="id"|| d.value!=this.row_saved[d.key]) {
record_params[d.key] = d.value
this.row[d.key] = d.value
}
}
params[this.l_request_tag] = record_params
params = this.ajax_hook("apijson_put","update",params)
var thisp = this
$.ajax({
type: "POST",
url: "{{=url_for('uliweb_apijson.apijson.views.ApiJson.put')}}",
contentType: 'application/json',
data: JSON.stringify(params),
success: function (data) {
if (data.code==200){
thisp.row_saved = thisp.row
thisp.$Notice.success({
title: 'success update #'+thisp.row.id+' in table '+thisp.model_name,
desc: data.msg
})
}
else {
thisp.$Notice.error({
title: 'error when update #'+thisp.row.id+' in table '+thisp.model_name,
desc: data.msg
})
}
}
})
},
ajax_hook: function(method,action,params) {
if (this.hook_ajax_params!=null) {
var after_hook = this.hook_ajax_params(method,action,params)
if (after_hook!=null) {
params = after_hook
}
else {
console.log("warning: hook_ajax_params('"+method+"','"+action+"',params) return null, so ignore this hook")
}
}
return params
}
},
mounted: function(){
if (this.request_tag==null) {
this.l_request_tag = this.model_name
}
else {
this.l_request_tag = this.request_tag
}
//if not do this, the first notice will hide behind the navigation bar in uliweb apps
this.$Notice.config({top: 100,duration: 8});
if (this.hook_init!=null) {
this.hook_init(this)
}
if (this.config!=null){
this.config_editable = this.config.editable || false
this.config_viewedit_fields = this.config.viewedit_fields || null
}
this.init_viewedit()
}
})
</script>
Loading